(Java achieve) two-way circular list

What is a two-way circular list

Before looking at two-way circular linked list, if the list do not have a clear idea, I suggest you look at a single list and a one-way circular linked list , which is conducive to a better understanding of your following. (A little more than nonsense [to escape]

Compared singly linked list, two-way circular linked list is a more complex structure. Because the only way circular linked list of nodes contain pointers (next) point to the next node, further comprising a pointer (PREV) points to a front node.

  • Bidirectional circular list, only the first pointer is not visible head, as well as the tail node end. This is the difference between the list and single.
  • Before head pointer of the head of a two-way circular linked list node pointing end, tail end node of a node pointing head.

Basic Operations

Basic Operation way circular linked list are: increase (add), delete (remove), change (set), check (find), inserted (insert) and the like. Here we only explain remove, insert and getNode operations, other implementations can look at the source below.

Gets node

Since the two-way linked list of nodes visible (head and end), thus obtaining two-way circular linked list and single linked list node operation vary.

  • The node number and the length of the list needs to be acquired / 2 Comparative
  • If less than, the node is biased described before, and therefore from the head all the way down next
  • If it exceeds, the node is a partial explanation, and therefore all the way up from the end prev
  • This design enables the operation time complexity getNode shortened O (logN)
Removing elements
  • Gets node node elements to be deleted
  • The next pointer is provided a node before the node is a node of the node. Embodied as: node.prev.next = node.next
  • Prev The node pointers, a node is set to a front node of the node. Embodied as: node.next.prev = node.prev
  • Since there is no pointer to the node, node will be automatically cleanup
  • Variable length record list -1
Insert elements
  • Get the node node element to be inserted
  • Create a node mynode, next point node, prev points to node.prev
  • The next point to the node node.prev mynode
  • The node of the previous node prev pointing mynode

Two-way circular list of pros and cons

Advantage
  • Compared to single chain, two-way linked list of all the basic operations cycles are faster than a single linked list (java classes LinkList source is bidirectional circular linked list)
  • Node can get directly in front of a node, very flexible
Disadvantaged
  • Compared singly linked list, doubly linked list of memory space is obviously much larger

Doubly linked list of design applications of the "space for time" thinking algorithm designed to reduce the complexity of the operation time by consuming more space.


Source code implementation

public class Node<Anytype> {
    public Anytype data;//数据
    public Node<Anytype> prev;//前一个节点
    public Node<Anytype> next;//后一个节点
    public Node(Anytype data,Node<Anytype> prev,Node<Anytype> next){
        this.data=data;
        this.prev=prev;
        this.next=next;
    }
}

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

public class DoubleLink<AnyType> {
    Node<AnyType> head;//头指针
    Node<AnyType> end;//尾节点
    int size;//记录链表长度

    //初始化链表
    public void initlist(){
        end=new Node<>(null,null,null);
        head=new Node<>(null,null,end);
        end.prev=head;
        end.next=head;
        size=0;
    }

    //获取长度
    public int length(){
        return size;
    }

    //获取节点
    public Node<AnyType> getNode(int index){
        Node<AnyType> n;
        if(index>=size/2){
            n=end;
            for(int i=length();i>index;i--){
                n=n.prev;
            }
            return n;
        }
        else{
            n=head;
            for(int i=0;i<=index;i++){
                n=n.next;
            }
            return n;
        }
    }

    //添加元素
    public void add(AnyType a){
        Node<AnyType> renode=new Node<>(a,getNode(size-1),end);
        renode.prev.next=renode;
        renode.next.prev=renode;
        size++;
    }

    //插入元素
    public void insert(int i,AnyType a){
        Node<AnyType> n=getNode(i);
        Node<AnyType> renode=new Node<>(a,n.prev,n);
        n.prev.next=renode;
        n.prev=renode;
        size++;
    }

    //删除元素
    public AnyType remove(int i){
        Node<AnyType> n=getNode(i);
        AnyType data=n.data;
        n.prev.next=n.next;
        n.next.prev=n.prev;
        size--;
        return data;
    }

    //获取i位置的数据
    public AnyType get(int i){
        return getNode(i).data;
    }

    //为i位置元素重新赋值
    public AnyType set(int i,AnyType a){
        Node<AnyType> n=getNode(i);
        AnyType old=n.data;
        n.data=a;
        return old;

    }

    //清空链表
    public void clear(){
        initlist();
    }



    public void print(){
        for(int i=0;i<size;i++){
            System.out.println(getNode(i).data);
        }
    }
}

Guess you like

Origin www.cnblogs.com/sang-bit/p/11614232.html