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)

Slides:



Advertisements
Similar presentations
Stack & Queues COP 3502.
Advertisements

Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
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.
What is a Queue? A queue is a FIFO “first in, first out” structure.
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.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
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.
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,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
Queues Chapter 8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Queue, Deque, and Priority Queue Implementations Chapter 11 Copyright ©2012 by 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.
Stacks, Queues, and Deques
Chapter 16 Stacks and Queues Saurav Karmakar Spring 2007.
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
Chapter 16 Stack and Queues part2
COMP 121 Week 14: Queues. Objectives Learn how to represent a queue Learn how to use the methods in the Queue interface Understand how to implement the.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
CS 1031 Queues Definition of a Queue Examples of Queues Design of a Queue Class Different Implementations of the Queue Class.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
1 5. Queues Def. As a data structure, a queue is an ordered collection of data items with the property that items can be removed only at one end, called.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
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.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
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.
CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
CHAPTER 5 QUEUE v2 by Queue in C++ [Non-English] It is an ordered group of homogeneous items of elements. [English] –You go to the shopping.
Queues Another Linear ADT Copyright © 2009 Curt Hill.
Queues 1. Introduction A sequential collection of data Changes occur at both ends, not just one Queue applications –files waiting to be printed –"jobs"
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue.
Review Array Array Elements Accessing array elements
CSCE 210 Data Structures and Algorithms
Data Structures Using C, 2e
Queues.
18 Chapter Stacks and Queues
CS505 Data Structures and Algorithms
Chapter 18: Stacks and Queues.
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
Queues.
Homework 4 questions???.
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Stack and Queue APURBO DATTA.
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.
CSC 143 Queues [Chapter 7].
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Queues Jyh-Shing Roger Jang (張智星)
Stacks, Queues, and Deques
[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:

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) –Items can be added only at the other end (the back of the queue)

Basic Operations construct a queue (usually empty) emptyCheck if queue is empty addQAdd an item to back of queue frontRetrieve element at front of queue RemoveQRemove element at front of queue

Queue FAQs A queue is a FIFO structure First-In First-Out Examples of queues –I/O buffers (stream of bytes from keyboard) –Scheduling queues in a multiuser OS –Printer queue for (;;) { while (printerQueue.empty()) sleep 1; printFile =printerQueue.removeQ(); Print(printFile); }

Scheduling Queues Resident queue –on disk waiting for memory Ready queue –in memory, only needs CPU to run Suspended queue –waiting for I/O transfer or to be reassigned to the CPU

Contrasting Stacks and Queues StacksQueues LIFO Elements stored in reverse of order received Used in applications where reversal or "unwinding" needed FIFO Elements stored in same order as received Used where service rendered is relative to order received

Interesting Application Evaluate If A String Is A Palindrome This code will not do what it claims. Do a white box test of the code.

Implementation with An Array Requirements Array space for storage for the data A pointer to indicate the front of the queue A pointer to indicate the back of the queue Structures would include itemType myQueue [queueQuantity]; int myFront, myBack;

Implementation with An Array Additions to the queue –place new element where back points, increment myB ack Deletions from the queue –increment myF ront What problems can you see with this? myFront myBack myQueue

Implementation with An Array Obviously we would run out of space Possible solutions –Shifting the array elements downward with each deletion –Viewing array as a circular buffer, i.e. wrapping the end to the front

"Circular" Array-Implementation Wraparound keeps the addition/deletion cycle from walking off the edge of the array Given itemType myQueue [queueQuantity]; int myFront, myBack; When myBack (or myFront ) hits the end of myQueue, it should wrap around to the beginning myBback = (myBack + 1) % queueQuantity;

"Circular" Array-Implementation Initially queue is empty myFront = myBack = 0 ; How do we know queue is empty at any time??  myFront == myBack What would happen if array fills up … what would be value of myFront and myBack ? –they would be the same … why? So … how to tell empty from full??

"Circular" Array-Implementation Common solutions Keep an empty slot between myFront and myBack –declare array as itemType myQueue [queueQuantity +1]; Keep an auxiliary counter to track actual number of elements in queue

Queue Class Specification Note again, capacity and element type declared outside class. #ifndef QUEUE #define QUEUE constint QUEUE_CAPACITY = 128; typedefintQueueElement; class Queue { /***** Function Members *****/ public: Queue(); bool empty()const; bool full() const; voidaddQ(constQueueElement & value); QueueElement frontconst();//nondestructive “peek” QueueElementremoveQ(); // retrieve and remove /***** Data Members *****/ private: QueueElementmyArray[QUEUE_CAPACITY]; intmyFront, myBack; }; //end of class declaration #endif

Queue Class Implementation Use the class declaration to write the implementation Also write a driver to test the implementation It should –instantiate multiple queues –test for empty, full –add, remove, and look at the front element –enter values from keyboard, from file –print output of queue to screen, to file

Linked List Implementation Interface would remain the same –empty(), full(), front(), addQ(), removeQ() The storage structure would be a linked list. The markers would be pointers instead of indices into an array myFront contains pointer to first node myBack contains pointer to last node

Linked List Implementation empty –returns true if myFront points to NULL addQ –allocates new node –link it off myBack –update myBack removeQ –remove and return first element –update myFront

Linked List Implementation front –returns value of node where myFront points to full??? –returns true if myFront = = myBack Write specification and implementation as an exercise

Dequeue Double-ended-queue Similar to a queue But … additions and deletions can occur at either end Methods must have –either a parameter specifying at which end the action should occur –or multiple methods (functions) provided

addQ( ) Example for Dequeue Version with parameter for where addition takes place

Multiple Method Version voidDeque::push_front(int item) {if ((myBack +1)%QUEUE_CAPACITY ==myFront) cout << "FULL, cannot add to queue." <<endl; else //enqueue at front {if (!myFront) myFront =QUEUE_CAPACITY; else myFront--; myArray[myFront] = item; } return; } voidDeque::push_back(int item) { if ((myBack +1)%QUEUE_CAPACITY ==myFront) cout << "FULL, cannot add to queue." <<endl; else // regularenqueuing { myArray[myBack] = item; myBack = (myBack+ 1) %QUEUE_CAPACITY; } return; }

Multiple Methods for Removing

Comments on deque A standard container provided by the STL Easily implemented by the vector type More coming in Chapter 6

Priority Queues Definition : A queue where the order of the elements … –is by some priority –rather than by the order in which they are received Any queue implementation can be used –Only the addQ() method must be altered –Element is not added at back –Queue must be scanned for correct location for insertion

Priority Queues What would be the drawback of using an implementation that used arrays? –shifting elements Hence a linked list implementation is preferred

Priority Queues What changes would be made to this to use a linked list version?