Download presentation
Presentation is loading. Please wait.
Published byBarbara Carroll Modified over 9 years ago
2
Queues Chapter 5
3
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)
4
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
5
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); }
6
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
7
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
8
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.
9
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;
10
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
11
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
12
"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;
13
"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??
14
"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
15
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
16
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
17
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
18
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
19
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
20
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
21
addQ( ) Example for Dequeue Version with parameter for where addition takes place
22
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; }
23
Multiple Methods for Removing
24
Comments on deque A standard container provided by the STL Easily implemented by the vector type More coming in Chapter 6
25
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
26
Priority Queues What would be the drawback of using an implementation that used arrays? –shifting elements Hence a linked list implementation is preferred
27
Priority Queues What changes would be made to this to use a linked list version?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.