![Page 1: 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)](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1acf7f8b9ab059970d66/html5/thumbnails/1.jpg)
Queues
Chapter 5
![Page 2: 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)](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1acf7f8b9ab059970d66/html5/thumbnails/2.jpg)
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)
![Page 3: 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)](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1acf7f8b9ab059970d66/html5/thumbnails/3.jpg)
Basic Operations
• construct a queue (usually empty)• empty Check if queue is empty• addQ Add an item to back of
queue• front Retrieve element at front of
queue• RemoveQ Remove element at front of
queue
![Page 4: 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)](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1acf7f8b9ab059970d66/html5/thumbnails/4.jpg)
Queue FAQs
• A queue is a FIFO structureFirst-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);}
![Page 5: 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)](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1acf7f8b9ab059970d66/html5/thumbnails/5.jpg)
Scheduling Queues
• Resident queue– on disk waiting for
memory• Ready queue
– in memory, onlyneeds CPU to run
• Suspended queue– waiting for I/O transfer or to be reassigned to
the CPU
![Page 6: 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)](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1acf7f8b9ab059970d66/html5/thumbnails/6.jpg)
Contrasting Stacks and Queues
Stacks Queues•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
![Page 7: 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)](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1acf7f8b9ab059970d66/html5/thumbnails/7.jpg)
Interesting Application Evaluate If A String Is A Palindrome
#include “Stack.h”;#include “Queue.h”;#include <iostream.h>using namespace std;int main(){
Stack S;Queue Q;char ch;cout << “Enter string to be tested as palindrome: “;while (cin >> ch){
S.push(ch);Q.addq(ch);
}
bool pal = true;while (!Q.empty())
pal = Q.removeq() = = S.pop();if (pal)
cout << “Palindrome!!” << endl;return 0;
}
This code will not do what it claims. Do a white box
test of the code.
![Page 8: 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)](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1acf7f8b9ab059970d66/html5/thumbnails/8.jpg)
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 includeitemType myQueue [queueQuantity];int myFront, myBack;
![Page 9: 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)](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1acf7f8b9ab059970d66/html5/thumbnails/9.jpg)
Implementation with An Array• Additions to the queue
– place new element where back points, increment myBack
• Deletions from the queue– increment myFront
• What problems can you see with this?myFront myBack
myQueue
![Page 10: 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)](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1acf7f8b9ab059970d66/html5/thumbnails/10.jpg)
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
![Page 11: 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)](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1acf7f8b9ab059970d66/html5/thumbnails/11.jpg)
"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 beginningmyBback = (myBack + 1) % queueQuantity;
![Page 12: 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)](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1acf7f8b9ab059970d66/html5/thumbnails/12.jpg)
"Circular" Array-Implementation
• Initially queue is emptymyFront = 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??
![Page 13: 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)](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1acf7f8b9ab059970d66/html5/thumbnails/13.jpg)
"Circular" Array-Implementation
Common solutions• Keep an empty slot between myFront and myBack– declare array asitemType myQueue [queueQuantity +1];
• Keep an auxiliary counter to track actual number of elements in queue
![Page 14: 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)](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1acf7f8b9ab059970d66/html5/thumbnails/14.jpg)
Queue Class SpecificationNote again, capacity and
element type declared outside class.
#ifndef QUEUE#define QUEUEconst int QUEUE_CAPACITY = 128;typedef int QueueElement;class Queue{ /***** Function Members *****/ public:
Queue();bool empty() const;bool full() const;void addQ(const QueueElement & value);QueueElement front const(); //nondestructive “peek”QueueElement removeQ(); // retrieve and remove
/***** Data Members *****/ private:
QueueElement myArray[QUEUE_CAPACITY];int myFront,
myBack;}; // end of class declaration#endif
![Page 15: 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)](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1acf7f8b9ab059970d66/html5/thumbnails/15.jpg)
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
![Page 16: 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)](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1acf7f8b9ab059970d66/html5/thumbnails/16.jpg)
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
![Page 17: 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)](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1acf7f8b9ab059970d66/html5/thumbnails/17.jpg)
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
![Page 18: 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)](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1acf7f8b9ab059970d66/html5/thumbnails/18.jpg)
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
![Page 19: 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)](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1acf7f8b9ab059970d66/html5/thumbnails/19.jpg)
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
![Page 20: 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)](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1acf7f8b9ab059970d66/html5/thumbnails/20.jpg)
addQ( ) Example for Dequeue
Version with parameter for where addition takes place
![Page 21: 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)](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1acf7f8b9ab059970d66/html5/thumbnails/21.jpg)
Multiple Method Versionvoid Deque::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;}
void Deque::push_back( int item){ if (( myBack +1)% QUEUE_CAPACITY == myFront)
cout << "FULL, cannot add to queue." << endl; else // regular enqueuing {
myArray[ myBack] = item;myBack = ( myBack+ 1) % QUEUE_CAPACITY;
} return;}
![Page 22: 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)](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1acf7f8b9ab059970d66/html5/thumbnails/22.jpg)
Multiple Methods for Removing
![Page 23: 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)](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1acf7f8b9ab059970d66/html5/thumbnails/23.jpg)
Comments on deque
• A standard container provided by the STL• Easily implemented by the vector type• More coming in Chapter 6
![Page 24: 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)](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1acf7f8b9ab059970d66/html5/thumbnails/24.jpg)
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
![Page 25: 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)](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1acf7f8b9ab059970d66/html5/thumbnails/25.jpg)
Priority Queues
• What would be the drawback of using an implementation that used arrays?
– shifting elements
• Hence a linked list implementation is preferred
![Page 26: 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)](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1acf7f8b9ab059970d66/html5/thumbnails/26.jpg)
Priority Queues
What changes would be made to this to use a linked list version?