As with stacks, we can also use a single-linked list to implement queue ADT. Similarly, for efficiency reasons, we will use the first (last) node of the single-linked list as the first (last) node of the queue?? This avoids the inefficiency of the single-linked list in the tail-delete operation. In addition, two instance variables are required to indicate the first and stub points of the table, respectively.
The Java code is as follows:
Queuelist:
PackageCom.list.queue;ImportJava.util.Arrays;ImportCom.list.stack.Node;ImportCom.list.stack.ExceptionStackEmpty;/** * * @author gannyee * * * * Public class queuelist { //declared head Node Private StaticNode head =NewNode ();//declared Rear Node Private StaticNode rear =NewNode ();//declared Size Private Static intSize//queue List Private Static intLength//constructor initialize head, rear, size Public queuelist() {head = Rear =NULL; Size = length =0; }//get size Public int GetSize(){returnSize }//get length Public int length(){returnLength }//is Empty Public Boolean IsEmpty(){returnSize = =0; }//enqueue Element Public void Enqueue(Object Element) {Node NewNode =NewNode (Element,head);if(GetSize () = =0) {head = NewNode; Rear = NewNode; } head = NewNode; Size + +; length = size; }//dequeue Element PublicObjectdequeue()throwsexceptionstackempty{if(IsEmpty ())Throw NewExceptionstackempty ("Queue is empty!"); Node Travelnode =NewNode (); Travelnode = head; Object elementrear; while(Travelnode! =NULL){if(Travelnode.getnext () = = rear) Break; Travelnode = Travelnode.getnext (); } elementrear = Rear.getelement (); Rear = Travelnode; Size--;returnElementrear; }//get head node but not delete PublicObjectGetheadnode()throwsexceptionstackempty{if(IsEmpty ())Throw NewExceptionstackempty ("Queue is empty!");returnHead.getelement (); }//travel All node of the queue Public void getallelement() {Node traveltop; Traveltop = head; object[] Array =NewObject[getsize ()]; for(inti =0; i < Array.length;i + +) {Array[i] = traveltop.getelement (); Traveltop = Traveltop.getnext (); } System.out.println ("Get all elemnt:"+ arrays.tostring (array)); }}
Queuelisttest:
Packagecom. List. Queue;Importcom. List. Stack. Exceptionstackempty;public class Queuelisttest {public static void main (string[] args) throws Exceptionstackempty {//TODO Auto-ge nerated method Stub//declared queuelist example Queuelist QL = new Queuelist ();System. out. println("Size is:"+ QL. GetSize());System. out. println("is empty?"+ QL. IsEmpty());Ql. Enqueue( A);Ql. Enqueue( +);Ql. Enqueue( *);Ql. Enqueue( A);Ql. Enqueue( the);Ql. Enqueue( $);Ql. Enqueue( -);Ql. Enqueue(2);Ql. Enqueue( the);System. out. println("Size is:"+ QL. GetSize());System. out. println("is empty?"+ QL. IsEmpty());Ql. Getallelement();for (int i =0; i < ql.length (); i + +)System. out. println(QL. Dequeue());System. out. println("Size is:"+ QL. GetSize());System. out. println("is empty?"+ QL. IsEmpty());}}
Test results:
Size is:0 is Empty?trueSize is:9 is Empty?falseGetAll elemnt: [ the,2, -, $, the, A, *, +, A] A + * A the $ -2 theSize is:0 is Empty?true
As with the stack structure based on the single-linked list, the various queue ADT methods implemented above can be completed in O (1) time. Also, there is no need to limit the maximum size of the queue. Of course, constrained by the single-linked list structure, we still need to specialize in a variety of special cases, such as team space time.
Implementing a Java Custom Queue queue based on a linked list