introduction
In the previous blog post, I briefly shared some knowledge about linked lists and some interview questions with you. If you are interested, you can go and have a look! Today I will bring you the implementation of doubly linked list and doubly linked list with puppet nodes. Look carefully, each step has a specific reason and understanding, I hope to help everyone!
Detailed step-by-step analysis , let's learn while watching the code!
Implementation of doubly linked list
class ListNode{ public int val;//Value field public ListNode prev; public ListNode next; public ListNode(int val) { this.val = val; } } public class DoubleLinkedList { public ListNode head;//Point to the head node of the double linked list public ListNode last;//Point to the tail node of the double-linked list //Print the linked list -- the same way as printing a single-linked list public void display(){ ListNode cur = this.head; while (cur!=null) { System.out.print (cur.val + " "); cur = cur.next; } System.out.println(); } //Get the length of the singly linked list public int size(){ int count = 0;//Record the number of nodes ListNode cur = this.head; while (cur!=null) { count++; cur = cur.next; } return count; public boolean contains(int key){ ListNode cur = this.head; while (cur!=null) { if(cur.val == key) {//If cur.val is equal to key, return true return true; }/ /Otherwise continue to go down cur = cur.next; } return false; } //Head insertion method public void addFirst(int data){ ListNode node = new ListNode(data);//New inserted node //Insert at any position, the first data node is subscript 0 if(head == null) {//If the linked list is empty when inserting, both head and tail point to new node node this.head = node; this.last = node; }else { node.next = head; head.prev = node; this.head = node; } } //尾插法 public void addLast(int data){ ListNode node = new ListNode(data); if(this.head == null) { this.head = node; this.last = node; }else { this.last.next = node; node.prev = last; this.last = node; } } public void addIndex(int index,int data){ ListNode node = new ListNode(data); if(index<0 || index>size()){ System.out.println("index位置不合法"); } if(index == 0) { addFirst(data); } if(index==size()){ addLast(data); } ListNode cur = searchIndex(index); node.next = cur.prev.next; cur.prev.next = node; node.prev = cur.prev; cur.prev = node; } public ListNode searchIndex(int index) { ListNode cur = this.head;//Create a method to find the position of cur when inserting while (index != 0) { cur = cur.next; index--; } return cur; } //Delete the node whose first occurrence of the keyword is key//Deleting the first occurrence of the keyword in the singly linked list needs to find the keyword precursor //The double linked list only needs to find the node directly public void remove(int key){ ListNode cur = this.head; while (cur!=null) { if(cur.val == key) {//There are two cases here if(cur == head) {//1.cur equals head Node head = head.next; head.prev = null;//manually set to empty }else {//2.cur is not equal to the head node cur.prev.next = cur.next; if(cur.next!= null) { //middle position cur.next.prev = cur.prev; }else {//tail position last = last.prev; } } return; }else { cur = cur.next; } } } //Remove all nodes whose value is key public void removeAllKey(int key){ ListNode cur = this.head; while (cur != null) { if (cur.val == key) { if (cur == head) { head = head.next; if (head != null) { head.prev = null; } else { last = null; } } } else { cur.prev.next = cur .next; if (cur.next != null) { //中间位置 cur.next.prev = cur.prev; } else { last = last.prev; } } //return; } cur = cur.next; } } public void clear(){ while (head!=null) { ListNode curNext = head.next; head.next = null; head.prev = null; head = curNext; } last = null; }
Implementation of Addition, Deletion, Addition and Modification of Doubly Linked List
public class TestDemo { public static void main(String[] args) { DoubleLinkedList doubleLinkedList = new DoubleLinkedList(); doubleLinkedList.addLast(12); doubleLinkedList.addLast(13); doubleLinkedList.addLast(14); doubleLinkedList.addLast(15); doubleLinkedList.addLast(16); doubleLinkedList.display(); System.out.println("================="); doubleLinkedList.addIndex(3,99); doubleLinkedList.display(); } }
Implementation of Doubly Linked List with Puppet Nodes
class ListNode{ public int val; public ListNode next; public ListNode prev; public ListNode(int val) { this.val = val; } } public class DoubleLinkedList {//Double linked list with dummy nodes public ListNode dummyHead;//Virtual node public ListNode last;//Tail node public DoubleLinkedList() { this.dummyHead = new ListNode(-1);//Puppet node } //Print singly linked list public void display() { ListNode cur = this.dummyHead.next; / /Refer to cur instead of head, head is equal to the next node of the puppet node while (cur != null) { System.out.print(cur.val + " "); cur = cur.next; } System.out.println(); } //Get the length of the singly linked list public int size() { int count = 0;//Record the number of nodes ListNode cur = this.dummyHead.next; while (cur != null) { count++; cur = cur.next; } return count; } //Head insertion method public void addFirst(int data) { ListNode node = new ListNode(data); //If there is no node inserted for the first time if ( this.dummyHead.next == null) { node.prev = dummyHead; this.dummyHead.next = node; this.last = node; return; } else { //The newly inserted node is connected to its front and back nodes node.next = this.dummyHead.next; node.prev = this.dummyHead; this.dummyHead.next.prev = node; this.dummyHead.next = node; } } //End insertion method public void addLast(int data) { ListNode node = new ListNode(-1); //Determine whether the head node is empty if (this.dummyHead.next == null && this.last.next = = null) { this.dummyHead.next = node; node.prev = this.dummyHead; this.last = node; return; } else { this.last.next = node; node.prev = last; this.last = node; } } //Insert at any position, the first data node is subscript 0 public void addIndex(int index, int data) { ListNode node = new ListNode(data); if(index<0 || index>size()){ System.out.println("index position is invalid"); return; }else { if(index == 0) { addFirst(data); } if(index == size()) { addLast(data); }else { //ListNode in the middle position cur = searchIndex(index); node.next = cur; cur.prev.next = node; node.prev = cur.prev; cur.prev = node; } } } //Create a method to find the position of cur public ListNode searchIndex(int index) { //First find the position of cur ListNode cur = this.dummyHead.next; while (index!=0) { cur = cur.next; index--; } return cur; } //Find whether the keyword key is included in the singly linked list public boolean contains(int key ){ ListNode cur = this.dummyHead.next; while (cur!=null) { if(cur.val == key) { return true; } cur = cur.next; } return false; } //Delete the node whose first occurrence of the keyword is key//Delete the first occurrence of the keyword in the singly linked list, you need to find the keyword precursor //Double linked list Just find this node directly public void remove(int key){ ListNode cur = this.dummyHead.next; if(cur.val == key){//There are two cases where cur=head or cur is not equal to head if(cur == this.dummyHead.next) {//cur==head this.dummyHead.next = this.dummyHead.next.next; this.dummyHead.next.next.prev = null; }else {//cur is not equal to There are two more cases of head //1.cur is in the middle position 2.cur is in the last position cur.prev.next = cur.next; //Determine whether it is the last node //The node to be deleted is not the last node if(cur.next!=null){ cur.next.prev = cur.prev; }else { this.last = cur.prev; } } return; }else { cur = cur.next; } } //Remove all nodes whose value is key public void removeAllKey(int key){ ListNode cur = this.dummyHead.next; while (cur!=null) { if(cur.val == key){ if(cur == this.dummyHead.next) { this.dummyHead.next = this.dummyHead.next.next; this.dummyHead.next.prev = null; }else { cur.prev.next = cur.next; //Judging if it is the last node if(cur.next == null) { this.last = cur.prev; }else { cur.next.prev = cur.prev; } } } cur = cur.next; } } public void clear(){ ListNode cur = this.dummyHead. next; ListNode curNext = cur.next; while (cur!=null) { cur.prev = null; cur.next = null; cur = curNext; } this.dummyHead = null; this.last = null; } }