1 / 46

ECE 242 Spring 2003 Data Structures in Java

ECE 242 Spring 2003 Data Structures in Java. http://rio.ecs.umass.edu/ece242 Queue Prof. Lixin Gao. Today’s Topics. Queue Implementation of Queue Usage of Queue. D. C. B. A. Queue. Queue: First In First Out (FIFO) Toll Station Car comes, pays, leaves Check-out in Big Y market

Audrey
Download Presentation

ECE 242 Spring 2003 Data Structures in Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ECE 242 Spring 2003Data Structures in Java http://rio.ecs.umass.edu/ece242 Queue Prof. Lixin Gao Data Structures in Java, Prof. Gao

  2. Today’s Topics • Queue • Implementation of Queue • Usage of Queue Data Structures in Java, Prof. Gao

  3. D C B A Queue • Queue: First In First Out (FIFO) • Toll Station • Car comes, pays, leaves • Check-out in Big Y market • Customer comes, checks out and leaves • More examples: Printer, Office Hours, … Output Input Data Structures in Java, Prof. Gao

  4. More Examples of Queue • In our daily life • Airport Security Check • Cinema Ticket Office • Bank, ATM • Anything else ? Data Structures in Java, Prof. Gao

  5. What Is Queue • Queue is an abstract data type • Adding an entry at the rear • Deleting an entry at the front Adding Deleting C B A front rear Data Structures in Java, Prof. Gao

  6. Abstract Data Types • Queue • Operating on both ends • Operations: EnQueue(in), DeQueue(out) enqueue dequeue C B A front rear Data Structures in Java, Prof. Gao

  7. Queue Queue is FIFO ( First-In First-Out) A queue is open at two ends. You can only add entry (enqueue) at the rear , and delete entry (dequeue) at the front. Note that you cannot add/extract entry in the middle of the queue. Data Structures in Java, Prof. Gao

  8. Applications of Queue • Printing Job Management • Packet Forwarding in Routers • Message queue in Windows • I/O buffer Data Structures in Java, Prof. Gao

  9. Printing Job Management • Many users send their printing jobs to ECS public printer • Printer will put them into a queue according to the arrival time and print the jobs one by one • These printing documents are A.doc, B.doc, C.doc and D.doc Data Structures in Java, Prof. Gao

  10. Printing Queue • A.doc B.doc C.doc arrive to printer. Now printing A.doc C B A C B A.doc is finished. Now printing B.doc D.doc comes D C B Now still printing B.doc D C B.doc is finished. Now printing C.doc D C.doc is finished. Now printing D.doc Data Structures in Java, Prof. Gao

  11. First-in First-out (FIFO) A, B, C come in The first one enqueued is the first one dequeued. (FIFO) C B A C When we enqueue entries in the queue and then dequeue them one by one, we will get the items in the same order. B A A, B, C come out C B A Data Structures in Java, Prof. Gao

  12. Question • Queue is an abstract data structure • Item can be Integer, Double, String, Employee, Faculty… • How to implement a general queue for all those types? Data Structures in Java, Prof. Gao

  13. Abstract Data Type • Same as Stack, we use Object data type instead of int or double or String or other data type • Use an array in queue, which stores all items come in. • Object Queue[ ]; • Other implementations are possible Data Structures in Java, Prof. Gao

  14. Array Implementation of Queue n-1 3 2 1 0 D C B A rear front Max_Size After A leaves, n-1 3 2 1 0 D C B rear front Max_Size Data Structures in Java, Prof. Gao

  15. Operations • enqueue • add a new item at the rear • dequeue • remove a item from the front • isEmpty • check whether the queue is empty or not • isFull • check whether the queue is full or not • size • return the number of items in the queue • peek • return the front item Data Structures in Java, Prof. Gao

  16. Problem • An array has limited size, once rear is at the end of this array, and there is new item coming in, what can we do? n-1 3 2 1 0 Y X … … front rear Data Structures in Java, Prof. Gao

  17. Two Solutions • Shifting all items to front in the array when dequeue operation. ( Too Costly… ) • Wrapped around array ---- Circular Array A leaves n-1 3 2 1 0 n-1 3 2 1 0 … … C B A … … C B rear=3 front=0 rear=2 front=0 Data Structures in Java, Prof. Gao

  18. Circular Array rear=3 • Wrapped around array 3 2 C front=0 1 n-1 3 2 1 0 B … … C B A 0 A n-1 rear=3 front=0 Data Structures in Java, Prof. Gao

  19. EnQueue & DeQueue In Circular Array • DeQueue • front = (front + 1) MOD n • EnQueue • rear = (rear + 1) MOD n rear=3 front=1 3 3 2 2 C C 1 1 B B 0 A 0 n-1 n-1 Data Structures in Java, Prof. Gao

  20. Empty/Full In Circular Array • When rear equals front, Queue is empty • When (rear + 1) MOD n equals front, Queue is full • Circular array with capacity n at most can hold n-1 items. Data Structures in Java, Prof. Gao

  21. Implementation for Queue public class ArrayQueuePT { private final static int DEFAULT_CAPACITY = 100; // suppose the default capacity for this queue is 100. private Object queue[ ]; // The array that holds the items private int rear, front; // Index of rear, front item in the queue; } Data Structures in Java, Prof. Gao

  22. ArrayQueuePT Constructor // Creates a queue with the default capacity public ArrayQueuePT () { this(DEFAULT_CAPACITY); } // Creates a queue with a user-specified capacity public ArrayQueuePT (int capacity) { if (capacity < 2) throw new IllegalArgumentException ("Capacity must be > 1"); queue = new Object[capacity]; rear = front = 0; } Data Structures in Java, Prof. Gao

  23. ArrayQueuePT --- Size() • How many items currently in the queue? public int size() { if( rear >= front ) return rear – front; else return queue.length + rear – front; } Data Structures in Java, Prof. Gao

  24. ArrayQueuePT --- isEmpty/isFull // check whether the queue is empty public boolean isEmpty() { return rear == front; } // check whether the queue is full public boolean isFull() { return size() == queue.length-1; } Data Structures in Java, Prof. Gao

  25. ArrayQueuePT --- enqueue() public void enqueue(Object item) { if (item == null) throw new IllegalArgumentException ("Item is null"); if (isFull()) throw new IllegalStateException (“Queue is full"); queue[rear] = item; rear = (rear + 1)%queue.length; } Data Structures in Java, Prof. Gao

  26. ArrayQueuePT --- dequeue() public Object pop( ) { if (isEmpty()) throw new IllegalStateException (“Queue is empty"); Object frontItem = queue[front]; queue[front] = null; front = (front + 1)%queue.length; return frontItem; } Data Structures in Java, Prof. Gao

  27. ArrayQueuePT peek() Method public Object peek() { if (isEmpty()) throw new IllegalStateException (“Queue is empty"); return queue[front]; } Data Structures in Java, Prof. Gao

  28. Interface • Users don’t need to know how Queue is implemented. • User only need to know how they can operate the Queue. • There are many ways to implement Queue, but all of them have the same interfaces • enqueue, dequeue, peek, isFull, isEmpty Data Structures in Java, Prof. Gao

  29. Interface and Implementation Class • Interface contains a skeleton for public operations • No real code for each method • Like function prototype • A class implements interface • Implements every method • Statement: public class myclass implements interface { … … } Data Structures in Java, Prof. Gao

  30. Interface for Queue public interface QueuePT { public boolean isEmpty(); public boolean isFull(); public Object peek(); public Object dequeue(); public void enqueue(Object item); public int size(); } Data Structures in Java, Prof. Gao

  31. Implementation of ArrayQueuePT Class • Example: QueuePT.java, ArrayQueuePT.java public class ArrayQueuePT implements QueuePT { private final static int DEFAULT_CAPACITY = 100; private Object queue[];// The array holds the queue private int rear, front; // Index of rear, front items in queue Data Structures in Java, Prof. Gao

  32. Create an object of ArrayQueuePT // create a queue with default capacity QueuePT myqueue = new ArrayQueuePT(); // create a queue with 1024 elements QueuePT registry = new ArrayQueuePT(1024); Data Structures in Java, Prof. Gao

  33. Using ArrayQueuePT • enqueue and dequeue characters in Queue myque • myque.enqueue( new Character(‘a’) ) • char ch = (Character)myque.dequeue()).charValue() • enqueue and dequeue Employee in Queue registry • registry.enqueue( new Employee(“Lixin”, “5456”) ) • Employee emp = (Employee) registry.dequeue(); Data Structures in Java, Prof. Gao

  34. Printing Job Management • 4 documents are ready to print • Print the printer status • new document comes • which document is finished • Using ArrayQueuePT • Complete Example: • PrintingJobQueue.java Data Structures in Java, Prof. Gao

  35. New Printing Jobs come public void printstatus() { QueuePT que = new ArrayQueuePT( ); //Create a newqueue System.out.println("Printing job: Null "); //print the printer status // new printing jobs come, a.doc, b.doc, c.doc, d.doc System.out.println("New printing job: a.doc"); que.enqueue( "a.doc" ); System.out.println("New printing job: b.doc"); que.enqueue( "b.doc" ); System.out.println("New printing job: c.doc"); que.enqueue( "c.doc" ); System.out.println("New printing job: d.doc"); que.enqueue( "d.doc" ); Data Structures in Java, Prof. Gao

  36. Finishing Printing Jobs // print the printer status System.out.println("\nPrinting job: there are " + que.size() + " jobs in the queue"); System.out.println(" " + que.dequeue() + " is Finished"); System.out.println(" " + que.dequeue() + " is Finished"); System.out.println(" " + que.dequeue() + " is Finished"); System.out.println(" " + que.dequeue() + " is Finished"); } Data Structures in Java, Prof. Gao

  37. Customer Service In Fleet Bank • Suppose there is only one customer service available in Fleet Bank in Saturday morning • In every 3 minutes, a new customer arrives at the end of waiting line • Each customer will need 5 minutes for the service • Print out the information after the first 30 minutes • The time of arriving and leaving for each customers • How many customers are in the line? • Who is the current serving customer? Data Structures in Java, Prof. Gao

  38. Customer Service Queue • Complete example: BankServiceQueue.java public void run( ) { // Create a new queue QueuePT que = new ArrayQueuePT(100); int time = 0;// in minutes int incustomer = 0;// secquence of customers int servicetime = 0;// service times Data Structures in Java, Prof. Gao

  39. Customer In Service // what's going on in 30 minutes while ( time <= 30 ) { // if queue is not empty, one customer service is working if( que.size()!=0 ) { servicetime = servicetime + 1; // customer leaves when finishing the service, the service time is 5 minutes if( servicetime == 5 ) { String name = (String)que.dequeue(); System.out.println("<< " + name + " leaves at time = " + time); // start to service time for next customer servicetime = 0; } } Data Structures in Java, Prof. Gao

  40. New Customer Comes // in every 3 minutes, there is a new customer coming. if( time%3==0 ) { incustomer = incustomer + 1; String name = "CUSTOMER " + incustomer; que.enqueue( name ); System.out.println(">> " + name + " arrives at time = " + time); } time = time + 1; } Data Structures in Java, Prof. Gao

  41. Print Status After 30 Minutes // print the status after 30 minutes System.out.println("\n==========================" ); if( que.size()!=0 ) { System.out.println("There are " + que.size() + " customers in the line" ); System.out.println("The current serving customer is " + que.peek() ); } else { System.out.println("There are no customers in the line" ); } } Data Structures in Java, Prof. Gao

  42. Priority Queue --- Air Travel • Only one check-in service in United Airline at airport • Two waiting lines for passengers • one is First class service • the other is Economy class service • Passengers in the first-class waiting line have higher priority to check in than those in the economy-class waiting line. Data Structures in Java, Prof. Gao

  43. Priority Queue • Two queues • one is high priority queue • the other is low priority queue • Service rules: • First serve the people in high priority queue • If no passengers are in high priority queue, serve the passengers in low priority queue Data Structures in Java, Prof. Gao

  44. Two Queues • High Priority Queue, will come in hpQue • Low Priority Queue, will come in lpQue High Priority Queue Check In Customers coming in H D C G F E B A Low Priority Queue Data Structures in Java, Prof. Gao

  45. Pseudocode For Arrival Passengers Arrival: if( new Passenger comes ) { if( is First Class) hpQue.enqueue( new Passenger ); else lpQue.enqueue( new Passenger ); } Data Structures in Java, Prof. Gao

  46. Pseudocode For Service Check-In Service: if( hpQue is not empty ) { serve the passenger from high priority queue, hpQue.dequeue(); } else { serve the passenger from low priority queue, lpQue.dequeue(); } Data Structures in Java, Prof. Gao

More Related