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

DS3 Queue

A Queue Data Structure is a linear data structure that operates on the FIFO principle, where the first element added is the first to be removed. It includes basic terminologies such as front, rear, size, and capacity, and supports operations like enqueue, dequeue, peek, and checks for empty or full states. Queues have various applications in computer systems, including task management, buffering, and algorithm techniques.

Uploaded by

kkr795707
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)
16 views25 pages

DS3 Queue

A Queue Data Structure is a linear data structure that operates on the FIFO principle, where the first element added is the first to be removed. It includes basic terminologies such as front, rear, size, and capacity, and supports operations like enqueue, dequeue, peek, and checks for empty or full states. Queues have various applications in computer systems, including task management, buffering, and algorithm techniques.

Uploaded by

kkr795707
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/ 25

Queue Data Structure

• A Queue Data Structure is a fundamental concept in computer science used


for storing and managing data in a specific order. It follows the principle of
"First in, First out" (FIFO), where the first element added to the queue is the
first one to be removed. Queues are commonly used in various algorithms
and applications for their simplicity and efficiency in managing data flow.

• Queue is a linear data structure that follows FIFO (First In First Out)
Principle, so the first element inserted is the first to be popped out.

1
Queue Data Structure
FIFO Principle in Queue:
• FIFO Principle states that the first element added
to the Queue will be the first one to be removed
or processed. So, Queue is like a line of people
waiting to purchase tickets, where the first person
in line is the first person served. (i.e. First Come
First Serve).

2
Queue Data Structure
Basic Terminologies of Queue
• Front: The position of the entry in a queue ready to be served, that
is, the first entry that will be removed from the queue, is called
the front of the queue. It is also referred to as the head of the
queue.
• Rear: The position of the last entry in the queue, that is, the one
most recently added, is called the rear of the queue. It is also
referred to as the tail of the queue.
• Size: Size refers to the current number of elements in the queue.
• Capacity: Capacity refers to the maximum number of elements the
queue can hold.
3
Queue Data Structure

4
Operations on Queue
1. Enqueue:
• Enqueue operation adds (or stores) an element to the end
of the queue.
Steps:
• Check if the queue is full. If so, return an overflow error
and exit.
• If the queue is not full, increment the rear pointer to the
next available position.
• Insert the element at the rear.
• If front pointer equal -1 the front=0.
5
Operations on Queue
2. Dequeue:
• Dequeue operation removes the element at the front of
the queue. The following steps are taken to perform
the dequeue operation:
• Check if the queue is empty. If so, return
an underflow error.
• Remove the element at the front.
• If front equal rear then front=rear=-1 else
increment the front pointer to the next element.
6
Operations on Queue

7
Operations on Queue
3. Peek or Front Operation:
• This operation returns the element at the front end without removing it (element
pointed by rear pointer).

4. Size Operation:
• This operation returns the number of elements present in the queue.

5. isEmpty Operation:
• This operation returns a Boolean value that indicates whether the queue is empty
or not, queue is empty if front = -1.

6. isFull Operation:
• This operation returns a Boolean value that indicates whether the queue is full or
not, queue is full if rear = size -1.
8
Types of Queues
Queue data structure can be classified into 4 types:
• Simple Queue: Simple Queue simply follows FIFO Structure.
We can only insert the element at the back and remove the
element from the front of the queue.
• Circular Queue: This is a special type of queue where the
last position is connected back to the first position. Here
also the operations are performed in FIFO order

9
Applications of Queue Data Structure
• Application of queue is common. In a computer system, there may be
queues of tasks waiting for the printer, for access to disk storage, or even in
a time-sharing system, for use of the CPU. Within a single program, there
may be multiple requests to be kept in a queue, or one task may create
other tasks, which must be done in turn by keeping them in a queue.
• A Queue is always used as a buffer when we have a speed mismatch
between a producer and a consumer. For example, keyboard and CPU.
• Queue can be used where we have a single resource and multiple
consumers like a single CPU and multiple processes.
• In a network, a queue is used in devices such as a router/switch and mail
queue.
• Queue can be used in various algorithm techniques like Breadth First
Search, Topological Sort, etc.
10
Implementation of queue operations in C++
#include <iostream>
using namespace std;
const int size=10;
int queue[size], front = - 1, rear = - 1;
void Insert() {
int val;
if (rear == size - 1)
cout<<"Queue Overflow"<<endl;
else {

cout<<"Insert the element in queue : "<<endl;


cin>>val;
rear++;
queue[rear] = val;
if (front == - 1) front = 0;

}
}
11
Implementation of queue operations in C++
void Delete() {
if (front == - 1 ) {
cout<<"Queue Underflow "<<endl;
return ;
} else {
cout<<"Element deleted from queue is : "
<< queue[front] <<endl;
if ( front == rear )
{ front = -1;
rear = -1;
}
else front++;;
}
}
12
Implementation of queue operations in C++
void Display() {
if (front == - 1)
cout<<"Queue is empty"<<endl;
else {
cout<<"Queue elements are : ";
for (int i = front; i <= rear; i++)
cout<<queue[i]<<" ";
cout<<endl;
}
}
13
Implementation of queue operations in C++
int main() {
int ch;

do {
cout<<"1) Insert element to queue"<<endl;
cout<<"2) Delete element from queue"<<endl;
cout<<"3) Display all elements of queue"<<endl;
cout<<"4) Exit"<<endl;
cout<<"Enter your choice : ";
cin>>ch;
14
Implementation of queue operations in C++
switch (ch) {
case 1: Insert();
break;
case 2: Delete();
break;
case 3: Display();
break;
case 4: cout<<"Exit"<<endl;
break;
default: cout<<"Invalid choice"<<endl;
}
} while(ch!=4);
return 0;
}
15
Operations on Circular Queue

16
Operations on Circular Queue
1. Enqueue:
• Enqueue operation adds (or stores) an element to the end
of the Cqueue.
• Steps:
• Check if the Cqueue is full. If so, return an overflow error
and exit.
• If the Cqueue is not full, if rear equal size-1 then rear equal
zero else increment the rear pointer to the next available
position.
• Insert the element at the rear.
• If front pointer equal -1 the front=0.
17
Operations on Circular Queue
2. Dequeue:
• Dequeue operation removes the element at the front of
the Cqueue. The following steps are taken to perform
the dequeue operation:
• Check if the Cqueue is empty. If so, return an
underflow error.
• Remove the element at the front.
• If front pointer equal rear pointer then front=rear=
-1 else increment the front pointer to the next
element.
18
Operations on Circular Queue
3. Peek or Front Operation:
• This operation returns the element at the front end without removing it
(element pointed by rear pointer).

4. Size Operation:
• This operation returns the number of elements present in the Cqueue.

5. isEmpty Operation:
• This operation returns a Boolean value that indicates whether the Cqueue is
empty or not, (Cqueue is empty if front = -1).

6. isFull Operation:
• This operation returns a Boolean value that indicates whether the Cqueue is
full or not, (Cqueue is full if (rear = front -1) or (front=0 and rear=size-1)).
19
Implementation of circular queue operations
#include <iostream>
using namespace std;
const int size=6;
int queue[size], front = - 1, rear = - 1;

void CQInsert() {
int val;
if (((rear == size - 1 ) && (front ==0))|| front == rear + 1)
cout<<"Queue Overflow"<<endl;
else {
cout<<"Insert the element in queue : ";
cin>>val;
if ( rear == size-1 ) rear = 0;
else rear++;
queue[rear] = val;
if (front == - 1) front = 0;
}
}

20
Implementation of circular queue operations
void CQDelete() {
if (front == - 1 ) {
cout<<"Queue Underflow "<<endl;
return ;
} else {
cout<<"Element deleted from queue is : "
<< queue[front] <<endl;
if ( front == rear )
{ front = -1;
rear = -1;
}
else
if ( front == size - 1 ) front =0;
else front++;;
}
}
21
Implementation of circular queue operations
void Display()
{
if (front == - 1)
cout<<"Queue is empty"<<endl;
else {
cout<<"Queue elements are : ";
if ( rear >= front )
for (int i = front; i <= rear; i++)
cout<<queue[i]<<" ";
else
{
for (int i = front; i <= size-1; i++)
cout<<queue[i]<<" ";
for (int i = 0; i <= rear; i++)
cout<<queue[i]<<" ";
}
}
cout<<endl;
}
22
Implementation of circular queue operations
int main() {
int ch;

do {
cout<<"1) Insert element to queue"<<endl;
cout<<"2) Delete element from queue"<<endl;
cout<<"3) Display all elements of queue"<<endl;
cout<<"4) Exit"<<endl;
cout<<"Enter your choice : ";
cin>>ch;
23
Implementation of circular queue operations
switch (ch) {
case 1: CQInsert();
break;
case 2: CQDelete();
break;
case 3: Display();
break;
case 4: cout<<"Exit"<<endl;
break;
default: cout<<"Invalid choice"<<endl;
}
} while(ch!=4);
return 0;
}
24
25

You might also like