码农的空间

codding
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

用java实现单链表

Posted on 2009-10-15 20:07  我是孙海龙  阅读(465)  评论(0编辑  收藏  举报

首先创建接口声明:声明单链表的public方法,接口声明如下:

public interface IMyList<T>{
    public boolean add(T entry);

    public boolean add(int aPosition,T entry);
    public T remove(int aPosition);
    public boolean contains(T entry);
    public boolean replace(int aPosition,T entry);
    public int getLength();
    public boolean isFull();
    public boolean isEmpty();
}

然后定义链表使用的节点类型,Node类,代码如下:

class Node<T>{
    private T data;
    private Node<T> next;
    public Node(){}
    public Node(T anEntry){
        data=anEntry;
    }
    public T getData(){
        return data;
    }
    public Node<T> getNext(){
        return next;
    }
    public void setData(T entry){
        data=entry;
    }
    public void setNext(Node<T> node){
        next=node;
    }
}

最后实现单链表类,并进行插入测试,代码如下

public class MyList<T> implements IMyList<T>{
    private int length;
    private Node<T> firstNode;
    public MyList(){
        clear();       
    }
    private final void clear(){
        length=0;
    }
    public boolean add(T entry){
        Node<T> newNode=new Node<T>(entry);
        if(!isEmpty()){
            Node<T> lastNode=getNodeAt(length-1);
            lastNode.setNext(newNode);
            length++;
            return true;
        }else{
            firstNode=newNode;
            length++;
            return true;
        }
    }
    public boolean add(int aPosition,T entry){
        Node<T> newNode=new Node<T>(entry);
        if(aPosition==0){
            newNode.setNext(firstNode);
            //firstNode.setNext(newNode);
            firstNode=newNode;
            length++;
            return true;
        }
        if(!isEmpty()&&aPosition<length){
            Node<T> NodeBefor=getNodeAt(aPosition-1);
            Node<T> NodeAfter=NodeBefor.getNext();
            NodeBefor.setNext(newNode);
            newNode.setNext(NodeAfter);
            length++;
            return true;
        }
        return false;
    }
    public T remove(int aPosition){
        Node<T> currentNode=firstNode;
        if(aPosition==0){
            currentNode=firstNode;
            firstNode=firstNode.getNext();
            //return firstNode.getData();
        }else{
            int index=1;
            Node<T> beforNode=firstNode;
            while(index<aPosition-1&&beforNode!=null){
                beforNode=beforNode.getNext();
                index++;
            }
            currentNode=beforNode.getNext();
            beforNode.setNext(currentNode.getNext());
        }
        length--;
        return currentNode.getData();
    }
    public boolean contains(T entry){
        Node<T> currentNode=firstNode;
        int index=0;
        boolean find=false;
        while(index<length&&!find&&currentNode!=null){
            if(currentNode.getData().equals(entry)){
                find=true;
            }
        }
        return find;
    }
    public boolean replace(int aPosition,T entry){
        boolean done=false;
        int index=0;
        Node<T> currentNode=firstNode;
        while(!done&&index<length&&currentNode!=null){
            if(currentNode.getData().equals(entry))
                done=true;
            currentNode=currentNode.getNext();
            index++;
        }
        return done;
    }
    public int getLength(){
        return this.length;
    }
    private Node<T> getNodeAt(int aPosition){
        Node<T> currentNode=firstNode;
        for(int index=0;index<aPosition;++index){
            currentNode=currentNode.getNext();
        }
        return currentNode;
    }
    public boolean isEmpty(){
        return (length==0);
    }
    public boolean isFull(){
        return false;
    }
    public void display(){
        Node<T> currentNode=firstNode;
        int index=0;
        while(currentNode!=null&&index<length){
            System.out.println(currentNode.getData());
            currentNode=currentNode.getNext();
            index++;
        }
    }
    public static void main(String[] args){
        MyList<String> lst=new MyList<String>();
        lst.add("sunzhenxing");
        lst.add(0, "tongji");
        lst.add("sunhailong");
        lst.display();
        System.out.println("=============");
        lst.remove(0);
        lst.display();
        System.out.println("=============");
        lst.remove(0);
        lst.display();
    }
}