Java Data Structure Queue code implementation, can be easily into the queue and out of the queue operation
/*** Implementation of the queue for Java data structures * 2016/4/27 **/ PackageCN. Link;ImportJava.util.*; Public classqueue{Node Tail=NewNode (); Node Nownode=NewNode ();//always point to the first team intsize; Queue () {}; Queue (String date) { This. tail.date =date; This. Tail.next =NULL; This. Nownode.next = This. Tail; This. Size = 1; System.out.println ("The queue has been established and" +date+ "is added to the queue"); } //Node Class classnode{String Date; Node Next; Node () {}; Node (String date) { This. Date =date; } } //inserting data into the queue Public voidEnQueue (String date) {Node Prenode=NewNode (date); Prenode.next= This. Nownode.next; This. Nownode.next =Prenode; This. size++; System.out.println ("Data" +date+ "is added to the queue"); } //Out-of- queue operations Public voidDeQueue () {Node Prenode=NewNode (); Prenode.next= This. Nownode.next; while(true){ if(PreNode.next.next! = This. Tail) {Prenode.next=PreNode.next.next; }Else{PreNode.next.next=NULL; This. Tail =Prenode.next; System.out.println ("Out of Queue complete ~ ~ ~"); Break; } } This. size--; } //traversing queue output Public voidPrintQueue () {Node Prenode=NewNode (); Prenode.next= This. Nownode.next; System.out.print ("Traverse output Queue:"); while(Prenode.next! =NULL) {System.out.print ("-" +preNode.next.date); Prenode.next=PreNode.next.next; } System.out.println ("+++++++++++ the current queue length is" + This. Size); } //Empty Queue Public voidClear () { This. Nownode.next =NULL; This. Tail =NULL; This. Size = 0; System.out.println ("The queue has been emptied"); } Public Static voidMain (string[] args) {Queue queue=NewQueue ("Sum1"); Queue. EnQueue ("Sum2"); Queue. EnQueue ("Sum3"); Queue. EnQueue ("Sum4"); Queue. EnQueue ("SUM5"); Queue. EnQueue ("SUM6"); Queue. EnQueue ("Sum7"); Queue. EnQueue ("SUM8"); Queue. PrintQueue (); Queue. DeQueue (); Queue. DeQueue (); Queue. PrintQueue (); }}
Implementation of the Java data structure queue