用java實現單鏈表

來源:互聯網
上載者:User

首先建立介面聲明:聲明單鏈表的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();
    }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.