用java语言实现单链表

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->  1 package com.base.list;

import java.util.HashSet;
import java.util.Set;


public class SingleLinkList {

    class Element {
        public Object value = null;
        public Element nextNode = null;
    }
    
    class Value
    {
        public String code;
        public String name;
        
        public Value(){}
        public Value(String code, String name){ this.code = code;this.name = name;}
        @Override
        public String toString() {
            return code + "-" + name ;
        }
        
        
    }
    private Element header = null;

    public static void main(String[] args) {
        SingleLinkList list = new SingleLinkList();
        Value value1 = list.new Value("1", "java");
        Value value2 = list.new Value("2", "c++");
        Value value3 = list.new Value("3", "c#");
        Value value4 = list.new Value("4", "vb");
        list.add(value1);
        list.add(value2);
        list.add(value3);
        list.add(value4);
        System.out.println("remove vb ?" + list.remove(value4));
        System.out.println("have c++ ?" + list.contains(value2));
        System.out.println("have vb ?" + list.contains(value4));
        System.out.println("list is emptry ? " + list.isEmpty());
        System.out.println(list);
        list.clear();
        System.out.println("list is emptry ? " + list.isEmpty());
    }
    

    public void add(Object node) {
        if (header == null) {
            header = new Element();
            header.value = null;
            header.nextNode = null;
        }
        Element element = new Element();
        element.value = node;
        element.nextNode = header.nextNode;
        header.nextNode = element;
    }

    public void clear() {
        header = null;
    }

    public boolean contains(Object o) {
        if(header==null)
            return false;
        Element eqEl = header.nextNode;
        while(eqEl!=null)
        {
            if(eqEl.value==o)
            {
                return true;
            }
            eqEl = eqEl.nextNode;
        }
        return false;
    }

    
    public String toString() {
        int size = this.size();
        String print = "";
        if(size==0)
            return print; 
        for( int i = 0; i < size ;i ++)
        {
            print = "," + this.get(i) + print;
        }
        print = "[" + print.substring(1) + "]";
        return print;
    }


    public Object get(int index) {
        if(header==null)
            return null;
        int size = this.size();
        if(index > (size - 1) || index < 0)
        {
            return null;
        }
        Element temp = header.nextNode;
        int i  = 0;
        while(temp!=null)
        {
            if(index == i)
            {
                return temp.value;
            }
            i++;
            temp = temp.nextNode;
        }
        return null;
    }

    private Element getElement(int index) {
        if(header==null)
            return null;
        int size = this.size();
        if(index > (size - 1) || index < 0)
        {
            return null;
        }
        Element temp = header.nextNode;
        int i  = 0;
        while(temp!=null)
        {
            if(index == i)
            {
                return temp;
            }
            i++;
            temp = temp.nextNode;
        }
        return null;
    }
    

    public boolean isEmpty() {
        if(header==null)
            return true;
        else
            return false;
    }


    public boolean remove(Object o) {
        if(header==null)
            return false;
        Element eqPreEl = header;
        Element eqEl = header.nextNode;
        while(eqEl!=null)
        {
            if(eqEl.value==o)
            {
                eqPreEl.nextNode = eqEl.nextNode;
                return true;
            }
            eqPreEl = eqEl;
            eqEl = eqEl.nextNode;
        }
        return false;
    }

    public int size() {
        if(header==null)
            return 0;
        Element temp = header.nextNode;
        int i  = 0;
        while(temp!=null)
        {
            i++;
            temp = temp.nextNode;
        }
        return i;
    }
    
    /**
     * 检查环状单链表
     * @return
     */
    public boolean checkLoop()
    {
        if(header==null)
            return false;
        int size = this.size();
        if(size==0)
            return false; 
        Set<Element> set = new HashSet<Element>();
        for( int i = 0; i < size ;i ++)
        {
            Element el = getElement(i);
            if(!set.contains(el)){
                set.add(el);
            }
            if(set.contains(el.nextNode))
            {
                return true;
            }
        }
        return false;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值