Package Queue.linkqueue;public class Node {//Data Field object element;//pointer domain node next;//header node Initialize public node {This.next = Next;} Non-head node initialization public node (Object element, node next) {this.element = Element;this.next = Next;}}
***************************************************************************************
Package Queue.linkqueue;public class Linkqueue {node front;//head node node rear;//tail node int size;//Queue Size//Initialize an empty queue public linkque UE () {front = new Node (null); rear = Front;size = 0;} The size of the queue public int size () {return size;} Determines whether the queue is empty public boolean isEmpty () {return size = = 0;} into the queue public void enQueue (int data) {node node = new Node (data, null); rear.next = node;rear=node;size++;} Out of queue public Object DeQueue () {if (IsEmpty ()) {throw new Indexoutofboundsexception ("Queue is Empty");} Node node = front.next;//Gets a node Front.next = node.next;//The first node of the head node points to the next node of the first node if (node = = rear) {///If there is only one element in the queue, then the R The ear points to the head node rear = front;} Size--;return node.element;} Get the opponent element, do not delete public Object Getfront () {if (IsEmpty ()) {return null;} else {return front.next.element;}} The elements in the print queue public void traverse () {if (IsEmpty ()) {System.out.println ("null"),} else {node current = front.next;//The present node for (node node = current; node = null; node = node.next) {System.out.print (node.element + "");}} System.out.println ();}}
Java Data Structure Series--Queue (3): Chain storage structure of queue and its implementation