Rossella Lau Lecture 11, DCO20105, Semester A,2005-6 DCO20105 Data structures and algorithms Lecture 11: Queue & Priority Queue Basic operations of a queue Applications of a queue Priority queue and the STL’s priority queue -- By Rossella Lau
Dec 22, 2015
Rossella Lau Lecture 11, DCO20105, Semester A,2005-6
DCO20105 Data structures and algorithms
Lecture 11: Queue & Priority Queue
Basic operations of a queue Applications of a queue Priority queue and the STL’s priority queue
-- By Rossella Lau
Rossella Lau Lecture 11, DCO20105, Semester A,2005-6
Queue
Queue is an ordered list of items, ordered in the input sequence
Basically, items are deleted at one end (the front/head of the queue) and added at the other end (the rear/tail of the queue)
An example:A B CDThe original queue
Take an itemAdd an item
B C
Rossella Lau Lecture 11, DCO20105, Semester A,2005-6
Operations of a queue
Basic: push() – to add an item; formerly called enqueue() pop() – to remove an item; formerly called dequeue()
Assistance: size() – returns the number of items in a queue empty() – to check if a queue is empty front() – returns the first element of a queue
Rossella Lau Lecture 11, DCO20105, Semester A,2005-6
ExercisesFord’s 8:9
queue<int> q;int x = 5, y = 3;
q.push(8);q.push(9);q.push(y);X = q.fron();q.pop();q.push(18);x = q.front();q.push(22);while (!q.empty()) { y = q.front(); q.pop(); cout << y << “ “;}cout << x << endl;
Ford’s 8:10: List the elements in intQ after each of the following operations:
queue<int> intQ; intQ.push(18);intQ.push(2);intQ.push(intQ.front());intQ.push(intQ.front());intQ.pop();
Rossella Lau Lecture 11, DCO20105, Semester A,2005-6
Typical applicationsA real life application – check out queues in a
super market (Ford’s slide: 8.2) When a customer goes to a cashier – push()
• If there is a cashier available, check out at that cashier• Line up a queue at the end and wait for checking out
When a cashier is working – pop()• Perform check out for the first customer on her/his
line one by one
Rossella Lau Lecture 11, DCO20105, Semester A,2005-6
Services for othersAs a service for other data structures and
algorithms Radix sort Traverse a binary tree in a breadth first order; i.e.,
from top to bottom and at each level, from left to right
Rossella Lau Lecture 11, DCO20105, Semester A,2005-6
Re-visit the Radix sort
25 57 48 37 12 92 86 33
12 92 33 25 86 57 37 48
25
5748
37
12 92
86
33
12
92
3325
86
57
3748
12 25 33 37 48 57 86 92
0123456789
0123456789
push(ni) to qs[k]
for (i=0;i<10,i++) while (!qs[i].empty()) qs[i].pop();
Rossella Lau Lecture 11, DCO20105, Semester A,2005-6
Breadth First Search (BFS) order
A BT’s Breadth First Search order is similar to the implicit array index
E.g., The BFS order for the BT on the right hand side is: 92, 37, 86, 33, 12, 48, 57, 25
92
8637
33 12 48 57
25
Rossella Lau Lecture 11, DCO20105, Semester A,2005-6
#include <queue>……void bfs(void) const { queue <BSTNode<T> *> children;
children.push(root); while (! children.empty()) { if (children.front()->left) children.push(children.front()->left); if (children.front()->right) children.push(children.front()->right); cout << children.front()->getItem() << " "; children.pop(); } cout << endl;}
BFS traversal
Rossella Lau Lecture 11, DCO20105, Semester A,2005-6
Implementation of a queue
When using a vector: As elements grow (shift) in one direction, slots in the
beginning become useless while new elements may be required to re-size the array frequently
Another approach is to use an array in a cyclic method -- calculation of the index becomes a bit more complicated and care should be taken in how to determine the front and the rear of the queue and an empty queue
Rossella Lau Lecture 11, DCO20105, Semester A,2005-6
Using a cyclic vector for implementing queue
A B C0 1 2 3 4
front rear
pop(): B C D0 1 2 3 4
front rear
push(): C D0 1 2 3 4
front rear
pop():
C D E0 1 2 3 4
front rear
push(): F C D E0 1 2 3 4
frontrear
push(): F D E0 1 2 3 4
frontrear
pop():
B C0 1 2 3 4
front rear
Initial:
Rossella Lau Lecture 11, DCO20105, Semester A,2005-6
Test of empty()
When front points to the first element of a queue, it is difficult to test if a queue is empty or has a single element
Initial or empty: 0 1 2 3 4
front rear
0 1 2 3 4
front rear
single element: A0 1 2 3 4
front rear
Rossella Lau Lecture 11, DCO20105, Semester A,2005-6
Modification of front
A special way is to make the front point to the vector index immediately preceding the first element.
B C0 1 2 3 4
front rear
pop(): B C D0 1 2 3 4
front rear
push(): C D0 1 2 3 4
front rear
pop():
C D E0 1 2 3 4
front rear
push(): F C D E0 1 2 3 4
frontrear
push(): F D E0 1 2 3 4
frontrear
pop():
Rossella Lau Lecture 11, DCO20105, Semester A,2005-6
list-based queue
Seems a more natural way to implement queue
But the cost of the operation new/delete makes a list-based container not favorable
Rossella Lau Lecture 11, DCO20105, Semester A,2005-6
Implementation of a queue using a list
// Queue.htemplate<class T>class Queue { private: list<T> queue;
public: T const & front() const { return queue.front();} void push(T const & item) {queue.push_back(item);} T & pop() {T t(front()); queue.pop_front(); return t;} size_t size() const { return queue.size();} bool empty() const { return queue.empty();}};
Rossella Lau Lecture 11, DCO20105, Semester A,2005-6
Execution time for operations of a queue
No matter which basic container is used to build a queue,
a push() is actually a push_back() a pop() is similar to a pop_front()
• Although pop_front() is not allowed in a STL’s vector, it can use a self-defined index to remember its head and move it forward to represent a pop() operation
• It can also use deque<T>Both operations are at O(1)Operations of a STL’s queue: Ford’s slides: 8.4-6
Rossella Lau Lecture 11, DCO20105, Semester A,2005-6
Priority Queue
Many applications need elements to queue up with priority.
Queues in bank: general a/c holders, priority a/c holders Task queues: jobs assigning a higher priority can be
executed earlier
Rossella Lau Lecture 11, DCO20105, Semester A,2005-6
ExercisesFord’s 8:15
priority_queue<int> pq;int arr[] = {10,50,30,40,60};int i, x, y;
for (i=0; i< 5; i++) pq.push(arr[i]);
pq.push(25);x = pq.top(); pq.pop();pq.push(35);y = pq.top(); pq.pop();cout << x << “ “ << y << endl;
while (!pq.empty()) { cout << pq.top() << “ “; pq.pop();}cout << endl;
Ford’s 8:16: priority_queue<int> intPQ;
intPQ.push(5);intPQ.push(27);intPQ.push(25);intPQ.pop();
intPQ.push(intPQ.top()*2);intPQ.push(intPQ.top()*5);
While (!intPQ.empty()) { cout<< intPQ.top() << “ “;
intPQ.opp();}cout << endl;
Rossella Lau Lecture 11, DCO20105, Semester A,2005-6
priority_queue<T>
The STL provides the class priority_queue E.g.,
• push(taskA,5), push(taskB,5), push(taskC,8), push(taskD,2), push(taskE,5), push(taskF,8)
• pop() returns (taskC, 8) priority_queue uses a heap to insert an item
• A heap works quite efficiently on vector and the STL uses a vector to implement priority_queue by default
The templated class of the priority_queue must provide the comparison operator <() for the heap construction
Rossella Lau Lecture 11, DCO20105, Semester A,2005-6
An example application
class task { string taskName, int priority; …}
operator< must be overloaded bool task::operator<(task const & rhs) const { return priority < rhs.priority;}
The application: priority_queue<task> taskQ; …… taskQ.push(taskObj); …… resultTask=taskQ.front(); taskQ.pop();
Rossella Lau Lecture 11, DCO20105, Semester A,2005-6
Summary
Queue can use different kinds of sequential containers with restricted operations as adaptor containers in the STL
Both the basic operations of a queue are at O(1) and make a queue one of the favored data structures being used in many other algorithms
Priority queue, in addition to the behaviors of a queue having a priority, uses a heap with a comparison method built on a vector or a deque
Rossella Lau Lecture 11, DCO20105, Semester A,2005-6
Reference
Ford: 8
STL online references http://www.sgi.com/tech/stl http://www.cppreference.com/
-- END --