java容器之自定义LinkList

前言

上一篇文章,我重写了arraylist,此次重写LInklist。

实现

LinkList实现图:


节点类:

package linklist;

public class Node {

    //上一个节点

    Node previous;

    //下一个节点

    Node next;

    //数据

    Object element;

    public Node(Node previous,Node next,Object element){

        this.element=element;

        this.previous=previous;

        this.next=next;

    }

    public Node(Object element){

        this.element=element;

    }

}

实现类:

package linklist;

public class NewLInklist {

    //首节点

    private Node first;

    //结尾节点

    private Node last;

    //list长度

    private int size;

    //add方法

    public void add(E obj){

        Node node=new Node(obj);

        if(first==null){ //第一个元素插入时首尾同为一节点

            first=node;

            last=node;

        }else{//否则首尾节点相连

            node.previous=last;

            node.next=null;

            last.next=node;

            last=node;

        }

    }

    //get方法

    public E get(int index){

        Node a=first;

        for(int i=0;i

            a=a.next;

        }

        return (E)a.element;

    }

    //get方法2

    //get方法

    public Node get2(int index){

        Node a=first;

        for(int i=0;i

            a=a.next;

        }

        return a;

    }

    //remove方法

    public void remove(int index){

      Node temp=get2(index);

      if(temp!=null){

          //记录index前面节点、后面节点

          Node up=temp.previous;

          Node down=temp.next;

          //判断是不是首尾节点首尾节点特殊处理,

          if(up!=null)

          up.next=down;

          if(down!=null)

          down.previous=up;

          if(index==0){

              first=first.next;

          }

          if(index==size-1){

              last=last.previous;

          }

          size--;

      }

    }

    //insert方法

    public void add(int index,E obj){

        Node temp=get2(index);

        if(temp!=null) {

            //记录index前面节点)

            Node n = new Node(obj);

            Node down = temp.next;

            temp.next=n;

            n.previous=temp;

            if(down!=null) {

                n.next = down;

                down.previous = n;

            }

        }

    }

    //toString方法

    public String toString(){

        StringBuilder sb=new StringBuilder();

        sb.append("[");

        Node b=first;

        while (b!=null){

            sb.append(b.element+",");

            b=b.next;

        }

        sb.setCharAt(sb.length()-1,']');

        return sb.toString();

    }

    public static void main(String[] args) {

        NewLInklist list=new NewLInklist<>();

        //测试add

        list.add("a");

        list.add("b");

        list.add("c");

        list.add("d");

        //测试tostring

        System.out.println(list);

        //测试get

        System.out.println(list.get(1));

        //测试remove

        list.remove(3);

        System.out.println(list);

        //测试insert

        list.add(2,"1");

        System.out.println(list);

    }

}

---------------------

作者:apple596529

来源:CSDN

原文:https://blog.csdn.net/apple596529/article/details/89397892

版权声明:本文为博主原创文章,转载请附上博文链接!

你可能感兴趣的:(java容器之自定义LinkList)