/** * @author Eightn0 * @create 2021-03-13 20:36 * Doubly linked list: one field stores data, two fields store pointers, one pointer points to the next node, and the other pointer points to the previous node * llink- data-rlink * Insert: Insert at the first position: point the right link of the new node to the first node of the original table, and the left link to null * Point the left link of the first node of the original table to the new node * Change the original table The head pointer head points to the new node * Insert at the last position: the right link of the last node of the original table points to the new node * The left link of the new node points to the last node of the original table, and the right link points to null * in the middle position Insert: remember that the left of the new node is ptr, which is again rtr * ptr The right link points to the new node * The left link of the new node points to ptr, the right link points to rtr * rtr The left link points to the new node * Delete: delete the first node: header Pointer head points to the next * New table head pointer points to null * Delete the last node: the right pointer of the penultimate node points to null * Delete the middle node: the right link of the previous node points to the next * The left link of the next node points to the previous */ class Node { int data; Node rnext; Node lnext; public Node(int data) { this.data = data; this.rnext = null; //Every node has Left pointer and right pointer, the default is null, and then assign this.lnext = null; } } public class Doubly { private Node first; private Node last; public boolean isEmpty(){ return first == null; } public void print( ){ Node current = first; while (current != null){ System.out.println("["+current.data+"]"); current = current.rnext; } System.out.println(); last.rnext = newN;//The end of the table The right link points to the new node } /*Insert node*/ public void insert(Node newN){ Node tmp; Node newNode; if (isEmpty()){ first = newN; first.rnext = last; last = newN; last.lnext = first; }else { if (newN.lnext == null){//Insert the header first.lnext = newN;//The left link of the original header points to the new node newN.rnext = first;//The right pointer of the new node points to the original header first = newN ;//The head of the table moves to the new pointer, the order can not be disordered }else { if (newN.rnext == null){//Insert the end of the table last = newN ; //The end of the new table points to the new node newN.lnext = last;//The left link of the new node points to the end of the original table Node newNode; }else {//Insert the middle newNode = first; tmp = first; while (newN.rnext != newNode.rnext){ tmp = newNode; newNode = newNode.rnext;//Search process } tmp.rnext = newN;// Insertion process newN.rnext = newNode; newNode.lnext = newN; newN.lnext = tmp; } } } } /*Delete node*/ public void delete(Node delNode){ if (first == null){/ / Determine whether the linked list is empty Node tmp; System.out.println("[The table is empty]"); return; } if (delNode == null){//Determine whether the delete condition exists System.out.println("[Error: del is not a table Node]"); return; } if (first.data == delNode.data){//Delete the header first = first.rnext;//Start moving to the second node first.lnext = null;// Set the left link of the second node to null }else if (last.data == delNode.data){//Delete the end of the table newNode = first; while (newNode.rnext != last) newNode = newNode.rnext; newNode .rnext = null; last = newNode; }else {//Delete the node in the middle of the table newNode = first; tmp = first; while (newNode.data != delNode.data){ tmp = newNode;//Search process newNode = newNode.rnext; } tmp.rnext = delNode.rnext;//Delete process tmp.lnext = delNode.lnext; } } }
Add and delete elements of doubly linked list
Guess you like
Origin blog.csdn.net/vivian233/article/details/114764841
Ranking