Queues CSI 1101 N. El Kadri. 2 Definitions A queue is a linear abstract data type such that insertions are made at one end, called the rear, and removals.

Slides:



Advertisements
Similar presentations
Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Advertisements

Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Queues A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
Topic 9 The Queue ADT.
1 Queues – Chapter 3 A queue is a data structure in which all additions are made at one end called the rear of the queue and all deletions are made from.
5/4/20151 Queues Implementations. 5/4/20152 Outline Queues Basic operations Examples of useImplementations Array-based and linked list-based.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Data Structure Dr. Mohamed Khafagy.
Queue Overview Queue ADT Basic operations of queue
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 4.
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
Queues Chapter 6. Chapter Objectives  To learn how to represent a waiting line (queue) and how to use the methods in the Queue interface for insertion.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
© 2006 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 Queues CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
1 Queues CPS212 Gordon College. 2 Introduction to Queues A queue is a waiting line – seen in daily life –Real world examples – toll booths, bank, food.
Cmpt-225 Queues. A queue is a data structure that only allows items to be inserted at the end and removed from the front Queues are FIFO (First In First.
Queues Chapter 6. Chapter 6: Queues2 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in the Queue interface.
Chapter 7 Queues. © 2005 Pearson Addison-Wesley. All rights reserved7-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
Queues Chapter 8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
Stacks. week 2a2 Outline and Reading The Stack ADT (§4.1) Applications of Stacks Array-based implementation (§4.1.2) Growable array-based stack Think.
CS 1031 Queues Definition of a Queue Examples of Queues Design of a Queue Class Different Implementations of the Queue Class.
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues (slightly modified by Dan Fleck)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)
Queues Chapter 6. Chapter 6: Queues Chapter Objectives To learn how to represent a waiting line (queue) and how to use the five methods in the Queue interface:
A queue is a linear, homogeneous, container that stores and dispenses its content in a FIFO manner. FIFO - Fast In First Out The first (most distant) item.
Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
The Abstract Data Type Queue A queue New items enter at the back, or rear, of the queue Items leave from the front of the queue First-in, first-out (FIFO)
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
Chapter 8 Queues. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
M180: Data Structures & Algorithms in Java Queues Arab Open University 1.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Queue. The Queue ADT Insertions and deletions follow the first-in first-out scheme Insertions are at the rear of the queue and removals are at the front.
Queues 1. Queue  a queue represents a sequence of elements where elements can be added at the back of the sequence and removed from the front of the.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Queues CS 367 – Introduction to Data Structures. Queue A queue is a data structure that stores data in such a way that the last piece of data stored,
Chapter 7 A Queues. © 2004 Pearson Addison-Wesley. All rights reserved7 A-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear,
1 Data Structures CSCI 132, Spring 2014 Lecture 7 Queues.
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
1 Data Structures and Algorithms Queue. 2 The Queue ADT Introduction to the Queue data structure Designing a Queue class using dynamic arrays Linked Queues.
Queues By Jimmy M. Lu. Overview Definition Standard Java Queue Operations Implementation Queue at Work References.
Queues1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich,
The Queue ADT.
Queues.
Queues Implementations 6/17/2018.
CSE 373: Data Structures and Algorithms
Stacks and Queues.
Queues Chapter 8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
CMSC 341 Lecture 5 Stacks, Queues
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queues 11/16/2018 4:18 AM Queues 11/16/2018 4:18 AM Queues.
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Queues.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Queues 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
ساختمان داده ها پشته ها Give qualifications of instructors: DAP
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Queues Definition of a Queue Examples of Queues
Presentation transcript:

Queues CSI 1101 N. El Kadri

2 Definitions A queue is a linear abstract data type such that insertions are made at one end, called the rear, and removals are made at the other end, called the front. Queues are sometimes called FIFOs: first-in first-out. enqueue()  Queue  dequeue() The two basic operations are: enqueue: adds an element to the rear of the queue. dequeue: removes and returns the element at the front of the queue.  Software queues are similar to physical ones: queuing at the supermarket, at the bank, at cinemas, etc.

3 Applications Shared resources management (system programming): –Access to the processor; – Access to the peripherals such as disks and printers. Application programs: –Simulations; –Generating sequences of increasing length over a finite size alphabet; –Navigating through a maze.

4 Example class Simple { public static void main( String[] args ) { Queue queue = new QueueImplementation(); for ( int i=0; i<10; i++ ) queue.enqueue( new Integer( i ) ); while ( ! queue.isEmpty() ) System.out.println( queue.dequeue() ); }  What does it print?

5 q = new QueueImpl() q  [] q.enqueue(a) q  [a] q.enqueue(b) q  [a,b] q.enqueue(c) q  [a,b,c] q.enqueue(d) q  [a,b,c,d] q.dequeue()  a q  [b,c,d] q.dequeue()  b q  [c,d] q.enqueue(e) q  [c,d,e] q.dequeue()  c q  [d,e] q.dequeue()  d q  [e]  Elements of a queue are processed in the same order as the they are inserted into the queue, here “a” was the first element to join the queue and it was the first to leave the queue: first-come first-serve.

6 Implementations Just like stacks, there are two families of implementations. – Array-based; – Linked-structure.

7 Today ’ s Topics Queue –Implementation of Queue –Usage of Queue

8 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, … ABCD Output Input

9 More Examples of Queue In our daily life –Airport Security Check –Cinema Ticket Office –Bank, ATM –Anything else ?

10 What Is Queue Queue is an abstract data type Adding an entry at the rear Deleting an entry at the front front rear Adding Deleting ABC

11 Abstract Data Types Queue –Operating on both ends –Operations: EnQueue(in), DeQueue(out) front rear enqueue dequeue ABC

12 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.

13 Applications of Queue Printing Job Management Packet Forwarding in Routers Message queue in Windows I/O buffer

14 Printing Job Management Many users send their printing jobs to a 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

15 Printing Queue Now printing A.doc A.doc is finished. Now printing B.doc Now still printing B.doc D.doc comes A.doc B.doc C.doc arrive to printer. ABC BC BCD CD D B.doc is finished. Now printing C.doc C.doc is finished. Now printing D.doc

16 First-in First-out (FIFO) When we enqueue entries in the queue and then dequeue them one by one, we will get the items in the same order. The first one enqueued is the first one dequeued. (FIFO) ABC A AB BC C A, B, C come in A, B, C come out

17 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?

18 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

19 Array Implementation of Queue BADC Max_Size rear front After A leaves, BDC Max_Size rear front 10n

20 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

21 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? YX …… 10n-132 rear front

22 Two Solutions Shifting all items to front in the array when dequeue operation. ( Too Costly… ) Wrapped around array ---- Circular Array BA …… C 10n-132 rear=3 front=0 CB …… 10n-132 rear=2 front=0 A leaves

23 Circular Array Wrapped around array BA …… C 10n-132 rear=3 front=0 A B C n-1 1 rear=3 front=0

24 EnQueue & DeQueue In Circular Array EnQueue –rear = (rear + 1) MOD n DeQueue –front = (front + 1) MOD n A B C n-1 1 rear=3 B C n-1 1 front=1

25 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.

26 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; }

27 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; }

28 ArrayQueuePT --- Size() How many items currently in the queue? public int size() { if( rear >= front ) return rear – front; else return queue.length + rear – front; }

29 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; }

30 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; }

31 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; }

32 ArrayQueuePT -- peek() public Object peek() { if (isEmpty()) throw new IllegalStateException (“Queue is empty"); return queue[front]; }

33 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

34 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 { … }

35 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(); }

36 Implementation of ArrayQueuePT Class Example: QueuePT.java, ArrayQueuePT.javaQueuePT.javaArrayQueuePT.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

37 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);

38 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( “ Hamad ”, “ 5456 ” ) ) –Employee emp = (Employee) registry.dequeue();

39 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.javaPrintingJobQueue.java

40 New Printing Jobs come public void printstatus() { QueuePT que = new ArrayQueuePT( ); //Create a new queue 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" );

41 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"); }

42 Customer Service In Royal Bank Suppose there is only one customer service available in Royal Bank on 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 customer –How many customers are in the line? –Who is the current serving customer?

43 Customer Service Queue Complete example: BankServiceQueue.java 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

44 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; }

45 New Customer Comes // 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; }

46 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" ); }

47 Priority Queue --- Air Travel Only one check-in service in Air Canada 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.

48 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

49 Two Queues High Priority Queue, will come in hpQue Low Priority Queue, will come in lpQue DCH Check In GFEBA High Priority Queue Low Priority Queue Customers coming in

50 Pseudocode For Arrival Passengers Arrival: if( new Passenger comes ) { if( is First Class) hpQue.enqueue( new Passenger ); else lpQue.enqueue( new Passenger ); }

51 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(); }