Java data structures and algorithms (Robert lafore) Chapter 4

Source: Internet
Author: User

Java data structures and algorithms (Robert lafore) Chapter 4

/* Write a method in the queue class of the program job 4.1 For the queue. Java program (listing 4.4) to display the queue content. Note that this is not a simple display of the array content. It is required to display data from the first inserted data item to the last inserted data item in the order of the inserted queue columns. Do not output a two-half fold because the array ends are bypassed. Note that no matter where the front and rear are located, a data item and no data item must be correctly displayed. 4.2 write a deque Class Based on the discussion of double-end queues in this chapter, which should include insertleft (), insertright (), removeleft (), removeright (), isempty (), isfull () method. It is required to support the rollback at the end of the data as in the queue. 4.3 compile a stack Class Based on the deque class of machine Job 4.2. This Stack class should have the same methods and functions as the stackx class in the stack. Java program (listing 4.1. 4.4 The priority queue shown in listing 4.6 can quickly delete data items with the highest priority, but data items are inserted slowly. It also includes a method for displaying the content of the priority queue, which must be the same as that in industry 4.1 on the computer. 4.5 queues are used to simulate the flow of people, cars, airplanes, businesses, and so on. Use the queue. Java program (listing 4.4) to write a program to simulate the collection queue of the supermarket. You can use the display () method of job 4.1 on the machine to display several queues of the customer. You can press a key to insert a new customer. Select a queue for the customer. The cashier's service time for each customer is random (it can be assumed that it depends on how much the customer has bought ). Once the account is cleared, the customer is deleted from the queue. For the sake of simplicity, the passage of time is simulated by hitting a key. It may take one minute to click a key table. (Of course, Java has a more complex way to process time .) */Package chap04; // queue. java // demonstrates queue // to run this program: c> JAVA queueapp //////////////////////////////////// //// // class queue {private int maxsize; private long [] quearray; private int front; private int rear; private int nitems; // specify public Queue (INT s) // constructor {maxsize = s; quearray = new long [Max Size]; front = 0; Rear =-1; nitems = 0;} // ------------------------------------------------------------ public void insert (long J) // put item at rear of queue {If (Rear = maxsize-1) // deal with wraparoundrear =-1; quearray [++ rear] = J; // increment rear and insertnitems ++; // one more item} // ---------------------------------------------------------- public long remove () // take item from FR Ont of queue {long temp = quearray [Front ++]; // get value and incr frontif (front = maxsize) // deal with wraparoundfront = 0; nitems --; // one less itemreturn temp;} // your public long peekfront () // peek at front of queue {return quearray [Front];} // your public Boolean isempty () // tru E if queue is empty {return (nitems = 0);} // empty public Boolean isfull () // true if queue is full {return (nitems = maxsize );} // -------------------------------------------------------------- public int size () // number of items in queue {return nitems ;} // ---------------------------------------------------------------- // ============== ==================================================================/// Programming job 4.1 public void display () {system. out. print ("queue:"); If (nitems = 0) {system. out. println ("null. "); Return;} If (Rear> = front) {for (INT I = front; I <= rear; I ++) {system. out. print (quearray [I] + "") ;}} else {for (INT I = front; I <maxsize; I ++) {system. out. print (quearray [I] + "") ;}for (INT I = 0; I <= rear; I ++) {system. out. print (quearray [I] + "") ;}} system. out. println ();} // ================================================ ===============================} // end class queue ///////////// /////////////////////// /// // Public class queueapp {public static void main (string [] ARGs) {queue thequeue = new Queue (5); // queue holds 5 itemsthequeue. display (); thequeue. insert (10); // insert 4 itemsthequeue. insert (20); thequeue. insert (30); thequeue. insert (40); thequeue. remove (); // remove 3 itemsthequeue. remove (); // (10, 20, 30) thequeue. remove (); thequeue. insert (50); // insert 4 more itemsthequeue. Insert (60); // (wraps around) thequeue. insert (70); thequeue. insert (80 ); // ================================================ === thequeue. display (); // ================================================ === while (! Thequeue. isempty () // remove and display {// All itemslong n = thequeue. remove (); // (40, 50, 60, 70, 80) system. out. print (n); system. out. print ("");} system. out. println ("");} // end main ()} // end class queueapp /////////////////////////////////// /////////////////////////////

Package chap04; // ================================================ ======================================/// programming job 4.2 class duqueue {private int maxsize; private long [] quearray; private int front; private int rear; private int nitems; Public duqueue (INT size) {maxsize = size; quearray = new long [maxsize]; front = 0; Rear =-1; nitems = 0;} public void insertleft (long value) {If (front = 0) {front = maxsize ;} quearray [-- front] = Value; nitems ++;} public void insertright (long value) {If (Rear = maxsize-1) {rear =-1;} quearray [++ rear] = value; nitems ++;} public long removeleft () {long temp = quearray [Front ++]; If (front = maxsize) {front = 0;} nitems --; return temp;} public long removeright () {long temp = quearray [rear --]; If (Rear =-1) {rear = maxsize-1;} nitems --; return temp;} public long peekleft () {return quearray [Front];} Public long peekright () {return quearray [rear];} public Boolean isempty () {return nitems = 0;} public Boolean isfull () {return nitems = maxsize ;} public int size () {return nitems;} public void display () {system. out. print ("queue:"); If (nitems = 0) {system. out. println ("null. "); Return;} If (Rear> = front) {for (INT I = front; I <= rear; I ++) {system. out. print (quearray [I] + "") ;}} else {for (INT I = front; I <maxsize; I ++) {system. out. print (quearray [I] + "") ;}for (INT I = 0; I <= rear; I ++) {system. out. print (quearray [I] + "") ;}} system. out. println ();}} // ================================================ ================================== public class duqueueapp {public static void main (string [] ARGs) {duqueue thequeue = new duqueue (5); // queue holds 5 itemssystem. out. println ("is the team instance empty:" + thequeue. isempty (); system. out. println ("is the team instance full?" + thequeue. isfull (); system. out. println ("queue size:" + thequeue. size (); thequeue. display (); thequeue. insertright (10); // insert 4 itemsthequeue. insertright (20); thequeue. insertright (30); thequeue. insertright (40); system. out. println ("queue size:" + thequeue. size (); thequeue. display (); thequeue. removeleft (); // remove 3 itemsthequeue. removeleft (); // (10, 20, 30) thequeue. removeleft (); system. out. println ("queue size:" + thequeue. size (); thequeue. display (); thequeue. insertleft (50); // insert 4 more itemsthequeue. insertleft (60); // (wraps around) thequeue. insertleft (70); thequeue. insertleft (80); system. out. println ("is the team instance empty:" + thequeue. isempty (); system. out. println ("is the team instance full?" + thequeue. isfull (); system. out. println ("queue size:" + thequeue. size (); thequeue. display (); thequeue. removeright (); // remove 3 itemsthequeue. removeright (); // (10, 20, 30) thequeue. removeright (); system. out. println ("queue size:" + thequeue. size (); thequeue. display ();} // end main ()} // end class queueapp

Package chap04; // ================================================ ================================/// programming job 4.3 class stacky {private duqueue stackqueue; public stacky (INT size) {stackqueue = new duqueue (size);} public void push (long value) {stackqueue. insertright (value);} public long POP () {return stackqueue. removeright ();} public long PEEK () {return stackqueue. peekright ();} public Boolean isempty () {return stackqueue. isempty ();} Public Boolean isfull () {return stackqueue. isfull ();}} // ================================================ ============================= public class stackapp {public static void main (string [] ARGs) {stacky thestack = new stacky (5); // make new stacksystem. out. println ("Stack is empty:" + thestack. isempty (); system. out. println ("Stack is full:" + thestack. isfull (); thestack. push (20); // push items onto stackthestack. Push (40); thestack. push (60); thestack. push (80); thestack. push (90); system. out. println ("Stack is empty:" + thestack. isempty (); system. out. println ("Stack is full:" + thestack. isfull (); While (! Thestack. isempty () // until it's empty, {// delete item from stacklong value = thestack. pop (); system. out. print (value); // display itsystem. out. print ("");} // end whilesystem. out. println ("");} // end main ()} // end class stackapp

Package chap04; // priorityq. java // demonstrates priority queue // to run this program: c> JAVA priorityqapp //////////////////////////////////// /// // class priorityq {// array in sorted order, from Max at 0 to min at size-1private int maxsize; private long [] quearray; private int nitems; // ----------------------------------------------------------- public priorityq (INT s) // const Ructor {maxsize = s; quearray = new long [maxsize]; nitems = 0 ;} // ------------------------------------------------------------- // ================================================ ========================================/// programming job 4.4 Public void insert (long item) // insert Item {quearray [nitems ++] = item; // Insert at 0} // end insert () // ================================================ ==========================================/// program job 4.4 Public long remove () // Remove minimum item {int highpriority = 0; For (INT I = 1; I <nitems; I ++) {If (quearray [I] <quearray [highpriority]) {highpriority = I ;}} long temp = quearray [highpriority]; for (INT I = highpriority; I <nitems-1; I ++) {// move the back part of the array forward to quearray [I] = quearray [I + 1];} nitems --; return temp ;} // ================================================ ==========================================/// programming job 4.4 // The question is ambiguous // Method 1: if public is displayed in the insert order Void display () {system. out. print ("queue:"); For (INT I = 0; I <nitems; I ++) {system. out. print (quearray [I] + "");} system. out. println ();} // Method 2: If the public void display1 () {long [] temp = new long [nitems] is displayed in the priority order, // The temporary table system. arraycopy (quearray, 0, temp, 0, nitems); // copy to the temporary table int out, in; For (out = 1; out <nitems; out ++) {In = out; long T = temp [out]; while (in> 0 & T <temp [in-1]) {temp [in] = temp [in -1]; In --;} temp [in] = T;} system. out. print ("queue:"); For (INT I = 0; I <nitems; I ++) {system. out. print (temp [I] + "");} system. out. println ();} // ================================================ =================== // ----------------------------------------------------------------- public long peekmin () // peek at minimum item {return quearray [nitems-1];} // --------------------------------------------------------- -------- Public Boolean isempty () // true if queue is empty {return (nitems = 0);} // --------------------------------------------------------------- public Boolean isfull () // true if queue is full {return (nitems = maxsize );} // begin} // end class priorityq /////////////////////////////// /// // public class priorityqapp {Public static void main (string [] ARGs) {priorityq thepq = new priorityq (5); thepq. insert (30); thepq. insert (50); thepq. insert (10); thepq. insert (40); thepq. insert (20); thepq. display (); // thepq. display1 (); While (! Thepq. isempty () {long item = thepq. remove (); system. out. print (item + ""); // 10, 20, 30, 40, 50} // end whilesystem. out. println ("");} // end main () // begin} // end class priorityqapp /////////////////////////////// /////////////////////////////////

Package chap04; import Java. io. bufferedreader; import Java. io. ioexception; import Java. io. inputstreamreader; Class utility {public static string getstring () throws ioexception {// accept the input string inputstreamreader in = new inputstreamreader (system. in); bufferedreader BF = new bufferedreader (in); string S = BF. readline (); Return s ;}} // ================================================ ==========================================/// programming job 4.5 is not complete Public class supermarket {private queue [] queue = {null, new Queue (20), new Queue (20), new Queue (20 ), new Queue (20)}; // 4 Customer queues // simulate cashier public void simulate () throws ioexception {long id = 0; // customer ID Boolean flag = true; while (FLAG) {system. out. println ("select event:"); system. out. print ("0. A customer enters a queue. "); System. Out. Print (" 1. A customer leaves 1st team samples. "); System. Out. Print (" 2. A customer leaves 2nd team samples. "); System. Out. Print (" 3. A customer leaves 3rd team samples. "); System. Out. Print (" 4. A customer leaves 4th team samples. "); System. Out. println (" Q. indicates that the program exits! "); String S = utility. getstring (); If (S. length () = 0) {// enter the carriage return and continue;} Char CH = S. charat (0); Switch (CH) {Case '0': Id ++; insertqueue (ID); // The customer enters the queue displayqueue (); // display the queue break; case '1': removequeue (1); // The customer leaves the displayqueue (); // display the queue break; Case '2': removequeue (2); displayqueue (); break; Case '3': removequeue (3); displayqueue (); break; Case '4': removequeue (4); displayqueue (); break; Case 'q ': // exit the program flag = False; system. Out. println ("Byebye! "); Break; default: break; }}// Delete the customer private void removequeue (INT queueid) {If (queue [queueid]. size () = 0) {return;} Long id = queue [queueid]. remove (); system. out. println ("customer" + ID + "Quit queue" + queueid +! ");} // Insert the customer to the queue public void insertqueue (long ID) {int queueid = getminqueueid (); queue [queueid]. insert (ID); system. out. println ("customer" + ID + "Enter the" + queueid + "Team instance");} // obtain the minimum queue number private int getminqueueid () {int min = 1; for (INT I = 2; I <5; I ++) {If (queue [I]. size () <queue [Min]. size () {min = I ;}} return min ;}// print the four queues public void displayqueue () {for (INT I = 1; I <5; I ++) {system. out. print ("no." + I + ""); queue [I]. display ();} system. out. println ();} public static void main (string [] ARGs) throws ioexception {supermarket Sm = new supermarket (); SM. simulate ();}}

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.