雙向鏈表--Java

interface  Linklist{
    void add(Object obj);
   boolean remove(int index);
   boolean set(int index,Object obj);
    Object get(int index);
    int contains(Object obj);
    int size();
    Object[] toArray();
    void printlink();
    void clear();
}
class LinklistImpl implements  Linklist{
    private Node first;
    private Node last;
    private int size=0;
    private class Node{
        private Node pre;
        private Object data;
        private Node next;
        public Node(Node pre,Object data,Node next){
            this.next=next;
            this.data=data;
            this.pre=pre;
        }
    }
    public void add(Object obj){  //尾插
        //先保存最後一個節點
        Node tmp=this.last;
        //創建新的節點
        Node newnode=new Node(tmp,obj,null);
        this.last=newnode;
        //判斷鏈表中是否只有這一個節點
        if(this.first==null){
            this.first=newnode;
        }else{
            tmp.next=newnode;
        }
        this.size++;
    }

    public boolean remove(int index) {
        //考慮三種情況
        //1.頭節點:後續由節點,後續無節點
        //2. 尾節點
        //3.中間節點
        if (!isLinkElements(index)) {
            return false;
        }
        Node node = node(index);
        if (node == this.first) { //頭節點
            if (node == this.last) {//後續無節點
                node=null;
                this.size--;
                return true;
            }else{//後續有節點
                Node tmp=this.first;
                this.first=node.next;
                tmp.next=null;
                this.first.pre=null;
                this.size--;
                return true;
            }
        }else if(node==this.last){//尾節點
            Node tmp=this.last;
            this.last=node.pre;
            tmp.pre=null;
            this.last.next=null;
            this.size--;
            return true;
        }
        //中間節點
        node.next.pre=node.pre;
        node.pre.next=node.next;
        node.pre=node.next=null;
        this.size--;
        return true;
    }
    public boolean set(int index,Object obj){ //根據索引找到節點,修改節點的值,返回修改前的索引下標
       if(!isLinkElements(index)){
           return false;
       }
       //找到索引
       Node node=node(index);
       //修改值
        node.data=obj;
        //返回修改前的索引
        return true;
    }
    public Object get(int index){  //根據索引返回內容
     if(!isLinkElements(index)){
         return null;
     }
     return node(index).data;
    }
    public int contains(Object obj){ //判斷指定內容是否存在,返回索引下標
      //判斷對象是否爲空,判斷對象爲空時返回index
        if(obj==null){
            int index=0;
            for(Node tmp=first;tmp!=null;tmp=tmp.next){
                if(tmp.data==null){
                    return index;
                }
                //索引繼續往後
                index++;
            }
            //指定內容不存在
            return -1;
        }else{ //對象不爲空時,equals判斷對象相等時,返回index
            int index=0;
            for(Node tmp=first;tmp!=null;tmp=tmp.next){
                if(tmp.data.equals(obj)){
                    return index;
                }
                index++;
            }
            return -1;
        }
    }
    public int size(){
     return this.size;
    }
    public Object[] toArray(){
  //初始化一個Object類對象數組
        Object [] result=new Object[size];
        int i=0;
        //遍歷
        for(Node tmp=first;tmp!=null;tmp=tmp.next){
            result[i++]=tmp.data;
        }
        //返回Object數組對象
        return result;
    }
    public void printlink(){
    //foreach遍歷輸出
        Object []obj=this.toArray();
        for(Object ret:obj){
            System.out.println(ret);
        }
    }
    public void clear(){
     for(Node tmp=first;tmp!=null;){
         //先保存下一個節點
         Node node=tmp.next;
         //前驅節點和後繼節點均置爲空
         tmp.pre=tmp.next=null;
         //tmp更新爲node
         tmp=node;
     }
    this.size=0;
    }
 //公共代碼,判斷索引是否合法,在size範圍內
    //僅供類內部使用
    private boolean isLinkElements(int index){
        return index>=0&&index<this.size;
    }
    //公共代碼,set和get方法均用到根據索引找節點
    private Node node(int index){
        //從前外後找
        if(index<(size>>1)){
            Node ret=this.first;
            for(int i=0;i<index;i++){
                ret=ret.next;
            }
            return ret;
        }
        //從後往前找
        Node ret=this.last;
        for(int i=size-1;i>index;i--){
            ret=ret.pre;
        }
        return ret;
    }
}

//測試類
public class Main{
    public static void main(String[] args) {
        Linklist link=new LinklistImpl();
        link.add("韓鵬博");
        link.add("張三");
        link.add("張爽");
        link.add("哈哈哈");
        link.add("李四");
        System.out.println(link.size());
        link.printlink();
       link.remove(0);
        link.remove(2);
        link.remove(4);
        link.printlink();
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章