CSCI01C_3_Queues
CSCI01C_3_Queues
Introduction to
Data Structure
and Algorithm
Design
Lecture 3
CSCI01C 2
This lecture
• Queue abstraction
• Simple Queue implementation
• Circular Queues
CSCI01C 3
Queue
abstraction
CSCI01C 4
Queue ADT
• In real life, a queue is a “waiting
line”.
CSCI01C 5
This Photo by Unknown Author is licensed under CC BY
Queue ADT
• In a queue ADT, items are removed
from a queue at one end, called the
front (or head) of the queue, and
elements are added only at the
other end called the back (or rear or
tail).
CSCI01C 6
This Photo by Unknown Author is licensed under CC BY
Queue ADT
Definition
• (Simple Queue) A sequence of data items with the property that items can be removed only at
one end, called the front of the queue, and items can be added only at the other end, called the
back of the queue.
Basic Operations
• Construct a queue (usually empty)
• Check if queue is empty
• Check if queue is full
• Enqueue/push: Add an element at the back of the queue
• Dequeue/pop: Remove the element at the front of the queue
• Front: Retrieve the element at the front of the queue
CSCI01C 7
Use cases of queue
• Handling requests for shared
resources (e.g. printer )
Queue ADT • Job scheduling and execution
by a computer processor
• Simulating wait in Traffic
systems
8 CSCI01C
What do we need to implement a Queue?
CSCI01C 9
Queue ADT
• Example:
• Assume we just created a queue data structure. Let’s trace what
happens by calling some of the queue class methods.
CSCI01C 10
Queue ADT
• >> isEmpty()
• //returns true as F = -1 and R = -1
• >> push(2)
• //adds 2 to the tail, R increments
(=0) and since it’s the first
element then F increments(=0)
CSCI01C 11
Queue ADT
• >> isEmpty()
• //returns true as F = -1 and R = -1
• >> push(2)
• //adds 2 to the tail, R increments (=0) and since
it’s the first element then F increments(=0)
• >> push(5)
• //adds 5 at the tail, R increments (=1) and F stays
the same (=0)
CSCI01C 12
Queue ADT
• >> isEmpty()
• //returns true as F = -1 and R = -1
• >> push(2)
• //adds 2 to the tail, R increments (=0) and since
it’s the first element then F increments(=0)
• >> push(5)
• //adds 5 at the tail, R increments (=1) and F stays
the same (=0)
CSCI01C 13
Queue ADT
• >> isEmpty()
• //returns true as F = -1 and R = -1
• >> push(2)
• //adds 2 to the tail, R increments (=0) and since it’s the
first element then F increments(=0)
• >> push(5)
• //adds 5 at the tail, R increments (=1) and F stays the same
(=0)
• >> pop()
• //removes 2, F increments (=1) and R stays the same (=2)
CSCI01C 14
Queue ADT
• >> isEmpty()
• //returns true as F = R and R = -1
• >> push(2)
• //adds 2 to the tail, R increments (=0) and since it’s the
first element then F increments(=0)
• >> push(5)
• //adds 5 at the tail, R increments (=1) and F stays the same
(=0)
• >> push (3)
• //adds 3 at the tail, R increments (=2) and F stays the same
(=0)
• >> pop()
• //removes 2, F increments (=1) and R stays the same (=2)
• >> front()
• //returns 5, both F and R don’t change
• >> isEmpty()
• //returns false as F ≠ R and R > -1
CSCI01C 15
Queue ADT
• >> pop()
• //removes 5, F increments (=2) and R stays
the same (=2)
• >> isEmpty()
• //returns false as F = R but R > -1
• >> pop()
• // removes 3, since F = R ‘only one element
in the stack’ we set them back to -1
• >> isEmpty()
• //returns true as F = R and R = -1
CSCI01C 16
Queue ADT
>> isEmpty()
>> push(2)
>> push(5)
>> push (3)
>> pop()
>> front()
>> isEmpty()
>> pop()
>> isEmpty()
>> pop()
>> isEmpty()
CSCI01C 17
Implementation of Queues
• Constructor:
• Simply allocates memory and initializes both of the members myFront and
myBack to -1.
• Empty:
• A queue will be empty when the Boolean expression
(myFront == -1) && (myBack == -1) is true.
• Full:
• A queue will be full when the rear index reaches QUEUE_CAPACITY – 1
CSCI01C 18
Implementation of Queues
• Push(value):
• adds new elements at the rear/tail of the queue
• If the queue is full: return a signal that a queue error (queue full)
occurred.
CSCI01C 19
Implementation of Queues
• Pop:
• Removes the element at the front of the queue.
• Else
• Set myFront equal to (myFront + 1)
CSCI01C 20
Implementation of Queues
• Front:
• returns the value of the element at the queue front.
• If !(myFront == -1):
return myArray[myFront]
• else:
return error signal queue is empty
CSCI01C 21
Queue
implementation
Kindly stick to the code
implementation you will learn in the
lab
CSCI01C 22
Example
Code
CSCI01C 23
Testing
the
code
CSCI01C 24
Queue in C++ STL
• If you decided to solve a problem
using the STL implementation of a
queue
https://cplusplus.com/reference/q
ueue/queue/
CSCI01C 25
Example of using queues
Concatenating two strings using a queue
CSCI01C 26
Example of using queues
Concatenating two strings using a queue
CSCI01C 27
Example of using queues
Concatenating two strings using a queue
CSCI01C 28
Implementing a queue using two stacks
PUSH
• It is very common to use one data structure logic to
implement another data structure logic.
QUEUE
PUSH POP 6
• A stack pushes and pops from one end (top) 5
• A queue pushes at one end (top) but pops at another 4
end (bottom) 3
6 2
• If a stack will replace a queue, the push operation will 5 1
work fine but the pop operation will face a problem.
4 POP
• The popped element has to be on the top of a stack i.e.
we need to reverse the stack content. This cannot be 3
done by one stack only. 2
• To implement a queue logic (FIFO) we need two stacks 1
(LIFO).
CSCI01C Stack 29
PUSH
QUEUE
6
5
1. 2. 3. 4
The Queue ADT PUSH POP POP
ALL ALL top 3
• Constructor: Two stacks objects instead of an array and 2
front/rear index 1
• Push: Stack_1 push POP
• Pop: Stack_1 pops all and Stack_2 pushes all then Stack_2 pop 6 1
its top element 5 2
• Front: Stack_1 pops all and Stack_2 pushes then Stack_2
STACKS
4 3
retrieves Top 3 4
CSCI01C 30
Implementing a queue using two stacks
• You better use the STL implementation of stacks
• Note: The library offers these functions only: https://cplusplus.com/reference/stack/stack/
• i.e., there is no isFull() or display()
CSCI01C 31
Implementing a queue using two stacks
• Thus, it is enough to implement a Queue class with the following basic
functions based on using stacks:
• Queue();
• void Push(datatype val); // without a queue_full check
• datatype Pop(); //No need for the front function
• bool isEmpty();
• void display();
CSCI01C 32
Other forms of Queue
CSCI01C 33
Circular
Queues
CSCI01C 34
Circular Queues
• This shifting of “Front” and “Rear” indices rather than shifting the
array elements locations is very inefficient.
• Successive pops will empty the front part of the queue while the back
end could be full (myBack = QUEUE_CAPACITY -1) and thus, no
more pushes are allowed.
CSCI01C 35
Circular Queues
• A better alternative is to simply let myBack start over
at the beginning of the array when it goes off the end.
• Adding a new value ‘8’ to the queue: Front index = 2, Rear index = 0
CSCI01C 37
Circular Queues
• Therefore,
• In a simple queue, insertion takes place at the rear and removal
occurs at the front. It strictly follows the FIFO (First in First out) rule.
CSCI01C 38
Circular Queues
• Push and Pop functions will be updated.
• Else
• Set myFront equal to (myFront + 1)%QUEUE_CAPACITY
CSCI01C 40
Circular Queues
• Also, isEmpty and isFull conditions will vary:
• isEmpty(): Checks if no elements are stored in the queue
• Else
• Return false
CSCI01C 41
Circular Queues
• isFull(): Checks if the queue reached its maximum capacity
• If myFront == 0 && myBack == QUEUE_CAPACITY -1 // queue is full
• Return true
• Else
• Return false
CSCI01C 42
FYI: Priority queues
Out of the course’s scope
• A priority queue is a special type of queue in
which each element is associated with a priority
value.
• Elements are served on the basis of their priority,
the higher priority the first served.
• If elements with the same priority occur, they are
served according to their order in the queue.
• Priority queue can be implemented using an array,
a linked list, a heap data structures.
CSCI01C 43
FYI: Double-ended queues
Out of the course’s scope
• In a double-ended queue (Deque), insertion and deletion can take
place at both the front and rear ends of the queue.
• Thus, it does not follow the FIFO (First In First Out) rule.
• It is faster in push/pop operations than the other types.
• Mainly needed for task scheduling for several processors.
CSCI01C 44
Wrap up • Queue abstraction
• Queue implementation
• Circular queue
CSCI01C 45
Thank You
CSCI01C 46