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.

Slides:



Advertisements
Similar presentations
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Advertisements

Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
Queues.
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.
Fundamentals of Python: From First Programs Through Data Structures
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.
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.
Simulation. Example: A Bank Simulator We are given: –The number of tellers –The arrival time of each customer –The amount of time each customer requires.
Queues and Priority Queues
CS Data Structures I Chapter 7 Queues II. 2 Topics Queue Application Simulation Comparison of List, Stack and Queue.
COSC2006 Chapter 8 Queues III
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.
Circular Arrays Neat trick: use a circular array to insert and remove items from a queue in constant time The idea of a circular array is that the end.
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.
Queues.
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.
Stack: Linked List Implementation Push and pop at the head of the list New nodes should be inserted at the front of the list, so that they become the top.
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.
Cmpt-225 Simulation. Application: Simulation Simulation  A technique for modeling the behavior of both natural and human-made systems  Goal Generate.
EXPANDING STACKS AND QUEUES CS16: Introduction to Data Structures & Algorithms 1 Tuesday, February 10, 2015.
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
Queues and Priority Queues
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues (slightly modified by Dan Fleck)
TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues.
Data Structures Using C++ 2E Chapter 8 Queues. Data Structures Using C++ 2E2 Objectives Learn about queues Examine various queue operations Learn how.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Data Structures Using Java1 Chapter 7 Queues. Data Structures Using Java2 Chapter Objectives Learn about queues Examine various queue operations Learn.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
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)
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
© 2011 Pearson Addison-Wesley. All rights reserved 8 B-1 Chapter 8 (continued) Queues.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Queue. Avoid confusion Britain Italy 6 Applications of Queues Direct applications –Waiting lists, bureaucracy –Access to shared resources (e.g.,
M180: Data Structures & Algorithms in Java Queues Arab Open University 1.
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.
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,
Queue Section 3 6/12/ The Abstract Data Type Queue A queue is a list from which items are deleted from one end (front) and into which items are.
Queue ADT for lining up politely. COSC 2006 queue2 Queue – simple collection class  first-in first-out (FIFO) structure insert new elements at one end.
Queues and Priority Queue Implementations Chapter 14 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
Queues1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich,
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Queues Chapter 8 (continued)
Chapter 7 Queues.
Data Abstraction & Problem Solving with C++
ADT description Implementations
CC 215 Data Structures Queue ADT
Csc 2720 Data structure Instructor: Zhuojun Duan
CENG 213 Data Structure Queue 7/2/2018.
Queues Queues Queues.
Queues.
Queues Chapter 8 (continued)
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,
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Figure 7.1 Some queue operations. Figure 7.1 Some queue operations.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Chapter 8 Queues © 2006 Pearson Addison-Wesley. All rights reserved.
Queues Jyh-Shing Roger Jang (張智星)
Queues cont. Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Queues Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Queues.
Presentation transcript:

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 Out) data structures – “fair” data structures

What Can You Use a Queue For? Processing inputs and outputs to screen (console) Server requests  Instant messaging servers queue up incoming messages  Database requests Print queues  One printer for dozens of computers Operating systems use queues to schedule CPU jobs Simulations

Queue Operations A queue should implement (at least) these operations:  enqueue – insert an item at the back of the queue  dequeue – remove an item from the front  peek – return the item at the front of the queue without removing it Like stacks it is assumed that these operations will be implemented efficiently  That is, in constant time

Queue: Array Implementation First consider using an array as the underlying structure for a queue, one plan would be to  Make the back of the queue the current size of the queue (i.e., the number of elements stored)  Make the front of the queue index 0  Inserting an item can be performed in constant time  But removing an item would require shifting all elements in the queue to the left which is too slow! Therefore we need to find another way

An Array-Based Implementation Figure 8-8 a) A naive array-based implementation of a queue; b) rightward drift can cause the queue to appear full

Circular Arrays Neat trick: use a circular array to insert and remove items from a queue in constant time The idea of a circular array is that the end of the array “wraps around” to the start of the array

The mod Operator The mod operator (%) is used to calculate remainders:  1%5 = 1, 2%5 = 2, 5%5 = 0, 8%5 = 3 mod can be used to calculate the front and back positions in a circular array, therefore avoiding comparisons to the array size  The back of the queue is: (front + count - 1) % items.length where count is the number of items currently in the queue  After removing an item the front of the queue is: (front + 1) % items.length;

Array Queue Example //Java Code Queue q = new Queue(); q.enqueue(6); 6 front =0 insert item at (front + count) % items.length count =01

Array Queue Example //Java Code Queue q = new Queue(); q.enqueue(6); q.enqueue(4); q.enqueue(7); q.enqueue(3); q.enqueue(8); 6 front = count =12345

Array Queue Example //Java Code Queue q = new Queue(); q.enqueue(6); q.enqueue(4); q.enqueue(7); q.enqueue(3); q.enqueue(8); q.dequeue();//front = 1 q.dequeue();//front = 2 q.enqueue(9); 6 front =0 4 make front = (0 + 1) % 6 = count =5434 make front = (1 + 1) % 6 = 2 2

Array Queue Example //Java Code Queue q = new Queue(); q.enqueue(6); q.enqueue(4); q.enqueue(7); q.enqueue(3); q.enqueue(8); q.dequeue();//front = 1 q.dequeue();//front = 2 q.enqueue(9); q.enqueue(5); front = insert at (front + count) % 6 = (2 + 4) % 6 = 0 count =45

List Queue Example front 6 back //Java Code Queue q = new Queue(); q.enqueue(6);

List Queue Example front 46 back //Java Code Queue q = new Queue(); q.enqueue(6); q.enqueue(4);

List Queue Example front 46 back //Java Code Queue q = new Queue(); q.enqueue(6); q.enqueue(4); q.enqueue(7); 7

List Queue Example front 46 back //Java Code Queue q = new Queue(); q.enqueue(6); q.enqueue(4); q.enqueue(7); q.enqueue(3); 73

List Queue Example front 46 back //Java Code Queue q = new Queue(); q.enqueue(6); q.enqueue(4); q.enqueue(7); q.enqueue(3); q.dequeue(); 73

Queue: Circular Linked List Implementation Possible implementations of a queue  A circular linked list with one external reference A reference to the back Figure 8-4b A reference-based implementation of a queue: b) a circular linear linked list with one external reference

Queue: Circular Linked List Implementation Figure 8-5 Inserting an item into a nonempty queue

Queue: Circular Linked List Implementation Figure 8-6 Inserting an item into an empty queue: a) before insertion; b) after insertion

Queue: Circular Linked List Implementation Figure 8-7 Deleting an item from a queue of more than one item

Queue: ADT List Implementation public void enqueue(Object newItem) { list.add(list.size()+1, newItem); } // end enqueue public Object dequeue() { Object temp = list.get(1); list.remove(1); return temp; } // end dequeue

Queue: ADT List Implementation Efficiency depends on implementation of ADT List – in most common implementations, at least one of operations enqueue() and dequeue() is not efficient On other hand: it was very fast to implement (code is easy, unlikely that errors were introduced when coding).

Application: Simulation Simulation  A technique for modeling the behavior of both natural and human-made systems  Goal Generate statistics that summarize the performance of an existing system Predict the performance of a proposed system  Example A simulation of the behavior of a bank

Application: Simulation Figure 8-14a and 8-14b A blank line at at time a) 0; b) 12

Application: Simulation Figure 8-14c and 8-14d A blank line at at time c) 20; d) 38

Application: Simulation An event-driven simulation  Simulated time is advanced to the time of the next event  Events are generated by a mathematical model that is based on statistics and probability A time-driven simulation  Simulated time is advanced by a single time unit  The time of an event, such as an arrival or departure, is determined randomly and compared with a simulated clock

Application: Simulation The bank simulation is concerned with  Arrival events Indicate the arrival at the bank of a new customer External events: the input file specifies the times at which the arrival events occur  Departure events Indicate the departure from the bank of a customer who has completed a transaction Internal events: the simulation determines the times at which the departure events occur

Input file Arrival transaction length

while (events remain to be processed){ currentTime=time of the next event; if(event is an arraival event) process the arrival event else process the departure event }

Application: Simulation An event list is needed to implement an event-driven simulation  An event list Keeps track of arrival and departure events that will occur but have not occurred yet Contains at most one arrival event and one departure event Figure 8-15 A typical instance of the event list

Create an empty bankQueue Create an empty eventList Get the first arrival event from the input file. Place the arrival event to the eventList. while (eventList is not empty){ newEvent = the first element in the eventList; if(newEvent is an arraival event) processArrival(newEvent); else processDeparture(newEvent); }

process arrival (Event arrivalEvent){ boolean atfront=bankQueue.isEmpty(); bankQueue.enqueue(arraivalEvent); Delete arrivalEvent From the event list; if(atFront){ Insert the departure event into the event list. time of departure=currentTime+transactionLength; } if(not at the end of input file) Get the next arrival and add to the eventList. }

process Departure (Event departureEvent){ bankQueue.dequeue(); Delete departureEvent From the event list; if(!bankQueue.isEmpty){ Insert into the event list a departure event. time of departure=currentTime+transactionLength of the first customer in the queue; }

Summary The definition of the queue operations gives the ADT queue first-in, first-out (FIFO) behavior A reference-based implementation of a queue uses either  A circular linked list  A linear linked list with a head reference and a tail reference An array-based implementation of a queue is prone to rightward drift  A circular array eliminates the problem of rightward drift

Summary To distinguish between the queue-full and queue- empty conditions in a queue implementation that uses a circular array, you can  Count the number of items in the queue  Use a full flag  Leave one array location empty Models of real-world systems often use queues  The event-driven simulation in this chapter uses a queue to model a line of customers in a bank

Summary Simulations  Central to a simulation is the notion of simulated time In a time-driven simulation  Simulated time is advanced by a single time unit In an event-driven simulation  Simulated time is advanced to the time of the next event  To implement an event-driven simulation, you maintain an event list that contains events that have not yet occurred