1 Queues and Priority Queues Chapter 8. 2 Introduction to Queues A queue is a waiting line – seen in daily life –Real world examples – toll booths, bank,

Slides:



Advertisements
Similar presentations
Data Structures Through C
Advertisements

Chapter 6 Queues and Deques.
Stack & Queues COP 3502.
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.
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
 A queue is a waiting line…….  It’s in daily life:-  A line of persons waiting to check out at a supermarket.  A line of persons waiting.
Queues CS-212 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed in their.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Data Structure Dr. Mohamed Khafagy.
Queue Overview Queue ADT Basic operations of queue
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
CS 206 Introduction to Computer Science II 10 / 22 / 2008 Instructor: Michael Eckmann.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Unit : Overview of Queues.
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.
Stacked Deque Gordon College Stacked Decks - get it?
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test.
Queues Chapter 8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Chapter 16 Stack and Queues part2
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
CHP-4 QUEUE.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
Stacks And Queues Chapter 18.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
Lecture 7 : Intro. to STL (Standard Template Library)
Queues Chapter 5 Queue Definition A queue is an ordered collection of data items such that: –Items can be removed only at one end (the front of the queue)
© 2011 Pearson Addison-Wesley. All rights reserved 8 B-1 Chapter 8 (continued) Queues.
Exam1 Review Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Give Eg:? Queues. Introduction DEFINITION: A Queue is an ordered collection of element in which insertions are made at one end and deletions are made.
Introduction Of Queue. Introduction A queue is a non-primitive linear data structure. It is an homogeneous collection of elements in which new elements.
CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
1 Queues Chapter 4. 2 Objectives You will be able to Describe a queue as an ADT. Build a dynamic array based implementation of a queue ADT.
Lecture 10 b Stacks b Queues. 2 Stacks b A stack ADT is linear b Items are added and removed from only one end of a stack b It is therefore LIFO: Last-In,
Queues 1. Introduction A sequential collection of data Changes occur at both ends, not just one Queue applications –files waiting to be printed –"jobs"
 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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
Review Array Array Elements Accessing array elements
Data Structures Using C, 2e
Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009
Queues.
18 Chapter Stacks and Queues
Chapter 18: Stacks and Queues.
Queues.
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Stacks and Queues.
Queues Queues Queues.
Algorithms and Data Structures
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.
Chapter 19: Stacks and Queues.
Queues.
CSC 143 Queues [Chapter 7].
Lecture 8 : Intro. to STL (Standard Template Library)
Queues cont. Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
[Most of the details about queues are left for to read about and work out in Lab 6.] Def. As a data structure, a queue is an ordered collection of data.
Getting queues right … finally (?)
Presentation transcript:

1 Queues and Priority Queues Chapter 8

2 Introduction to Queues A queue is a waiting line – seen in daily life –Real world examples – toll booths, bank, food –Plenty of CS examples: Printer queue, server queues Event queue for programs (controls program interaction) Game programming queue (Networking Game Programming) Communication queues – between threads

3 Queue Basics A queue is a sequence of data elements In the sequence –Items can be removed only at the front –Items can be added only at the other end, the back Basic operations –Construct a queue –Check if empty –Enqueue (add element to back) –Front (retrieve value of element from front) –Dequeue (remove element from front)

4 Designing and Building a Queue Class Array-Based Consider an array in which to store a queue Note additional variables needed –myFront, myBack Picture a queue object like this

5 Problems –We quickly "walk off the end" of the array Possible solutions –Shift array elements –Use a circular queue –Note that both empty and full queue gives myBack == myFront Designing and Building a Queue Class Array-Based

6 Using a static array –QUEUE_CAPACITY specified –Enqueue increments myBack using mod operator, checks for full queue –Dequeue increments myFront using mod operator, checks for empty queue Designing and Building a Queue Class Array-Based

7 Using Dynamic Array to Store Queue Elements Similar problems as with list and stack –Fixed size array can be specified too large or too small Dynamic array design allows sizing of array for multiple situations Results in structure as shown –myCapacity determined at run time

8 Linked Queues Even with dynamic allocation of queue size –Difficult to adjust during run of program Could use linked list to store queue elements –Can grow and shrink to fit the situation –No need for upper bound ( myCapacity )

9 Linked Queues Constructor initializes myFront, myBack Front –return myFront->data Dequeue –Delete first node (watch for empty queue) Enqueue –Insert node at end of list

10 Application of Queues: Buffers and Scheduling Important use of queues is I/O scheduling –Use buffers in memory to improve program execution –Buffer arranged in FIFO structure

11 Application of Queues: Buffers and Scheduling Also times when insertions, deletions must be made from both ends –Consider a scrolling window on the screen This requires a double ended queue –Called a deque (pronounced "deck") –Could also be considered a double ended stack (or "dack")

12 Application of Queues: Buffers and Scheduling Consider a keyboard buffer –Acts as a queue –But elements may be removed from the back of the queue with backspace key A printer spool is a queue of print jobs

13 Application of Queues: Buffers and Scheduling Queues used to schedule tasks within an operating system Job moves from disk to ready queue

14 Application of Queues: Buffers and Scheduling Ready queue may actually be a priority queue … job may get to "cut the line" based on its priority

15 Deque “double-ended queue” Push and pop from either side Deques have the following properties: –Individual elements can be accessed by their position index. –Iteration over the elements can be performed in any order. –Elements can be efficiently added and removed from any of its ends (either the beginning or the end of the sequence).

16 Deque What methods are available? Iterators: beginReturn iterator to beginning endReturn iterator to end rbeginReturn reverse iterator to reverse beginning rendReturn reverse iterator to reverse end Capacity: sizeReturn size max_sizeReturn maximum size resizeChange size (public member functions) emptyTest whether container is empty Element access: operator[]Access element atAccess element frontAccess first element backAccess last element Modifiers: assignAssign container content push_backAdd element at the end push_frontInsert element at beginning pop_backDelete last element pop_frontDelete first element insertInsert elements eraseErase elements swapSwap content clearClear content

17 Deque #include using namespace std; int main () { unsigned int i; deque first; deque second (4,100); deque third (second.begin(),second.end()); deque fourth (third); for (deque ::iterator it = third.begin();it!=third.end();it++) cout << *it << " "; cout << endl; int myints[] = {16,2,77,29}; deque fifth (myints, myints + sizeof(myints) / sizeof(int) ); cout << "The contents of fifth are:"; for (i=0; i < fifth.size(); i++) cout << " " << fifth[i]; cout << endl; return 0; }

18 Deque How does it work internally? Divided in several chunks of storage Deque class keeps all this information and provides a uniform access to the elements. Deques are a little more complex internally.

19

20

21

22

23

24

25

26

27

28