Java Algorithm (queue operation instance)

Source: Internet
Author: User

The complete code is as follows:

Package COM. xu. test; import Java. util. authorization; public class p5_1 {/*** @ function: queue operation instance * @ Author: * @ Date: 2012-10-20 */public static void main (string [] ARGs) {sqtype ST = new sqtype (); Data d = NULL; partition input = new partition (system. in); sqtype stack = ST. sqtypeinit (); system. out. println ("Queuing operation:"); system. out. println ("enter the name and age to join the queue:"); do {data Data = new data (); data. name = input. next (); data. age = input. nextint (); If (Data. name. equals ("0") {break; // enter "0", then exit} else {St. insqtype (stack, data) ;}} while (true); string temp = "1"; system. out. println ("team-out operation: Any team-out operation not 0:"); temp = input. next (); While (! Temp. equals ("0") {d = ST. outsqtype (stack); system. out. printf ("the data in the output queue is (% s, % d) \ n", D. name, D. age); temp = input. next ();} system. out. println ("test ended! "); ST. sqtypefree (stack) ;}} class data {string name; int age;} class sqtype {static final int queuelen = 15; data [] DATA = new data [queuelen]; // queue array int head; int tail; @ suppresswarnings ("UNUSED") sqtype sqtypeinit () {sqtype Q; If (q = new sqtype ())! = NULL) {q. head = 0; q. tail = 0; return Q;} else {return NULL;} int sqtypeisempty (sqtype q) // judge that the queue is empty {int temp = 0; If (Q. head = Q. tail) {temp = 1;} return temp;} int sqtypeisfull (sqtype q) // judge if the queue is full {int temp = 0; If (Q. tail = queuelen) return 1; return temp;} void sqtypeclear (sqtype q) // clear the queue {q. head = 0; q. tail = 0;} void sqtypefree (sqtype q) // release the queue {If (Q! = NULL) q = NULL;} int insqtype (sqtype Q, data) // incoming queue {If (Q. tail = queuelen) {system. out. println ("the queue is full! Operation failed! "); Return 0;} else {q. data [q. tail ++] = data; return 1 ;}} data outsqtype (sqtype q) // output queue {If (Q. head = Q. tail) {system. out. println ("the queue is empty. operation failed! "); System. exit (0);} else {return Q. data [q. head ++];} return NULL;} data peeksqtype (sqtype q) // read node data {If (sqtypeisempty (q) = 1) {system. out. println ("Empty queue! "); Return NULL;} else {return Q. data [q. head] ;}} int sqtypelen (sqtype q) // calculate the queue length {int temp; temp = Q. tail-Q. head; return temp ;}}

Running result:

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.