4.0 queue
4.0 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.
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.
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).
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
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
int main() {
// Create a queue of integers
std::queue<int> myQueue;
Algorithm
1 – START
2 – If the count of queue elements equals the queue size, return true
3 – Otherwise, return false
4 – END
#include <iostream>
#include <queue>
int main() {
std::queue<int> myQueue;
return 0;
}
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
int main() {
std::queue<int> myQueue;
return 0;
}