This article comes from liuyubobobo bobobobo's video tutorial "Playing Data Structures from Initial to Advanced"
The list structure is shown below. The list node contains an e attribute and a next attribute.
Using java code to implement linked list nodes
// struct Node private class Node{ // Value of the current node public E e; // The next attribute of the current node points to the next node public Node next; public Node(E e, Node next){ this.e = e; this.next = next; } public Node(E e){ this.e = e; this.next = null; } public Node(){ this.e = null; this.next = null; } @Override public String toString(){ return e.toString(); } }
To implement queues through linked lists, you need to add the head and tail of the queue, as shown below.
The current head points to Node0, and the next in Node0 points to Node1, which is node0.next=node1
java code implementation
//Implementing a queue using a linked list public class LinkListQueue<E>{ // struct Node private class Node{ // Value of the current node public E e; // The next attribute of the current node points to the next node public Node next; public Node(E e, Node next){ this.e = e; this.next = next; } public Node(E e){ this.e = e; this.next = null; } public Node(){ this.e = null; this.next = null; } @Override public String toString(){ return e.toString(); } } private Node head, tail; //Head and tail of team private int size; //Queue size // Initialization queue public LinkListQueue(){ head = null; tail = null; size = 0; } public int getSize(){ return size; } public boolean isEmpty(){ return size == 0; } // Enter the team and see the illustrations at the end of the article public void enqueue(E e){ // Tail = null queue without elements if (tail == null){ tail = new Node(e); head = tail; }else { // There are already elements in the queue. The next of the last element points to a new Node. tail.next = new Node(e); // The tail attribute maintained in the queue class (the end of the queue) also points to the newly created node. tail = tail.next; } size++; // Queue size plus one } //Get out of line and look at the illustrations at the back of the article public E dequeue(){ if (isEmpty()) throw new RuntimeException("The queue was empty."); //Get the first node of the team Node cur = head; // The head attribute maintained in the queue class (the head of the queue) points to the next node head = head.next; // Delete the reference to the queue node to allow the java virtual machine to reclaim memory space cur.next = null; // The head of the queue points to null, indicating that there are no elements in the queue if (head == null) tail = null; size--; // Queue size minus one // Returns the value of the first node of the queue return cur.e; } @Override public String toString(){ StringBuilder res = new StringBuilder(); res.append("Queue: front "); Node cur = head; while(cur != null) { res.append(cur + "->"); cur = cur.next; } res.append("NULL tail"); return res.toString(); } public static void main(String[] args){ LinkListQueue<Integer> queue = new LinkListQueue<>(); for(int i = 0 ; i < 10 ; i ++){ queue.enqueue(i); System.out.println(queue); if(i % 3 == 0){ queue.dequeue(); System.out.println(queue); } } for(int i = 0 ; i < 7 ; i ++){ queue.dequeue(); System.out.println(queue); } } }
1, out of team
2. Entering the queue is to add a Node node, so that the next of Node4 points to the new Node node, while tail also points to the new Node node.