The common algorithm of Java in retreat cultivation--queue structure

Source: Internet
Author: User

   

     What is the queue structure:

        The queue structure is similar to the stack structure.

The same as the stack structure is a special operation rules, from the logical structure of the data, the queue structure in fact

is a linear structure .

Further partitioning from the storage structure is also divided into two categories:

          sequential queue structure: saves the data in the queue sequentially, even with a contiguous set of memory cells. In

program, you can define an array of structures of a specified size as a queue.

          chained queue structure: The value of each element in the queue is saved as a list.

     A typical queue structure:

Operations on both ends are allowed in the queue structure, but the operations on both ends are different. One end can only be deleted-- team head , a

End can only be inserted-the tail of the team .

     Operation rules for queues:

is to process the node data according to the advanced back-out (first in first Out,fifo).

Two basic operations for a general queue:

        into queue: adds an element to the end of the queue (equivalent to queuing at the end).

        out queue: takes the element of the team head out and deletes the element, making the latter element the opponent.

See more than hands to operate, haha, understand the code is important ~

1 ImportJava.util.Scanner;2 3 /*******************************************************4  *                                                        *5 * Queue Structure *6  *                                                        *7  *******************************************************/8 9 classdata4{Ten String name; One     intAge ; A } -  - classsqtype{ the     Static Final intQueuelen =15; -data4[] data =NewData4[queuelen];//Queue Array -     intHead//rival -     intTail//End of Team +      -@SuppressWarnings ("Unused") + Sqtype Sqtypeinit () { A Sqtype Q; at         if(q =NewSqtype ())! =NULL){//Request Memory -Q.head = 0;//Set the enemy -Q.tail = 0;//Set the tail -             returnQ; -         } -         Else{ in             return NULL;//return Empty -         } to     } +     //determine the empty queue -     intsqtypeisempty (Sqtype q) { the         inttemp = 0; *         if(Q.head = =q.tail) $temp = 1;Panax Notoginseng         return(temp); -     } the     //determine the full queue +     intsqtypeisfull (Sqtype q) { A         inttemp = 0; the         if(Q.tail = =Queuelen) +Temp =1; -         return(temp); $     } $     //Empty Queue -     voidsqtypeclear (Sqtype q) { -Q.head = 0;//Set the enemy theQ.tail = 0;//Set the tail -     }Wuyi     //Release Queue the     voidSqtypefree (Sqtype q) { -         if(q!=NULL){ Wuq=NULL; -         } About     } $     //into the queue -     intInsqtype (sqtype q,data4 data) { -         if(Q.tail = =Queuelen) { -System.out.println ("The queue is full! Operation failed! "); A             return(0); +         } the         Else{ -q.data[q.tail++] = data;//put elements into a pair of columns $             return(1); the         } the     } the     //out Queue the DATA4 Outsqtype (Sqtype q) { -         if(Q.head = =q.tail) { inSystem.out.println ("Queue with empty!") Operation failed! "); theSystem.exit (0); the         } About         Else{ the             returnq.data[q.head++]; the         } the         return NULL; +     } -     //read the node data. the DATA4 Peeksqtype (Sqtype q) {Bayi         if(Sqtypeisempty (q) ==1){ theSystem.out.println ("Empty queue"); the             return NULL; -         } -         Else{ the             returnQ.data[q.head]; the         } the     } the     //calculate the length of a column -     intSqtypelen (Sqtype q) { the         inttemp; thetemp = Q.tail-Q.head; the         return(temp);94     } the } the  the 98  Public classP2_4 { About  -@SuppressWarnings ("Resource")101      Public Static voidMain (string[] args) {102Sqtype st =NewSqtype ();103 DATA4 data1;104          theScanner input =NewScanner (system.in);106Sqtype stack = St. Sqtypeinit ();//Initialize Queue107System.out.println ("Into queue operation:");108System.out.println ("Enter the name of the age in the queue operation:");109          Do{ theDATA4 data =NewDATA4 ();111Data.name =Input.next (); theData.age =input.nextint ();113             if(Data.name.equals ("0")){ the                  Break;//If you enter 0, exit the             } the             Else{117 St. Insqtype (Stack, data);118             }119} while(true); -         121String temp = "1";122System.out.println ("Out of queue operation: Press any non-0 key to do the stack operation:");123temp =Input.next ();124          while(!temp.equals ("0")){ theData1 =St. Outsqtype (stack);126System.out.printf ("Out-of-queue data is (%s,%d) \ n", data1.name,data1.age);127temp =Input.next (); -         }129SYSTEM.OUT.PRINTLN ("The test is over! "); theSt. Sqtypefree (stack);//frees the space occupied by the queue131     } the 133}

          

The common algorithm of Java in retreat cultivation--queue structure

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.