0% found this document useful (0 votes)
24 views25 pages

Stack, Queues, and Priority Queues

The document provides an overview of stacks, queues, and priority queues, defining each data structure and their basic operations. Stacks operate on a LIFO basis, while queues operate on a FIFO basis, and priority queues manage elements based on priority. The document also discusses implementation methods, running time complexities, and how these data structures are represented in the Standard Library.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views25 pages

Stack, Queues, and Priority Queues

The document provides an overview of stacks, queues, and priority queues, defining each data structure and their basic operations. Stacks operate on a LIFO basis, while queues operate on a FIFO basis, and priority queues manage elements based on priority. The document also discusses implementation methods, running time complexities, and how these data structures are represented in the Standard Library.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

4.

Stack, Queues,
and Priority
queues

1/17
4. Stack, Queues, and Priority
queues

What is a stack, a queue and a priority queue?

Stack:

 A stack is an abstract data type with a bounded (predefined) capacity.

 Simple data structure that allows adding and removing elements in a


particular order.

 Abstract data type that serves as a collection of elements, with two


principal operations:
 push

 pop

2/17
4. Stack, Queues, and Priority
queues

Basic Operations of the stack

The stack operation has 2 main operations:


 Push  Adds an element to the collection

 Pop  Removes the most recently added element to the collection

The order in which elements are added/removed gives the stack its
alternative name: LIFO (Last In First Out).

The stack is said to be in an Overflow state when it is completely full


and in an Underflow state if it is completely empty.

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

elements  Only at one end: the top of the stack!

NOTE: We can use the peek operation to know the


value of the top element, without removing it.

Example of LIFO in real life:


A stack of books

4/17
4. Stack, Queues, and Priority
queues

Implementation of the stack data structure

Stack can be easily implemented using an array or a


linked list.

In the following table we can see pros and cons of


each implementation way:

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

• Adding an element  Enqueue


• Removing an element  Dequeue
• Implemented using a ring buffer

7/17
4. Stack, Queues, and Priority
queues

Implementation of a queue (2)

 Algorithm to do an enqueue operation:


1. Check if the queue is full or not.
2. If the queue is full, then print overflow error and exit the program.
3. If the queue is not full, then increment the tail and add the element.

 Algorithm to do a dequeue operation:


1. Check if the queue is empty or not.
2. If the queue is empty, then print underflow error and exit the program.
3. If the queue is not empty, then delete the element at the head.

8/17
4. Stack, Queues, and Priority
queues

Definition of a priority queue

• 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

Implementation of a priority queue

A common way to implement a priority queue is with a binary heap


 A binary heap is a data structure that takes the form of a binary
tree

Example of a min-heap property binary


tree (the value of each children node is
greater or equal to the value of its
parent)

10/17
4. Stack, Queues, and Priority
queues

Complete tree versus heap

Only the left one is a heap.

11/17
4. Stack, Queues, and Priority
queues

Inserting an element into a heap

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

Inserting an element into a heap

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

Taking an element out of heap (deleteMin)

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

Taking an element out of heap (deleteMin)

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

Taking an element out of heap (deleteMin)

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

Running time complexity of operations – Stack operations

For the stack operations, we have the following:

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

Running time complexity of operations – Queue operations

For the queue operations, we have the following running time complexity operations:

18/17
4. Stack, Queues, and Priority
queues

Running time complexity of operations – Priority Queues operations

• If we want to add/remove n elements, one at a time, then the complexity is:

O (n* log(n))

• Only initially, to build the heap, it takes the complexity of:

O (n)

19/17
4. Stack, Queues, and Priority
queues

Running time complexity of operations - Summary

20/17
4. Stack, Queues, and Priority
queues

Implementation in STL

How are stacks, queues and priority queues implemented in the Standard Library?

Container adapter Implementation

Stack std::stack

Queue std::queue

Priority Queue std::priority_queue

21/17
4. Stack, Queues, and Priority
queues

Implementation of stack in STL

22/17
4. Stack, Queues, and Priority
queues

Implementation of queue in STL

23/17
Implementation of priority queue in STL

Syntax Methods Description


priority_queue<type> pq: inserts the element into the priority
push()
queue

removes the element with the highest


pop()
priority

returns the element with the highest


top()
priority

size() returns the number of elements

returns true if the priority_queue is


empty()
empty

24
4. Stack, Queues, and Priority
queues

Thank you!

25/17

You might also like