0% found this document useful (0 votes)
44 views14 pages

Queue and Dequeue Algorithms Explained

The document provides an overview of queue algorithms, specifically focusing on the enqueue and dequeue operations within a queue data structure. It defines a queue as a linear structure that follows a First In, First Out (FIFO) principle and outlines the main operations such as adding and removing elements. Additionally, it introduces the concept of a dequeue, which allows insertion and deletion from both ends, enhancing flexibility compared to a standard queue.
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)
44 views14 pages

Queue and Dequeue Algorithms Explained

The document provides an overview of queue algorithms, specifically focusing on the enqueue and dequeue operations within a queue data structure. It defines a queue as a linear structure that follows a First In, First Out (FIFO) principle and outlines the main operations such as adding and removing elements. Additionally, it introduces the concept of a dequeue, which allows insertion and deletion from both ends, enhancing flexibility compared to a standard queue.
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

Data Structure

AI-414
4(3-1)

M. Mudassir Riaz
Department of Computer Sciences
The University of Faisalabad (TUF)
[Link]
unioffaisalabad

Today lecture topics:


▪ Queue Algorithms
▪ Enqueue

▪ Dequeue
[Link]
unioffaisalabad

Queue Definition
• A Queue is a linear data structure that stores elements in a First
In, First Out (FIFO) order.
This means the element that is inserted first will be removed
first.
• Example:
Just like a line of people waiting for tickets — the first person in
the line is served first, and the last person is served last.
[Link]
unioffaisalabad

Main Operations:
•Enqueue: Add an element at the end (rear).
•Dequeue: Remove an element from the front.
•Front: Shows the first element.
•Rear: Shows the last element.
[Link]
unioffaisalabad

Enqueue
• Enqueue means inserting (adding) an element at the end (rear) of the
queue.
It follows the FIFO (First In, First Out) rule — new elements always go
to the back of the queue.
Enqueue Working Principle
• The Enqueue operation works on the FIFO (First In, First Out) principle.
This means the first element inserted into the queue will be the first
one removed later.
In enqueue, we always add a new element at the rear (end) of the
queue.
[Link]
unioffaisalabad

Steps:
[Link] if the queue is full
•If rear == MAX - 1, then
•print “Queue Overflow” (no more space).
[Link] the queue is empty
•Set front = 0 (because we are adding the first element).
[Link] rear forward
•Increase rear by 1 → rear = rear + 1
[Link] the new element
•Place the item at queue[rear]
[Link] a message
•Print “Element added successfully”
[Link]
unioffaisalabad

void display() {
#include <iostream>
if (front == -1)
using namespace std;
cout << "Queue is empty!" << endl;
#define MAX 5 // maximum size of queue
else {
class Queue { cout << "Queue elements: ";
int arr[MAX]; for (int i = front; i <= rear; i++)
int front, rear; cout << arr[i] << " ";
public:
cout << endl;
}
Queue() {
}
front = -1;
};
rear = -1;

} int main() {
// Function to add (enqueue) an element Queue q;
void enqueue(int item) {
[Link](10);
if (rear == MAX - 1) {
[Link](20);
cout << "Queue Overflow! Cannot add more elements." << endl;
[Link](30);
} [Link]();
else {

if (front == -1) // first element [Link](40);


front = 0;
[Link](50);
[Link]();
rear = rear + 1;

arr[rear] = item;
// Trying to add one more element (will show overflow)
cout << item << " added to the queue." << endl; [Link](60);
}

} return 0;
}
[Link]
unioffaisalabad

Dequeue:
• A Dequeue (Double Ended Queue) is a special type of queue in which
insertion and deletion operations can be performed at both ends —
front and rear.
It is a more flexible form of a normal queue.
[Link]
unioffaisalabad

Working Principle of Dequeue:


• A dequeue allows two-way data handling, unlike a normal queue
(which is FIFO — First In First Out).
• You can insert (enqueue) or delete (dequeue) elements from:
• The front end, and
• The rear end.
• This makes it possible to use a dequeue as:
• A Queue (FIFO), or
• A Stack (LIFO), depending on how you use it.
[Link]
unioffaisalabad

Working Example (Step-by-Step):


Let’s take an empty dequeue.
Initial: [ ]
[Link] 10 at Rear → [10]
[Link] 20 at Rear → [10, 20]
[Link] 5 at Front → [5, 10, 20]
[Link] from Rear → [5, 10]
[Link] from Front → [10]
[Link]
unioffaisalabad

Algorithm of Dequeue
Step 1: Start
Step 2: Create an empty array (or list) called dequeue
Step 3:
You can perform the following operations:
A. Insert at Rear
•Check if the dequeue is full
•If not full → add element at the rear end
B. Insert at Front
•Check if the dequeue is full
•If not full → add element at the front end
C. Delete from Front
•Check if the dequeue is empty
•If not empty → remove element from the front end
D. Delete from Rear
•Check if the dequeue is empty
•If not empty → remove element from the rear end
Step 4: Display all elements of the dequeue
Step 5: Stop
[Link]
unioffaisalabad

if (front == -1) {
#include <iostream> front = rear = 0;
} else if (front == 0) {
using namespace std;
front = SIZE - 1;
#define SIZE 5 } else {
class Dequeue { front--;
int arr[SIZE]; }
arr[front] = value;
int front, rear; }
public: // Insert at rear
Dequeue() { void insertRear(int value) {
if ((front == 0 && rear == SIZE - 1) || (front == rear + 1))
front = -1;
{
rear = -1; cout << "Dequeue is Full\n";
} return;
// Insert at front }
if (front == -1) {
void insertFront(int value) { front = rear = 0;
if ((front == 0 && rear == SIZE - 1) || (front == rear + 1)) } else if (rear == SIZE - 1) {
{ rear = 0;
} else {
cout << "Dequeue is Full\n";
rear++;
return; }
} arr[rear] = value;
}
[Link]
unioffaisalabad

// Delete from front


// Display elements
void deleteFront() {
void display() {
if (front == -1) {
if (front == -1) {
cout << "Dequeue is Empty\n";
cout << "Dequeue is Empty\n";
return;
return;
}
}
cout << "Deleted: " << arr[front] << endl;
cout << "Dequeue Elements: ";
if (front == rear) {
int i = front;
front = rear = -1;
while (true) {
} else if (front == SIZE - 1) {
cout << arr[i] << " ";
front = 0;
if (i == rear)
} else {
break;
front++;
i = (i + 1) % SIZE;
}
}
}
cout << endl;
// Delete from rear
}
void deleteRear() {
};
if (rear == -1) {
cout << "Dequeue is Empty\n";
int main() {
return;
Dequeue dq;
}
[Link](10);
cout << "Deleted: " << arr[rear] << endl;
[Link](20);
if (front == rear) {
[Link](5);
front = rear = -1;
[Link]();
} else if (rear == 0) {
[Link]();
rear = SIZE - 1;
[Link]();
} else {
[Link]();
rear--;
[Link]();
}
return 0;
}
}
[Link]
unioffaisalabad

Any Question ?

You might also like