hello everyone
Today sir gave us a program: “Insert and Delete operations on a Circular Queue using Linked List.” And honestly, the moment he said circular, all three of us had the same reaction: Flashbacks of life going in circles for no reason.
Scene 1: Rahul’s Philosophy Lecture Nobody Asked For
As soon as sir explained that the last node points back to the first, Rahul whispered: “Exactly like our life. Problems end and come back again. Assignments? Circular. Tests? Circular. Aarav’s stupidity? MOST circular.” Aarav looked offended. Then accepted it. Then nodded proudly. Me (Daksh)? Opening my notebook like: “Okay this is definitely going into the blog.”
What Even Is a Circular Queue Using Linked List?
Our DSU gang decoded it:
-
Every node = data + pointer
But this time… the rear node points back to the front node
-
INSERT happens at rear
-
DELETE happens at front
-
And the whole thing loops like a ferris wheel nobody asked to ride
frontNode - first person entering the ride, rearNode - last person sitting, rearNode - next - back to frontNode - friendship circle but more stable Aarav called it: "Queue that never ends. Like my mother’s lecture.” We pretended not to hear that.
Division of Responsibilities (aka “Who Ruins What”)
Rahul – The Rear Gatekeeper AGAIN
He handled the INSERT operation. Every time he added a node, he proudly reminded us: “Don’t forget to point it back to the front. Connection matters.” Bro became emotional while inserting.
Aarav – The Front Bouncer AGAIN
His job: DELETE from front. His reality: “Can I delete from rear too? Please?” Rahul: “NO BRO THIS IS CIRCULAR QUEUE NOT CIRCULAR NONSENSE.” Aarav: “Then why circle? Why complicate?” Rahul lost 3 brain cells that day.
Me (Daksh) – Display + Therapist
I printed the queue after each operation. But every time, someone asked: “Front where? Rear where? Why is it looping?” And I had to explain: “Its just pointer loop, dont put your brain in loops”
Why This Whole Circular Drama Happens?
Because Circular Queues are: Memory-efficient, Perfect for continuous looping tasks, No wasted space, No array size limits, No NULL at the end (instead friendship circle pointer), And great for situations where you don’t know how many inserts will come
Basically perfect for: operating systems, CPU scheduling, And handling friends like Aarav who keep coming back even after being deleted from the queue 3 times.
Code:
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h>
struct Node { int data; struct Node *next; };
struct Node *front = NULL; struct Node *rear = NULL;
void insertElement(int value) { struct Node *newNode = (struct Node *)malloc(sizeof(struct Node)); if (newNode == NULL) { printf("Memory not available. Insertion failed.\n"); return; } newNode->data = value; if (front == NULL) { front = newNode; rear = newNode; newNode->next = front; // circular link } else { rear->next = newNode; rear = newNode; rear->next = front; } printf("Inserted %d successfully.\n", value); }
void deleteElement() { if (front == NULL) { printf("Queue Underflow! No elements to delete.\n"); return; } struct Node *temp = front; int deletedValue = temp->data;
if (front == rear) { // only one element front = rear = NULL; } else { front = front->next; rear->next = front; } free(temp); printf("Deleted %d successfully.\n", deletedValue); }
void displayQueue() { if (front == NULL) { printf("Queue is empty.\n"); return; } struct Node *temp = front; printf("Circular Queue elements are: "); do { printf("%d ", temp->data); temp = temp->next; } while (temp != front); printf("\n"); }
int main() { int choice, value; char continueChoice;
do { printf("\n--- Circular Queue Using Linked List ---\n"); printf("1. Insert Element\n"); printf("2. Delete Element\n"); printf("3. Display Queue\n"); printf("Enter your choice: "); scanf("%d", &choice);
switch (choice) { case 1: printf("Enter the value to insert: "); scanf("%d", &value); insertElement(value); break; case 2: deleteElement(); break; case 3: displayQueue(); break; default: printf("Invalid choice! Please try again.\n"); }
printf("Do you want to continue? (y/n): "); scanf(" %c", &continueChoice);
} while (continueChoice == 'y' || continueChoice == 'Y');
printf("Program exited. Goodbye!\n"); return 0; }
#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <stdlib.h>struct Node {int data;struct Node *next;};struct Node *front = NULL;struct Node *rear = NULL;void insertElement(int value) {struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));if (newNode == NULL) {printf("Memory not available. Insertion failed.\n");return;}newNode->data = value;if (front == NULL) {front = newNode;rear = newNode;newNode->next = front; // circular link} else {rear->next = newNode;rear = newNode;rear->next = front;}printf("Inserted %d successfully.\n", value);}void deleteElement() {if (front == NULL) {printf("Queue Underflow! No elements to delete.\n");return;}struct Node *temp = front;int deletedValue = temp->data;if (front == rear) { // only one elementfront = rear = NULL;} else {front = front->next;rear->next = front;}free(temp);printf("Deleted %d successfully.\n", deletedValue);}void displayQueue() {if (front == NULL) {printf("Queue is empty.\n");return;}struct Node *temp = front;printf("Circular Queue elements are: ");do {printf("%d ", temp->data);temp = temp->next;} while (temp != front);printf("\n");}int main() {int choice, value;char continueChoice;do {printf("\n--- Circular Queue Using Linked List ---\n");printf("1. Insert Element\n");printf("2. Delete Element\n");printf("3. Display Queue\n");printf("Enter your choice: ");scanf("%d", &choice);switch (choice) {case 1:printf("Enter the value to insert: ");scanf("%d", &value);insertElement(value);break;case 2:deleteElement();break;case 3:displayQueue();break;default:printf("Invalid choice! Please try again.\n");}printf("Do you want to continue? (y/n): ");scanf(" %c", &continueChoice);} while (continueChoice == 'y' || continueChoice == 'Y');printf("Program exited. Goodbye!\n");return 0;}
Output:
| ← Previous | 🏠 Homepage | Next Chapter → |
