Stack, Queues, and Priority Queues
Stack, Queues, and Priority Queues
Stack, Queues,
and Priority
queues
1/17
4. Stack, Queues, and Priority
queues
Stack:
pop
2/17
4. Stack, Queues, and Priority
queues
The order in which elements are added/removed gives the stack its
alternative name: LIFO (Last In First Out).
3/17
4. Stack, Queues, and Priority
queues
go out!
Basic Operations of the stack (2) t to
:
LIFO is th e firs
e stack
h
e nter t
st to
la
We can use push and pop to insert or remove The
4/17
4. Stack, Queues, and Priority
queues
Advantage Disadvantage
Array Quick Limited in size
Linked List Unlimited in size Requires overhead to allocate,
deallocate, link and unlink.
5/17
4. Stack, Queues, and Priority
queues
Definition of a queue
A queue is an abstract data type in which the first element is inserted from
one end called the rear (also called tail) and the removal of existing
element takes place from the other end called front (or head)
They operate in a FIFO (First In First Out) type of arrangement
Elements are inserted at the back (end) and are deleted from the front
E.g.:
Queue for tickets in a cinema The first person in the queue will be the
first to leave the queue when he has paid for the ticket.
6/17
4. Stack, Queues, and Priority
queues
Implementation of a queue
7/17
4. Stack, Queues, and Priority
queues
8/17
4. Stack, Queues, and Priority
queues
• Abstract data type, similar to a stack or a queue, but with the addition of
each element having a priority associated to them.
• If two elements have the same priority, they are served in the order of
their enqueing.
• They are often implemented with heaps, hence they are also known as
binary heaps, but it is important to remark that they are not conceptually
the same.
9/17
4. Stack, Queues, and Priority
queues
10/17
4. Stack, Queues, and Priority
queues
11/17
4. Stack, Queues, and Priority
queues
When you want to insert a new element into a heap. We créate the hole at the end of heap and bubble it
up until the parent value is less than the inserted value.
Step 1
12/17
4. Stack, Queues, and Priority
queues
When you want to insert a new element into a heap. We créate the hole at the end of heap and bubble it
up until the parent value is less than the inserted value.
Step 2
13/17
4. Stack, Queues, and Priority
queues
You remove the root value and replace it with a hole. Then keeping moving the hole downwards replacing
it with the smaller of the two children.
Step 1
14/17
4. Stack, Queues, and Priority
queues
You remove the root value and replace it with a hole. Then keeping moving the hole downwards replacing
it with the smaller of the two children.
Step 2
15/17
4. Stack, Queues, and Priority
queues
You remove the root value and replace it with a hole. Then keeping moving the hole downwards replacing
it with the smaller of the two children.
Step 3
16/17
4. Stack, Queues, and Priority
queues
NOTE: The push () and pop () operations, for example, are O (1) because we always
have to insert or remove the data from the top of the stack, which is a one step
process.
17/17
4. Stack, Queues, and Priority
queues
For the queue operations, we have the following running time complexity operations:
18/17
4. Stack, Queues, and Priority
queues
O (n* log(n))
O (n)
19/17
4. Stack, Queues, and Priority
queues
20/17
4. Stack, Queues, and Priority
queues
Implementation in STL
How are stacks, queues and priority queues implemented in the Standard Library?
Stack std::stack
Queue std::queue
21/17
4. Stack, Queues, and Priority
queues
22/17
4. Stack, Queues, and Priority
queues
23/17
Implementation of priority queue in STL
24
4. Stack, Queues, and Priority
queues
Thank you!
25/17