Implementation of the Java data structure queue

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.