Friday, March 31, 2023

Queues: Operations on Queue

 

📌 Queues: Operations on Queue

A Queue is a linear data structure that follows the FIFO (First-In, First-Out) principle. This means that the element added first will be removed first, just like a queue of people waiting in line.


1️⃣ Basic Queue Operations

OperationDescription
Enqueue()Adds an element to the rear (back) of the queue.
Dequeue()Removes an element from the front of the queue.
Front() / Peek()Returns the front element without removing it.
isEmpty()Checks if the queue is empty.
isFull()Checks if the queue is full (for fixed-size queues).

2️⃣ Implementation of Queue in C

(a) Queue Using Arrays


#include <stdio.h> #define MAX 5 // Maximum queue size int queue[MAX], front = -1, rear = -1; // Enqueue operation void enqueue(int value) { if (rear == MAX - 1) { printf("Queue Overflow!\n"); return; } if (front == -1) front = 0; // Set front to first element if empty queue[++rear] = value; printf("Enqueued: %d\n", value); } // Dequeue operation void dequeue() { if (front == -1 || front > rear) { printf("Queue Underflow!\n"); return; } printf("Dequeued: %d\n", queue[front++]); if (front > rear) front = rear = -1; // Reset when queue is empty } // Display the queue void display() { if (front == -1) { printf("Queue is empty!\n"); return; } printf("Queue: "); for (int i = front; i <= rear; i++) printf("%d ", queue[i]); printf("\n"); } int main() { enqueue(10); enqueue(20); enqueue(30); display(); dequeue(); display(); return 0; }

Simple Implementation
Wastes space when elements are dequeued (fixed front).


(b) Queue Using Linked List


#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; struct Node *front = NULL, *rear = NULL; // Enqueue operation void enqueue(int value) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = value; newNode->next = NULL; if (rear == NULL) front = rear = newNode; else { rear->next = newNode; rear = newNode; } printf("Enqueued: %d\n", value); } // Dequeue operation void dequeue() { if (front == NULL) { printf("Queue Underflow!\n"); return; } struct Node* temp = front; printf("Dequeued: %d\n", front->data); front = front->next; free(temp); if (front == NULL) rear = NULL; } // Display queue void display() { if (front == NULL) { printf("Queue is empty!\n"); return; } struct Node* temp = front; printf("Queue: "); while (temp != NULL) { printf("%d ", temp->data); temp = temp->next; } printf("\n"); } int main() { enqueue(10); enqueue(20); enqueue(30); display(); dequeue(); display(); return 0; }

Efficient memory usage
No fixed size limit
More complex (pointers needed)


3️⃣ Special Types of Queues

1️⃣ Circular Queue – Overcomes the issue of wasted space in arrays.
2️⃣ Deque (Double-Ended Queue) – Can add/remove from both front & rear.
3️⃣ Priority Queue – Elements are dequeued based on priority, not order.

No comments:

Post a Comment

Complete Binary Tree in Data Structures

  Complete Binary Tree in Data Structures 🌳 A Complete Binary Tree (CBT) is a type of Binary Tree where: ✔ All levels except possibly t...