首页 > 代码库 > 爪哇国新游记之十六----泛型单链表类

爪哇国新游记之十六----泛型单链表类

/** * 单链表节点类 * @param <T> */class Node<T extends Object>{    protected T value;    protected Node next;}/**  * 单链表类 * @param <T> */public class ChainList<T extends Object>{    private Node<T> first;        public void addTail(T t){        Node<T> newNode=new Node<T>();        newNode.value=t;                if(first==null){                                    first=newNode;        }        else{            Node<T> curr=first;                        while(curr.next!=null){                curr=curr.next;            }                        curr.next=newNode;        }    }        public void addHead(T t){        Node<T> newNode=new Node<T>();        newNode.value=t;        newNode.next=first;        first=newNode;    }        public int size(){        int size=0;        Node<T> curr=first;                while(curr!=null){            size++;            curr=curr.next;        }                return size;    }        public T get(int index){        if(index<0){            throw new IllegalArgumentException("下标不能是负数");        }        else{            Node<T> curr=first;                        while(curr!=null){                                if(index==0){                    return curr.value;                }                                index--;                curr=curr.next;            }                        throw new NullPointerException();        }    }        public void remove(int index){        if(index<0){            throw new IllegalArgumentException("下标不能是负数");        }        else{                        if(index==0){                Node<T> next=first.next;                                first=next;            }            else{                            Node<T> curr=first;                Node<T> prev=curr;                                while(curr!=null){                                    if(index==0){                        prev.next=curr.next;                        return;                    }                                        index--;                    prev=curr;                    curr=curr.next;                }                                throw new NullPointerException();            }        }    }        public void display(){        System.out.print("链表元素有");        Node<T> curr=first;                while(curr!=null){            System.out.print(curr.value+",");            curr=curr.next;        }        System.out.println();    }        public static void main(String[] args){        ChainList<String> ls=new ChainList<String>();                ls.addTail("A");        ls.addTail("B");        ls.addTail("C");        ls.display();                ls.addHead("1");        ls.display();        System.out.println("数组长度为:"+ls.size());        System.out.println("第三个元素为:"+ls.get(2));                for(int i=0;i<ls.size();i++){            System.out.println("第"+(i+1)+"个元素为:"+ls.get(i));        }                ls.addTail("E");        ls.addTail("F");        ls.remove(0);                ls.display();                ls.remove(1);                ls.display();                ls.remove(ls.size()-1);                ls.display();                for(int i=0;i<ls.size();i++){            System.out.println("第"+(i+1)+"个元素为:"+ls.get(i));        }    }}

输出:

链表元素有A,B,C,链表元素有1,A,B,C,数组长度为:4第三个元素为:B第1个元素为:1第2个元素为:A第3个元素为:B第4个元素为:C链表元素有A,B,C,E,F,链表元素有A,C,E,F,链表元素有A,C,E,第1个元素为:A第2个元素为:C第3个元素为:E