"Data Structure Beginner" (Java Implementation chapter)--Queue

Source: Internet
Author: User

Original address: http://www.cnblogs.com/skywang12345/p/3603935.html

Original address: http://www.cnblogs.com/skywang12345/p/3603935.html

Original address: http://www.cnblogs.com/skywang12345/p/3603935.html

(Oneself is a javav beginner with the help of Daniel's article to record the Daily learning situation!) Everyone to go to this Daniel blog inside to see, inside a lot of dry absolutely welfare! )

The implementation of the queue is also available in the JDK package queue. The queue interface in the JDK is "queues", and its implementation classes are also queues, with the most linkedlist. This section describes the 2 types of Java implementations
1. Java implementation One: array implementation of the queue, can store any type of data. 2. Java implementation Two: example of a "queue" (LinkedList) that comes with the collection collection in Java.

1. Java implementation One: array implementation of the queue, can store any type of data

Implementation code (ARRAYQUEUE.JAVA)

/*** Java: Array Implementation "Queue", can only store int data. * * @authorSkywang * @date 2013/11/07*/ Public classArrayqueue {Private int[] Marray; Private intMCount;  PublicArrayqueue (intSZ) {Marray=New int[SZ]; MCount= 0; }    //add Val to the end of the queue     Public voidAddintval) {Marray[mcount++] =Val; }    //return to "queue start element"     Public intFront () {returnMarray[0]; }    //returns the top element value of the stack and removes the top element of the stack     Public intpop () {intRET = marray[0]; MCount--;  for(intI=1; i<=mcount; i++) Marray[i-1] =Marray[i]; returnret; }    //returns the size of the stack     Public intsize () {returnMCount; }    //returns whether the "stack" is empty     Public BooleanIsEmpty () {returnSize () ==0; }     Public Static voidMain (string[] args) {intTmp=0; Arrayqueue Astack=NewArrayqueue (12); //Push 10, 20, 30 into the stack in turnAstack.add (10); Astack.add (20); Astack.add (30); //assigns the top element of the stack to TMP and deletes the top element of the stackTMP =Astack.pop (); System.out.printf ("Tmp=%d\n", TMP); //assign only the top of the stack to TMP, and do not delete the element.TMP =Astack.front (); System.out.printf ("Tmp=%d\n", TMP); Astack.add (40); System.out.printf ("IsEmpty () =%b\n", Astack.isempty ()); System.out.printf ("Size () =%d\n", Astack.size ());  while(!Astack.isempty ()) {System.out.printf ("Size () =%d\n", Astack.pop ()); }    }}
Operation Result:
TMP=10tmp=20isEmpty ()=falsesize ()=3size ()=20size ()=30 size ()=40
Result Description: Arrayqueue is a queue implemented through arrays, and generics are used in Arrayqueue, so it supports any type of data.

2. Java implementation Two: example of a "queue" (LinkedList) that comes with the collection collection in Java

Implementation code (QUEUETEST.JAVA)

ImportJava.util.Stack;/*** Implement queue with "stack" * *@authorSkywang*/ Public classStacklist<t> {    //when adding data to a queue: (01) Move all existing data to Min. (02) Add "Newly added data" to Min.     PrivateStack<t> mIn =NULL; //when fetching elements from a queue: (01) Move all existing data to mout. (02) Return and delete the top element of the mout stack.     PrivateStack<t> Mout =NULL; //Statistics Count    Private intMCount = 0;  Publicstacklist () {mIn=NewStack<t>(); Mout=NewStack<t>(); MCount= 0; }    Private voidAdd (T t) {//move all existing data to min         while(!mout.empty ()) Min.push (Mout.pop ()); //Add "newly added data" to minMin.push (t); //Statistics +1mcount++; }    PrivateT Get () {//move all existing data to Mout         while(!min.empty ()) Mout.push (Min.pop ()); //Statistics-1mcount--; //returns and removes the top element of the Mout stack        returnMout.pop (); }    Private intsize () {returnMCount; }    Private BooleanIsEmpty () {returnMcount==0; }     Public Static voidMain (string[] args) {stacklist slist=Newstacklist (); //Push 10, 20, 30 into the stack in turnSlist.add (10); Slist.add (20); Slist.add (30); System.out.printf ("IsEmpty () =%b\n", Slist.isempty ()); System.out.printf ("Size () =%d\n", Slist.size ());  while(!Slist.isempty ()) {System.out.printf ("%d\n", Slist.get ()); }    }}
Operation Result:
TMP=10tmp=20isEmpty ()=falsesize ()=3tmp=20tmp=30tmp =40

"Data Structure Beginner" (Java Implementation chapter)--Queue (GO)

Related Article

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.