Week 5
– Queues and Priority Queues
1
Main Index
Contents
Container Types
2
Sequence
Containers
Adapter
Containers
Associative
Containers
Vector
Stack
Set, Multiset
Deque
Queue
Map,
Mutltimap
List
Priority Queue
Main Index
Main
Index
Content
Contents
s
Grocery Store Checkout: A
Model for a Queue
3
Main Index
Contents
Queues
A queue inserts new elements at the back and removes
elements from the front of the sequence.
Pop
Push
Queue: First in, first out, (FIFO)
Stack: Last in, first out, (LIFO)
The Queue
A
push A
front
back
A
B
push B
front back
A
B
front
B
C push C
back
C
pop A
front back
C
A Queue is a FIFO
(First in First Out) Data
Structure. Elements are
inserted in the Rear of
the queue and are
removed at the Front.
pop B
front
back
6
Main Index
Contents
CLASS queue
Constructor
<queue>
Operations
<queue>
queue();
Create an empty queue.
CLASS queue
bool empty() const;
Check whether the queue is empty. Return true if it is
empty and false otherwise.
int size() const;
Return the number of elements in the queue.
7
Main Index
Contents
CLASS queue
Operations
<queue>
T & front();
Return a reference to the value of the item at the font
of the queue. The "oldest" element.
Precondition: The queue is not empty.
T & Back();
Return a reference to the value of the item at the end
of the queue. The “newest" element.
Precondition: The queue is not empty.
8
Main Index
Contents
CLASS queue
Operations
<queue>
void push(const T& item);
Insert the argument item at the back of the queue.
Postcondition: The queue has a new item at the back
void pop();
Remove the item from the front of the queue.
Precondition: The queue is not empty.
Postcondition: The element at the front of the queue
is the element that was added
immediately after the element just
popped or the queue is empty.
9
Main Index
Contents
Implementation of Queue in List
template <typename T>
{
public:
miniQueue();
//member functions push(), front(), size(),
//empty()
private:
list<T> qlist;
};
Implement the functions empty() and size() by returning
the result from a call to the corresponding list functions.
Application of Queue
Queues provide services in Computer Science, transport, and operations
research where various entities such as data, objects, persons, or events are
stored and held to be processed later. In these contexts, the queue performs
the function of a buffer. Buffers are usually used in a FIFO (first in, first
out) method, outputting data in the order it arrived.
Buffers are often used in conjunction with I/O to hardware, such as disk
drives, sending or receiving data to or from a network, or playing sound on a
speaker.
Priority Queue
A Special form of queue from which items are removed
according to their designated priority and not the order
in which they entered.
Job # 1
Manager
Job # 4
Supervisor
Job # 3
Clerk
Job # 2
President
Items entered the queue in sequential order but will be
removed in the order #2, #1, #4, #3.
15
Main Index
Contents
CLASS priority_queue
Constructor
<queue>
priority_queue();
Create an empty priority queue. Type T must
implement the operator <.
CLASS priority_queue
Operations
<queue>
bool empty() const;
Check whether the priority queue is empty. Return true
if it is empty, and false otherwise. Create
void pop();
Remove the item of highest priority from the queue.
Precondition: The priority queue is not empty.
Postcondition: The priority queue has 1 less element
16
Main Index
Contents
CLASS priority_queue
Operations
<queue>
void push(const T& item);
Insert the argument item into the priority queue.
Postcondition: The priority queue contains a new
element.
int size() const;
Return the number of items in the priority queue.
T& top();
Return a reference to the item having the highest
priority.
Precondition: The priority queue is not empty.
17
Main Index
Contents
Double ended queue
Deque (usually pronounced like "deck") is an irregular acronym of doubleended queue.
Double-ended queues are sequence containers with dynamic sizes that can
be expanded or contracted on both ends (either its front or its back).
Similar to vectors, but with efficient insertion and deletion of elements also
at the beginning of the sequence, and not only at its end.
But, unlike vectors, deques are not guaranteed to store all its elements in
contiguous storage locations.
vector holds data in a contiguous memory block that has the same memory footprint as a classical
array;
Expensive to insert or delete in the middle as it involves copying to maintain the memory
layout;
Can only append to one end;
Never releases memory once allocated without user intervention;
Has random access and therefore random access iterators;
Should be the default container of choice unless a specific design decision can be found to
use another container type.
list Holds data in individually allocated blocks of memory;
Quick to insert or delete and beginning, end or in the middle also quick to sort/reorder as
these just involve swapping pointer values not objects;
Does not have random access and therefore does not have random access iterators only
sequential iterators.
deque Holds data in blocks of allocated memory;
Quick to insert or delete and beginning or end, deletion/insertion in the middle is slow;
Has random access and therefore random access iterators.
A benchmark comparing the performance of std::vector,
std::list and std::deque on different workloads:
C++ benchmark
– std::vector VS std::list VS std::deque
Container classes
Underlying storage structure
The storage structure should be selected so that
the container operations can be implemented
efficiently
22
Main Index
Contents
Reading & HW reminder
Book chapter 3.7, chapter 6
HW3 is due this Friday.
(Submission before Thursday will receive
20 additional points)
23
Main Index
Contents
© Copyright 2026 Paperzz