Download presentation
Presentation is loading. Please wait.
2
Rossella Lau Lecture 11, DCO20105, Semester A,2005-6 DCO 20105 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
3
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: ABC D The original queue Take an itemAdd an item BC
4
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
5
Rossella Lau Lecture 11, DCO20105, Semester A,2005-6 Exercises Ford’s 8:9 queue 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 intQ; intQ.push(18); intQ.push(2); intQ.push(intQ.front()); intQ.pop();
6
Rossella Lau Lecture 11, DCO20105, Semester A,2005-6 Typical applications A 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
7
Rossella Lau Lecture 11, DCO20105, Semester A,2005-6 Services for others As 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
8
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 2525 5757 4848 3737 12129292 8686 3 1212 9292 3 2525 8686 5757 3737 4848 12 25 33 37 48 57 86 92 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 push(n i ) to qs[k] for (i=0;i<10,i++) while (!qs[i].empty()) qs[i].pop();
9
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 33124857 25
10
Rossella Lau Lecture 11, DCO20105, Semester A,2005-6 #include …… void bfs(void) const { queue *> 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 getItem() << " " ; children.pop(); } cout << endl; } BFS traversal
11
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
12
Rossella Lau Lecture 11, DCO20105, Semester A,2005-6 Using a cyclic vector for implementing queue ABC 01234 frontrear pop() : BCD 01234 frontrear push() : CD 01234 frontrear pop() : CDE 01234 frontrear push() : FCDE 01234 front rear push() : FDE 01234 frontrear pop() : BC 01234 frontrear Initial :
13
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: 01234 front rear 01234 front rear single element: A 01234 front rear
14
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. BC 01234 frontrear pop() : BCD 01234 frontrear push() : CD 01234 frontrear pop() : CDE 01234 frontrear push() : FCDE 01234 front rear push() : FDE 01234 front rear pop() :
15
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
16
Rossella Lau Lecture 11, DCO20105, Semester A,2005-6 Implementation of a queue using a list // Queue.h template class Queue { private: list 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();} };
17
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 Both operations are at O(1) Operations of a STL’s queue: Ford’s slides: 8.4-6
18
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
19
Rossella Lau Lecture 11, DCO20105, Semester A,2005-6 Exercises Ford’s 8:15 priority_queue 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 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;
20
Rossella Lau Lecture 11, DCO20105, Semester A,2005-6 priority_queue 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
21
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 taskQ; …… taskQ.push(taskObj); …… resultTask=taskQ.front(); taskQ.pop();
22
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
23
Rossella Lau Lecture 11, DCO20105, Semester A,2005-6 Reference Ford: 8 STL online references http://www.sgi.com/tech/stl http://www.sgi.com/tech/stl http://www.cppreference.com/ http://www.cppreference.com/ -- END --
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.