0% found this document useful (0 votes)
59 views35 pages

Queue Operations in C++ Lecture

Uploaded by

titirshaasingh
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)
59 views35 pages

Queue Operations in C++ Lecture

Uploaded by

titirshaasingh
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/ 35

Subject: Data Structure

Using C++ (CSET243)

Week 9 Lecture

Queue & Operations


What, Why and How?

by

Dr Gaurav Kumar
Asst. Prof, Bennett University
Quick Recap of Previous Weeks’ Learnings

• Understood the STACK Applications

• Discussed the Polish Notation

• Solved Expression Evaluation using Stack


Understanding Queue

A Queue is defined as a linear data


structure that is open at both ends
and the operations are performed in
First In First Out (FIFO) order.
Applications of Queue

1. When a resource is shared among


multiple consumers. Examples include CPU
scheduling, Disk Scheduling, Task Scheduling
Applications of Queue

• Luggage checking machine

• Vehicles on toll tax bridge

• One way exits

• Patients waiting outside the doctor’s clinic

• Phone answering systems

• Cashier line in a store


Assessment Time

Which of the following is not a real-life


example of a Queue?
A) Waiting in line to order food at a restaurant
B) Waiting in line to buy movie tickets
C) Managing tasks on a CPU
D) Using a stack of dishes

Correct Answer is D
Standard Terms used in Queue

• The first entry that will be removed from


the queue, is called the front (or head) of
the queue.

• The position of the last entry in the queue,


(one most recently added) is called
the rear (or the tail) of the queue.
Basic Operations enqueue(): Inserts an
element at the end of the
used in Queue queue i.e. at the rear
end.

dequeue(): This
rear(): This operation
operation removes and
returns the element at
returns an element that
the rear end without
is at the front end of the
removing it.
queue.

front(): This operation


returns the element at
the front end without
removing it.
Basic Operations
used in Queue isEmpty(): This
operation indicates
whether the queue is
empty or not.

size(): This operation


returns the size of the isFull(): This operation
queue i.e. the total indicates whether the
number of elements it queue is full or not.
contains.
Implementation of Queue using Array

• For implementing queue, we need to keep track of two indices,


front and rear.

• We enqueue an item at the rear and dequeue an item from the


front.
Empty Queue Implementation using Array

int queue[n], n = 5, front = - 1, rear = - 1;


Empty Queue Implementation using Array
int queue[n], n = 5, front = - 1, rear = - 1;
bool isempty()
{
if(front == -1 || front > rear)
print “Underflow”;
return true;
else
return false;
}
Implementation of Queue using Array

int queue[100], n = 100, front = - 1, rear = - 1;


bool isempty ()

bool isempty() {
if (front == - 1 || front > rear)
{
{
if(front == -1 || front > rear) cout<<"Queue Underflow ";
print “Underflow”; return 1;
return true; }

else else {
return 0;
return false;
}
}
isFull Queue Implementation using Array

Insertion always
happens from
Rear

int queue[100], n = 100, front = - 1, rear = - 1;


bool isFull() bool isFull()
{ {

if(rear == MAXSIZE - 1) if (rear == n - 1) {

print “ Overflow”; cout<<"Queue Overflow"<<endl;


return 1;
return true; }
else else
return false; return 0;

} }
enQueue Implementation using Array

Steps for enqueue int queue[100], n = 100, front = - 1, rear = - 1;


1. Check the queue is full or not int enqueue(int data)

2. If full, print overflow and exit {


if (isFull())
3. If queue is not full, increment
return 0;
rear/tail and add the element else {
rear++;
queue[rear] = data;
if (front == - 1)
front = 0;
return 1;
}
}
Implementation of Queue using Array

Steps for dequeue int queue[100], n = 100, front = - 1, rear = - 1;


1. Check queue is empty or not int dequeue() {
2. if empty, print underflow and exit if (isEmpty())

3. if not empty, print element at the return 0 ;


else
head and increment head
{
cout<<"Element deleted from queue is : "<< queue[front] <<endl;
front++;
return 1;

}
}
Assessment Time

Q: If the elements “A”, “B”, “C” and “D” are placed in a


queue and are deleted one at a time, in what order will they
be removed?
a) ABCD
b) DCBA
c) DCAB
d) ABDC

Correct Answer is A
Time Complexity of Queue Operations

Operations Complexity

Enqueue(insertion) O(1)

Deque(deletion) O(1)

Front(Get front) O(1)

Rear(Get Rear) O(1)

IsFull(Check queue is full or not) O(1)


IsEmpty(Check queue is empty or not) O(1)
Assessment Time

Q. What is the value of Front and Rear after following


Queue Operations: enqueue(10);
enqueue(20); dequeue(); enqueue(30);
enqueue(40); dequeue();
Output:
A. Front is 10 and Rear is 40
B. Front is 30 and Rear is 10
C. Front is 30 and Rear is 40
D. Front is 10 and Rear is 30
Correct Answer is C
Assessment Time

Q. A normal queue, if implemented using an array

of size MAX_SIZE, gets full, what is the condition?

A. Rear = MAX_SIZE – 1

B. Rear = MAX_SIZE – 1 && (Front==0)

C. Front = rear + 1

D. Rear = MAX_SIZE- front

Correct Answer is A
Problems with Simple Queue

Insertion always
happens from Rear

If the queue has a large number of enqueue and dequeue operations, at some point, we
may not be able to insert elements in the queue even if the queue is empty.
Solution is: Circular Queue

• Circular Queue is a linear data structure in which


the operations are performed based on FIFO
principle and the last position is connected back
to the first position to make a circle.

• It is also called ‘Ring Buffer’.


Some Applications of Circular Queue

Memory Management: The unused memory locations in the


case of ordinary queues can be utilized in circular queues.

Traffic system: In a computer-controlled traffic system,


circular queues are used to switch on the traffic lights one by
one repeatedly as per the time set.

CPU Scheduling: Operating systems often maintain a queue


of processes that are ready to execute or that are waiting for a
particular event to occur.
Implementation of Circular Queue using Array
Assessment Time

Q. In Circular Queue, if implemented using an array

of size MAX_SIZE, gets full, what is the condition?

A. Rear = MAX_SIZE – 1

B. Rear = MAX_SIZE – 1 && (Front==0)

C. Front = rear + 1

D. Rear = MAX_SIZE- front

Correct Answer is B, C
Enqueue Implementation of Circular Queue
int c_queue[100], n = 100, front = - 1, rear = - 1;
Enqueue Operation int enqueue(int data)
i. check if the queue is full {
ii. for the first element, set value of Front to 0 if (isFull())
iii. circularly increase the rear index by 1 (i.e. if the rear reaches return 0;
the end, next it would be at the start of the queue) else {

iv. add the new element in the position pointed to by REAR if (rear == Max_Size- 1)
rear = 0;
Rear is at End else

If( rear==Max_Size-1) rear++; //Normal Increment


rear=0; //Start of the Queue c_queue[rear] = data;
else if (front == - 1)
rear++; //Normal Increment front = 0;
return 1; } }
Enqueue Implementation of Circular Queue

if (rear == Max_Size- 1)
Circular Increment
rear = 0; rear = (rear + 1)%Max_Size
else
rear++; //Normal Increment
Circular Increment

Max_Size= 5
rear = (rear + 1)%5 = 0 (start of queue)
Enqueue Implementation of Circular Queue

Enqueue Operation int c_queue[100], n = 100, front = - 1, rear = - 1;


i. check if the queue is full int enqueue(int data)
ii. for the first element, set value of Front to 0 {

iii. circularly increase the rear index by 1 (i.e. if the rear reaches if (isFull())
the end, next it would be at the start of the queue) return 0;
else {
iv. add the new element in the position pointed to by REAR
rear= (rear+1)%Max_Size;
c_queue[rear] = data;
Circular Increment if (front == - 1) front = 0;
return 1; }
rear = (rear + 1)%Max_Size = 0 (start of queue)
}
Dequeue Implementation of Circular Queue

Dequeue Operation int queue[100], Max_Size = 100, front = - 1, rear = - 1;

i. check if the queue is empty int deQueue() {


int element;
ii. return the value pointed by front
if (isEmpty()) {
iii. circularly increase the front index by 1
cout << "Queue is empty" << endl;
iv. for the last element, reset the values of front and rear to -1 return -1; }
else {
if (front == rear) {
element = items[front];
front = -1; rear = -1; }
if (front == rear) {
else {
front = -1; rear = -1; }
if (front == Max_Size- 1)
else {
front = 0;
front = (front + 1) % Max_Size; }
else
return (element);
front++; } //Normal Increment
Circular Increment : front = (front + 1)%Max_Size }
Different Types of Queue

• Simple Queue

• Circular Queue

• Double Ended Queue (Deque)

• Priority Queue
Double Ended Queue (Deque)

• Deque is a linear data structure where the insertion and deletion operations are

performed from both ends. It will not follow the FIFO rule.

• We can say that deque is a generalized version of the queue.


Double Ended Queue (Deque)

1. Input Restricted Dequeue

2. Output Restricted Dequeue

• Insertion operation can be performed at only one


end, while deletion can be performed from both
ends.

• Deletion operation can be performed at only one


end, while insertion can be performed from both
ends.
Priority Queue
• A priority queue is a special type of queue in which each
element is associated with a priority value and, elements are
served on the basis of their priority (higher priority elements
are served first).

• If elements with the same priority occur, they are served


according to their order in the queue.

• The element with the highest value is considered the highest


priority element but we may also assume the element with the
lowest value as the highest priority element (we can also set
priorities according to our needs).
Assessment Time

Q. What is the value of Front and Rear after following


Circular Queue Operations (Max_Size =3): enqueue(10);
enqueue(20); enqueue(30); dequeue(); dequeue();
enqueue(40); enqueue(50);
Output:
A. Front is 10 and Rear is 40
B. Front is 30 and Rear is 50
C. Front is 20 and Rear is 40
D. Front is 10 and Rear is 30
Correct Answer is B
Any Queries?
Office MCub311
Discussion Time: 3-5 PM
Mob: +91-8586968801

You might also like