Queue Notes
Queue Notes
A queue is an ordered list in which insertion is done at one end called REAR and deletion at
another end called FRONT. The first inserted element is available first for the operations to
be performed and is the first one to be deleted. Hence, it is known as First In First
Out, FIFOor Last In Last Out, LILO.
Real-life examples of queues are a ticket queue outside a ticket counter, students standing in
a queue for assembly prayer on the school grounds, and a queue of persons standing
outside the booking counter of a theatre. In all these examples, the person standing first in
the line is the first one for access.
Operation
s Time Complexity Space Complexity
The enqueue() The operation inserts an element from the back of a queue to the end of a queue or
the rear end of the queue.
Algorithm
Step 1: START
Step 4: If the queue is not full, increment the rear pointer to point to the next space.
Step 5: Add a data element to the queue location, where the rear is pointing.
2. Deletion: dequeue()
The dequeue() Operation is used to remove and return the element from the front of a queue.
Algorithm
Step 1: START
Step 4: If the queue is not empty, access the data where the front is pointing.
Step 5: Increment the front pointer to point to the next available data element.
Step 6: Set the front and rear as -1 for the last element.
3. peek()
The peek()The operation returns the value at the front end of the queue without removing it.
Algorithm
Step 1: START
4. isFull()
The isFull() operation is used to determine if the queue is full or not. A queue is said to be full if it has
reached its maximum capacity and there is no more space to add new elements to it.
Algorithm
Step 1: START
Step 2: If the count of queue elements equals the queue size, return true.
5. isEmpty()
The isEmpty() operation is used to check if the queue is empty or not. It returns a boolean value, true
when the queue is empty, otherwise false.
Algorithm
Step 1: START
Two pointers are there denoting two ends, FRONT and REAR.
In this, Items are taken out of the front and put in the back.
2. Circular Queue
A circular queue is similar to a simple queue, but the last element is connected to the first
element which creates a circular structure.
It is a special type of queue in which each element has a priority assigned to it.
This is useful in situations where certain elements need to be processed before others.
In this, the elements can be added or removed from both endsfront and rear of the queue.
Dequeue allows insertion and deletion at both front and rear ends.
Program
Write a program to create a queue and use the operations of queue in it:
#include <iostream>
#define MAX_SIZE 5
class Queue {
private:
public:
Queue(){
front = -1;
rear = -1;
bool isFull(){
return true;
return false;
}
bool isEmpty(){
if(isFull()){
} else {
rear++;
myqueue[rear] = value;
int deQueue()
int value;
if(isEmpty()){
else {
value = myqueue[front];
front = -1;
rear = -1;
else {
front++;
cout << endl << "Deleted => " << value << " from myqueue";
}
return(value);
void displayQueue()
int i;
if(isEmpty()) {
else {
cout << endl << "Rear = " << rear << endl;
};
int main()
Queue myq;
myq.deQueue();
cout<<"Queue created:"<<endl;
myq.enQueue(10);
myq.enQueue(20);
myq.enQueue(30);
myq.enQueue(40);
myq.enQueue(60);
myq.displayQueue();
//deQueue =>removes 10
myq.deQueue();
//queue after dequeue
myq.displayQueue();
return 0;
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
};
class Queue {
private:
Node* front;
Node* rear;
public:
Queue() {
front = rear = nullptr;
}
// Enqueue operation
void enqueue(int value) {
Node* newNode = new Node();
newNode->data = value;
newNode->next = nullptr;
if (rear == nullptr) {
front = rear = newNode;
} else {
rear->next = newNode;
rear = newNode;
}
}
// Dequeue operation
void dequeue() {
if (front == nullptr) {
cout << "Queue Underflow\n";
return;
}
Node* temp = front;
front = front->next;
delete temp;
}
// Peek front
int peek() {
if (front == nullptr) {
cout << "Queue is empty\n";
return -1;
}
return front->data;
}
// Display queue
void display() {
Node* temp = front;
while (temp != nullptr) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL\n";
}
int main() {
Queue q;
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
q.dequeue();
q.display(); // 20 -> 30 -> NULL