双向链表--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();
    }
}

 

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