0% found this document useful (0 votes)
9 views

Queue Notes

A queue is a data structure that follows the First In First Out (FIFO) principle, where insertion occurs at the rear and deletion at the front. It has various operations such as enqueue, dequeue, peek, isFull, and isEmpty, all with O(1) time complexity. There are different types of queues including simple queues, circular queues, priority queues, and double-ended queues (dequeues).

Uploaded by

sahilboora946
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Queue Notes

A queue is a data structure that follows the First In First Out (FIFO) principle, where insertion occurs at the rear and deletion at the front. It has various operations such as enqueue, dequeue, peek, isFull, and isEmpty, all with O(1) time complexity. There are different types of queues including simple queues, circular queues, priority queues, and double-ended queues (dequeues).

Uploaded by

sahilboora946
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

What is a Queue in Data Structures?

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.

Complexity Analysis of Operations on Queue

Operation
s Time Complexity Space Complexity

enqueue O(1) O(1)

dequeue O(1) O(1)

front O(1) O(1)

size O(1) O(1)

isEmpty O(1) O(1)


Operation
s Time Complexity Space Complexity

isFull O(1) O(1)

Operations Of Queue in Data Structures


1. Insertion: enqueue()

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 2: Check if the queue is full.

Step 3: If the queue is full, produce an overflow error and exit.

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.

Step 6: End the process and exit.

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 2: Check if the queue is empty.

Step 3: If the queue is empty, print underflow and exit.

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.

Step 7: End the process and exit.

3. peek()

The peek()The operation returns the value at the front end of the queue without removing it.

Algorithm
Step 1: START

Step 2: Check if the Queue is empty.

Step 3: Return the element at the front of the queue

Step 4: End the process and exit.

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.

Step 3: Otherwise, return false

Step 4: End the process and exit.

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

Step 2: If the count of queue elements equals zero, return true.

Step 3: Otherwise, return false

Step 4: End the process and exit.

Queue operations work as follows:

 Two pointers are there denoting two ends, FRONT and REAR.

 FRONT Tracks the first element of the queue.

 REAR Tracks the last element of the queue.

 Initially, set the value of FRONT and REAR to -1.

Types of Queues in Data Structures

There are 4 types of queues in data structures:

1. Simple or Linear Queue

 As we mentioned above, a Simple queue follows the First-In-First-Out (FIFO) principle.

 In this, Items are taken out of the front and put in the back.

 It is necessary to remove outdated elements in order to add new ones.

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.

 This allows for efficient use of memory.

 It is also known as a Ring Buffer.

3. Priority Queue in Data Structure

 It is a special type of queue in which each element has a priority assigned to it.

 The element with the highest priority is removed first.

 This is useful in situations where certain elements need to be processed before others.

4. Dequeue (Double-Ended Queue)

 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.

 It provides flexibility in managing data with operations from both ends.

Program
Write a program to create a queue and use the operations of queue in it:

#include <iostream>

#define MAX_SIZE 5

using namespace std;

class Queue {

private:

int myqueue[MAX_SIZE], front, rear;

public:

Queue(){

front = -1;

rear = -1;

bool isFull(){

if(front == 0 && rear == MAX_SIZE - 1){

return true;

return false;

}
bool isEmpty(){

if(front == -1) return true;

else return false;

void enQueue(int value){

if(isFull()){

cout << endl<< "Queue is full!!";

} else {

if(front == -1) front = 0;

rear++;

myqueue[rear] = value;

cout << value << " ";

int deQueue()

int value;

if(isEmpty()){

cout << "Queue is empty!!" << endl; return(-1);

else {

value = myqueue[front];

if(front >= rear){ //only one element in queue

front = -1;

rear = -1;

else {

front++;

cout << endl << "Deleted => " << value << " from myqueue";

}
return(value);

void displayQueue()

int i;

if(isEmpty()) {

cout << endl << "Queue is Empty!!" << endl;

else {

cout << endl << "Front = " << front;

cout << endl << "Queue elements : ";

for(i=front; i<=rear; i++)

cout << myqueue[i] << "\t";

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(50); //enqueue 60 => queue is full

myq.enQueue(60);

myq.displayQueue();

//deQueue =>removes 10

myq.deQueue();
//queue after dequeue

myq.displayQueue();

return 0;

Queue using linked list:-

#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;

// If front becomes NULL, update rear also


if (front == nullptr) {
rear = nullptr;
}

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";
}

// Check if queue is empty


bool isEmpty() {
return front == nullptr;
}
};

int main() {
Queue q;
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);

q.display(); // 10 -> 20 -> 30 -> NULL

q.dequeue();
q.display(); // 20 -> 30 -> NULL

cout << "Front element: " << q.peek() << endl;


return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy