0% found this document useful (0 votes)
13 views46 pages

CSCI01C_3_Queues

This lecture covers the concept of queues in data structures, explaining their abstraction, implementation, and operations such as enqueue and dequeue. It also discusses circular queues, highlighting their efficiency over simple queues by using a circular array structure. Additionally, the lecture touches on priority queues, which serve elements based on their priority rather than their order of arrival.

Uploaded by

qfzg7f99qd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views46 pages

CSCI01C_3_Queues

This lecture covers the concept of queues in data structures, explaining their abstraction, implementation, and operations such as enqueue and dequeue. It also discusses circular queues, highlighting their efficiency over simple queues by using a circular array structure. Additionally, the lecture touches on priority queues, which serve elements based on their priority rather than their order of arrival.

Uploaded by

qfzg7f99qd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

CSCI01C:

Introduction to
Data Structure
and Algorithm
Design
Lecture 3

Dr. Randa Elanwar


Email: [email protected]
Office: H111
Previously,
• Stack abstraction
• Stack implementation
• Examples on using stacks:
• Reversing a string
• Checking balanced parenthesis
• More on Stacks
• Infix, Prefix, and Postfix
• Infix/Prefix/Postfix conversions
using stack

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”.

• For customers, planes, vehicles in a


waiting line, the first thing to enter
the queue has been waiting the
longest  must be served first

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).

• Therefore, a queue is the data


structure of whatever goes in first,
comes out first (First in First out:
FIFO)

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?

• Queues can be implemented using arrays as the basic storage structures

• The queue data structure is composed of


• a series of contiguous memory locations (initially unassigned)  myArray[QUEUE_CAPACITY]
• an int value saving the index of the front element (initially -1)  myFront
• an int value saving the index of the rear element (initially -1)  myBack

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)

• >> push (3)


• //adds 3 at the tail, R increments (=2) 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)

• >> 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)

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.

• Else If the queue is empty:


• Set myFront =0 and myBack =0
• myArray[myBack] equal to value.

• Else: increment the rear index (myBack + 1)


• Set myArray[myBack] equal to value.

CSCI01C 19
Implementation of Queues
• Pop:
• Removes the element at the front of the queue.

• If the queue is empty // checks both F and R == -1


• Return a signal that a queue error (queue empty) occurred.

• If myFront == myBack // queue has one element


• Set myFront = - 1 and set myBack = -1

• 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/

• Please note that the available


functions in the STL
implementation:

CSCI01C 25
Example of using queues
Concatenating two strings using a queue

• Slight change in the queue class


implementation:
• The array should store char data.
• The Push function argument and Front
function return will be of type char.

CSCI01C 26
Example of using queues
Concatenating two strings using a queue

• Step1: A queue is created


• Step 2: two strings are pushed to it.
• Step 3: The queue is popped to overwrite
the content of the first string with the
concatenated string

• You can use STL also to solve the example

CSCI01C 27
Example of using queues
Concatenating two strings using a queue

• Testing the code

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

Implementing a queue using two stacks

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

• Empty: checking that both stacks are empty 2 5


1 6

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.

• Thinking of the array as circular, with the first element


following the last.

• This can be done by incrementing myFront and


myBack using addition modulo the array's capacity
instead of incrementing by 1.
myFront = (myFront + 1) % QUEUE_CAPACITY
myBack = (myBack + 1) % QUEUE_CAPACITY
CSCI01C 36
Circular Queues
• Adding a new value ‘5’ to the queue: Front index = 2, Rear index = 9

• myBack = (myBack + 1) % QUEUE_CAPACITY


• myBack = (9 + 1) % 10 = 0
• myArray[myBack] = 5

• Adding a new value ‘8’ to the queue: Front index = 2, Rear index = 0

• myBack = (myBack + 1) % QUEUE_CAPACITY


• myBack = (0 + 1) % 10 = 1
• myArray[myBack] = 8

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.

• A circular queue is a special case of a simple queue in which the last


member is linked to the first. As a result, a circle-like structure is
formed.

CSCI01C 38
Circular Queues
• Push and Pop functions will be updated.

• Push(value): adds new elements at the rear/tail of the queue


• If (myBack + 1)%QUEUE_CAPACITY == myFront: // the next position to R is F
• return a signal that a queue error (queue full) occurred.

• Else If the queue is empty:


• Set myFront =0 and myBack =0
• myArray [myBack] equal to value.

• Else: increment the rear index (myBack + 1)%QUEUE_CAPACITY


• Set myArray [myBack] equal to value.
CSCI01C 39
Circular Queues
• Pop():
• Removes the element at the front of the queue.

• If the queue is empty // checks both F and R == -1


• Return a signal that a queue error (queue empty) occurred.

• If myFront == myBack // queue has one element


• Set myFront = - 1 and set myBack = -1

• 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

• If myFront == -1 // queue is empty


• Return true

• 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

• If myFront == myBack + 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

• The list ADT


Next • Linked list abstraction
• Linked list implementation
• Examples on using linked lists

CSCI01C 45
Thank You

CSCI01C 46

You might also like