0% found this document useful (0 votes)
11 views5 pages

4.0 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)
11 views5 pages

4.0 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
You are on page 1/ 5

What is Queue Data Structure?

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.

1. A queue can be defined as an ordered list which enables insert operations to be performed at one end
called REAR and delete operations to be performed at another end called FRONT.

2. Queue is referred to be as First In First Out list.

3. For example, people waiting in line for a rail ticket form a queue.

Applications of Queue

Due to the fact that queue performs actions on first in first out basis which is quite fair for the ordering of
actions. There are various applications of queues discussed as below.

1. Queues are widely used as waiting lists for a single shared resource like printer, disk, CPU.
2. Queues are used as buffers in most of the applications like MP3 media player, CD player, etc.
3. Queues are used in operating systems for handling interrupts.

Basic Operations

Queue operations also include initialization of a queue, usage and permanently deleting the data from the
memory.

The most fundamental operations in the queue ADT include: enqueue(), dequeue(),isFull(), isEmpty(). These
are all built-in operations to carry out data manipulation and to check the status of the queue.

Queue uses two pointers − front and rear. The front pointer accesses the data from the front end (helping in
enqueueing) while the rear pointer accesses data from the rear end (helping in dequeuing).

1. Insertion operation: enqueue()

The enqueue() is a data manipulation operation that is used to insert elements into the stack. The following
algorithm describes the enqueue() operation in a simpler way.
Algorithm
1 − START
2 – Check if the queue is full.
3 − If the queue is full, produce overflow error and exit.
4 − If the queue is not full, increment rear pointer to point the next empty space.
5 − Add data element to the queue location, where the rear is pointing.
6 – END

2. Deletion Operation: dequeue()


The dequeue() is a data manipulation operation that is used to remove elements from the stack. The following
algorithm describes the dequeue() operation in a simpler way.

Algorithm
1 – START
2 − Check if the queue is empty.
3 − If the queue is empty, produce underflow error and exit.
4 − If the queue is not empty, access the data where front is pointing.
5 − Increment front pointer to point to the next available data element.
6 − Return success.
7 – END

Program having both enqueue and dequeue operations


#include <iostream>
#include <queue>

int main() {
// Create a queue of integers
std::queue<int> myQueue;

// Push elements into the queue


myQueue.push(10);
myQueue.push(20);
myQueue.push(30);
myQueue.push(40);

// Display the elements in the queue


std::cout << "Elements in the queue after pushing: ";
while (!myQueue.empty()) {
std::cout << myQueue.front() << " ";
myQueue.pop();
}
return 0;
}

3. The isFull() Operation

The isFull() operation verifies whether the stack is full.

Algorithm
1 – START
2 – If the count of queue elements equals the queue size, return true
3 – Otherwise, return false
4 – END

Program of isfull() operation :

#include <iostream>
#include <queue>

const int MAX_SIZE = 5; // Maximum size of the queue

bool isFull(const std::queue<int>& myQueue) {


return myQueue.size() == MAX_SIZE;
}

int main() {
std::queue<int> myQueue;

// Enqueue some elements


myQueue.push(10);
myQueue.push(20);
myQueue.push(30);

// Check if the queue is full


if (isFull(myQueue)) {
std::cout << "Queue is full." << std::endl;
} else {
std::cout << "Queue is not full." << std::endl;
}
// Enqueue more elements to reach the maximum size
myQueue.push(40);
myQueue.push(50);

// Check again if the queue is full


if (isFull(myQueue)) {
std::cout << "Queue is full." << std::endl;
} else {
std::cout << "Queue is not full." << std::endl;
}

return 0;
}

4. The isEmpty() operation

The isEmpty() operation verifies whether the stack is empty. This operation is used to check the status of the
stack with the help of top pointer.

Algorithm
1 – START
2 – If the count of queue elements equals zero, return true
3 – Otherwise, return false
4 – END

Program of isempty() operation:


#include <iostream>
#include <queue>

bool isEmpty(const std::queue<int>& myQueue) {


return myQueue.empty();
}

int main() {
std::queue<int> myQueue;

// Check if the queue is empty


if (isEmpty(myQueue)) {
std::cout << "Queue is empty." << std::endl;
} else {
std::cout << "Queue is not empty." << std::endl;
}

// Enqueue some elements


myQueue.push(10);
myQueue.push(20);
myQueue.push(30);

// Check again if the queue is empty


if (isEmpty(myQueue)) {
std::cout << "Queue is empty." << std::endl;
} else {
std::cout << "Queue is not empty." << std::endl;
}

return 0;
}

You might also like