30.10.25

24. Circular Queue Using Array The One Where Rahul Says “Dude, Stop Wasting Memory!”

     

hello everyone 

After surviving Linear Queue drama, today’s DS lecture felt like a plot twist. Sir said: “Students today we will learn circular queue.” And instantly Aarav shouted: “OH! Like a merry-go-round??” Rahul facepalmed so hard the sound echoed through the lab. I (Daksh) just quietly opened a new .c file. Because I already knew this was going to be a full episode.

Why Circular Queue? (DSU Friends Explanation)

Aarav’s Understanding: “Dude queue ends too fast… circular queue means it never ends . Like our assignments.” Not wrong. Not right. Somewhere in between.

Rahul’s Actual Explanation: “It’s just a queue where rear wraps back to the first index. Basically: NO wasted space, no shifting, no drama.”

My Summary: Circular Queue = You delete from front, space becomes empty, and rear can use that space again later. Aarav called it “recycling bin queue”.

How It Actually Works

  1. frontPosition - where deletion happens

  2. rearPosition - where insertion happens

  3. When rear reaches end, it jumps to index 0 (circle vibes)

  4. Overflow happens when queue is full

  5. Underflow happens when queue is empty

Rahul described it perfectly:

“Think of seats on a Ferris wheel. People exit at one point and new ones enter at the next cycle.”

Aarav:
“Ferris wheel?? So I’m the rear?”

Rahul:
“You’re the NULL pointer of the group.”

Me:
peacefully typing “case 1: insert”

Division of Roles (as Always)

Rahul – Insertion Machine

He controlled the rearPosition like it was his personal kingdom.
Every time he inserted he said: “Rear moved successfully.” Dude sounded like a bus conductor.

Aarav – Delete Button Crusher

His job was to delete. But he kept deleting even when the queue was empty. Error Output: “Underflow.” Lab Output: “Aarav stop pressing delete.”

Me (Daksh) – Display Guy + Story Writer

I printed the queue in a neat loop. Circular queues look clean unlike our lab benches.

Why Circular Queue Feels “Smarter”

  • No wasted space after deletion

  • Perfect use of array

  • No shifting needed

  • Efficient for continuous operations

  • And most importantly…
    It prevents Aarav from claiming “DUDE QUEUE FULL?” after only 5 operations

Code:

 #define _CRT_SECURE_NO_WARNINGS
 #include <stdio.h>
 #define MAXIMUM_SIZE 5

 int queue[MAXIMUM_SIZE];
 int frontPosition = -1, rearPosition = -1;

 void insertElement(int value) {
     if ((frontPosition == 0 && rearPosition == MAXIMUM_SIZE - 1) || (rearPosition + 1 == frontPosition)) {
         printf("Queue Overflow! Cannot insert more elements.\n");
     } else {
         if (frontPosition == -1)
             frontPosition = rearPosition = 0;
         else if (rearPosition == MAXIMUM_SIZE - 1)
             rearPosition = 0;
         else
             rearPosition++;
         queue[rearPosition] = value;
         printf("Inserted %d into the queue.\n", value);
     }
 }

 void deleteElement() {
     if (frontPosition == -1) {
         printf("Queue Underflow! No elements to delete.\n");
     } else {
         printf("Deleted %d from the queue.\n", queue[frontPosition]);
         if (frontPosition == rearPosition)
             frontPosition = rearPosition = -1;
         else if (frontPosition == MAXIMUM_SIZE - 1)
             frontPosition = 0;
         else
             frontPosition++;
     }
 }

 void displayQueue() {
     if (frontPosition == -1) {
         printf("Queue is empty.\n");
         return;
     }
     printf("Queue elements are: ");
     if (rearPosition >= frontPosition) {
         for (int index = frontPosition; index <= rearPosition; index++)
             printf("%d ", queue[index]);
     } else {
         for (int index = frontPosition; index < MAXIMUM_SIZE; index++)
             printf("%d ", queue[index]);
         for (int index = 0; index <= rearPosition; index++)
             printf("%d ", queue[index]);
     }
     printf("\n");
 }

 int main() {
     int userChoice, elementValue;
     char userDecision;

     do {
         printf("\n--- Circular Queue Using Array ---\n");
         printf("1. Insert Element\n2. Delete Element\n3. Display Queue\n");
         printf("Enter your choice: ");
         scanf("%d", &userChoice);

         switch (userChoice) {
             case 1:
                 printf("Enter value to insert: ");
                 scanf("%d", &elementValue);
                 insertElement(elementValue);
                 break;
             case 2:
                 deleteElement();
                 break;
             case 3:
                 displayQueue();
                 break;
             default:
                 printf("Invalid choice.\n");
         }

         printf("\nDo you want to continue or quit? (y/n): ");
         scanf(" %c", &userDecision);

     } while (userDecision == 'y' || userDecision == 'Y');

     printf("Program ended.\n");
     return 0;
 }

Output:

Uploading: 35778 of 35778 bytes uploaded.


← Previous 🏠 Homepage Next Chapter →

No comments:

Post a Comment

rating System

Loading...

A Friendship Story of Learning Data Structures with C

Sr. No. DSU BLOGS CHAPTERS 1 Array Operations in C, The Group of Friendship (Create, Insert, Delete ...