I remember in an interview with a company there is a question, write a two-way list, including the basic operation of the list, insert, delete, get the length of the operation, because of the time rush, the code is relatively messy, even I did not see, and then think that I have never carefully written the data structure, always feel that as long as the principle is clear, everything is fine, facts prove, theory. There is still a big gap between practice and practice.
Limited level, if there is a mistake, please don't hesitate to give advice.
Define an internal class Node for storing node elements
class Node { private Node previous;//Precursor node private Node next;//Successor node private E e;//Generic element values public Node(Node previous, Node next, E e) { this.previous = previous; this.next = next; this.e = e; }
The key of bi-directional linked list is the transfer of node pointer
Here's a brief introduction to removeElement(E value) as an example
public void removeElement(E value){ Node index=this.first;//Create index node to point to first node while(index!=null){ if(index.e==value)break; index=index.next; }//The while loop is used to traverse the entire list to get pointers to the nodes to be deleted index.previous.next=index.next; index.next.previous=index.previous; length--; }
index.previous indicates that the precursor node of the node is to be deleted
index.previous.next=index.next; meaning that the pointer to the successor of the precursor node is pointed to the successor node of the node to be deleted
Similarly, index.next denotes the successor node of the node to be deleted
index.next.previous=index.previous; meaning that the forward pointer of the successor node points to the precursor node to delete the node
Maybe it's a little winding. It's easy to draw a list structure diagram.
Insert Next (E base Element, E value) is the same as insert Previous (E base Element, E value), which is not discussed here.
DoubleLinkedList contains two node pointers (pseudo pointer, no pointer concept in java) first and last, pointing to the first and last elements of the list, respectively.
Whole code
public class DoubleLinkedList<E> { private Node first;//Point to the first element private Node last;//Point to the last element private int length=0;//Length of linked list class Node { private Node previous; private Node next; private E e; public Node(Node previous, Node next, E e) { this.previous = previous; this.next = next; this.e = e; } } /*** * By adding elements to the head node, the node structure should be invisible to the outside world, so only a generic value e is passed here. */ public void addFirst(E e) { if (first == null) {//Null List Judgment Node node = new Node(null, null, e);//Create a new node with empty precursors and successors this.first = node; this.last=node;//Point first and last pointers to the first element of the list length++;//The length of the list increases by one, the same as below }else{ Node node=new Node(null,first,e);//The list does not create a node whose precursor is empty, followed by the current first node, whose value is the incoming parameter e. this.first.previous=node;//The current first precursor is set to node this.first=node;//Point the first pointer to the new node length++; } } /*** *addLast Same addFirst */ public void addLast(E e) { if (last == null) { Node node = new Node(null, null, e); this.first = node; this.last=node; length++; }else{ Node node=new Node(last,null,e); this.last.next=node; this.last=node; length++; } } public void insertPrevious(E baseElement,E value){ Node index=this.first; while(index!=null){ if(index.e==baseElement)break; index=index.next; } Node insertValue=new Node(index.previous,index,value); index.previous.next=insertValue; index.previous=insertValue; length++; } public void insertNext(E baseElement,E value){ Node index=this.first; while(index!=null){ if(index.e==baseElement)break; index=index.next; } Node insertValue=new Node(index,index.next,value); index.next.previous=insertValue; index.next=insertValue; length++; } public void removeElement(E value){ Node index=this.first; while(index!=null){ if(index.e==value)break; index=index.next; } index.previous.next=index.next; index.next.previous=index.previous; length--; } public int getLength(){ return length; } @Override public String toString() { StringBuffer sb=new StringBuffer(); Node current=this.first; while(current!=null){ sb.append(current.e+"->"); current=current.next; } return sb.toString(); } public static void main(String[] args) { DoubleLinkedList<String> list=new DoubleLinkedList<>(); list.addLast("value1"); list.addLast("value2"); list.addLast("value3"); list.addLast("value4"); list.addFirst("value0"); list.insertPrevious("value3","insertValue"); list.insertNext("value3","insertValue2"); System.out.println(list.toString()); System.out.println("The length of the list is"+list.getLength()); list.removeElement("value3"); System.out.println(list.toString()); System.out.println("The length of the list is"+list.getLength()); } }