Queues
Queues
Queues
Slides by
Mohamed El-Ramly, PhD
Basheer Youssef, PhD
Agenda
1. Queues and their operations
2. Queues’ implementations
3. Queues’ applications
2
Queues
1. A queue is a linear data structure that
resembles a waiting line that grows by adding
elements to its end and shrinks by taking
elements from its front.
2. It is FIFO structure.
Queue Operations
1. clear () empties the queue
10 5 10 10 15 10 15 10 7 10 7
Implementing Priority Queue
1. One problem with priority queues is finding an
efficient implementation.
• Array?
• Linked List?
• What kind?
• Sorted or unsorted?
• Heap?
Queue in STL
Dequeue in STL
Priority Queue in STL
• Read the book and check cplusplus.com to
know what support is available for these data
structures in STL.
FIFO Queue in STL
• Queues is a FIFO container adaptor, where
elements are inserted into one end of the container
and extracted from the other.
front()
back()
push_back()
pop_front()
FIFO Queue in STL
• The standard container class templates deque
and list can be used.
int main() {
queue<int> q1;
queue<int,list<int> > q2; //leave space > >
q1.push(1); q1.push(2); q1.push(3);
q2.push(4); q2.push(5); q2.push(6);
q1.push(q2.back());
while (!q1.empty()) {
cout << q1.front() << ' '; // 1 2 3 6
q1.pop();
}
while (!q2.empty()) {
cout << q2.front() << ' '; // 4 5 6
q2.pop();
}
return 0;
}
27