30.10.25

25. Circular Queue Using Linked List The One Where Our Queue Turns Into a Never-Ending Circle


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:

  1. Every node = data + pointer

  2. But this time… the rear node points back to the front node

  3. INSERT happens at rear

  4. DELETE happens at front

  5. 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;
 }

Output:

Uploading: 35778 of 35778 bytes uploaded.


← Previous 🏠 Homepage Next Chapter →

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 →

23. Linear Queue Using Linked List The One Where Rahul Says “Dynamic Problems Need Dynamic Solutions”

     

hello everyone 

Today felt like one of those days where the actual code was calm but the DSU friend group? pure chaotic. We had to write a C program for: “insert and delete operations on a Linear Queue using a Linked List.” And trust me this was the perfect topic for our gang.

Scene 1: The “Array Queue Trauma” Flashback

As soon as sir said Linked List Queue, Aarav screamed: “finally! No more overflow! My trauma is healed!” Rahul looked at him the same way you look at someone who says “WiFi not working? Restart the whole society.” He calmly replied: “Aarav overflow still happens. RAM doesn’t grow on trees.” Me (Daksh) silently started writing the blog title.

What Makes Linked List Queue Different?

Here’s what our little DSU gang discovered:

  1. Every node holds data + a pointer

  2. A new element always goes to the rear

  3. Deletion always happens at the front (FIFO forever!)

  4. Memory is dynamic, so no fixed size like arrays

  5. frontNode - first element

  6. rearNode - last element

Aarav called this: “Queue with unlimited storage. Basically my 230 GB WhatsApp backup.”

No comments.

Division of Responsibilities (aka “Why We Always Fight”)

Rahul – The Rear Gatekeeper

He handled INSERT operations. He kept saying: “If you want to enter, go behind everyone” and glared at Aarav.

Aarav – The Accidental Front Bouncer

He handled DELETE. But he kept trying to delete from the rear too. Rahul snapped: “DUDE, THIS IS NOT A STACK. FRONT MEANS FRONT.” Aarav: “Then why isn't it called FRONTSTACK???” Me: deeply questioning my life choices.

Me (Daksh) – Display + Peacekeeper

I displayed the queue after each operation.

Why?

  • No array size limits

  • No shifting of elements

  • Efficient for dynamic input

  • Perfect for unpredictable workloads

  • And perfect for unpredictable friends like Aarav

Code:

 #define _CRT_SECURE_NO_WARNINGS
 #include <stdio.h>
 #include <stdlib.h>

 struct Node {
     int dataValue;
     struct Node* nextNode;
 };

 struct Node* frontNode = NULL;
 struct Node* rearNode = NULL;

 void insertElement(int value) {
     struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
     newNode->dataValue = value;
     newNode->nextNode = NULL;
     if (rearNode == NULL) {
         frontNode = rearNode = newNode;
     } else {
         rearNode->nextNode = newNode;
         rearNode = newNode;
     }
     printf("Inserted %d into the queue.\n", value);
 }

 void deleteElement() {
     if (frontNode == NULL) {
         printf("Queue Underflow! No elements to delete.\n");
         return;
     }
     struct Node* temporaryNode = frontNode;
     printf("Deleted %d from the queue.\n", frontNode->dataValue);
     frontNode = frontNode->nextNode;
     if (frontNode == NULL)
         rearNode = NULL;
     free(temporaryNode);
 }

 void displayQueue() {
     if (frontNode == NULL) {
         printf("Queue is empty.\n");
         return;
     }
     struct Node* currentNode = frontNode;
     printf("Queue elements are: ");
     while (currentNode != NULL) {
         printf("%d ", currentNode->dataValue);
         currentNode = currentNode->nextNode;
     }
     printf("\n");
 }

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

     do {
         printf("\n--- Linear Queue Using Linked List ---\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:


← Previous 🏠 Homepage Next Chapter →

22. Linear Queue Using Array The One Where Aarav Keeps Jumping the Line

     

hello everyone 

So today in class, we were supposed to write a C program that performs INSERT and DELETE operations on a Linear Queue using an Array. But of course things can never be normal when Aarav is around.

Today’s Chaos Begins

Sir explained: “Queue means FIFO - First In First Out.”

Before anyone else said anything, Aarav shouted: “So I get to go first, right?”

Rahul turned to him slowly like a disappointed father: “NO. You literally go LAST.”

Aarav: confused noises

And boom the chapter began.

How a Linear Queue Works (According to Rahul & Reality)

A Linear Queue using an Array works like this:

  1. The front index removes elements

  2. The rear index inserts elements

  3. FIFO first element inserted gets removed first

  4. Overflow happens when the queue is full

  5. Underflow happens when it's empty

But according to Aarav:

“Overflow happens when Rahul keeps eating all the chips.”

We ignored him and continued.

What Each of Us Did

Rahul - The Rear Manager

He took control of INSERT operations like a king managing an entry gate.
Whenever a new value arrived, he said: “Go to the rear. NO CUTTING.”

Aarav - The Front Exit Guy

He was in charge of DELETE but kept deleting the wrong things. Rahul had to keep reminding him: “Bro, you can ONLY delete from the front. That’s the rule.”

Me (Daksh) - The Display Guy

I printed the queue after every operation like:
“Here’s the queue more organized than our actual life.”

Why?

  • Helps understand FIFO clearly

  • Uses simple array indexing

  • Good practice for queue fundamentals

  • Makes you appreciate how ordering systems work

  • Plus, when you do it with Aarav around, it becomes comedy

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 (rearPosition == MAXIMUM_SIZE - 1) {
         printf("Queue Overflow! Cannot insert more elements.\n");
     } else {
         if (frontPosition == -1)
             frontPosition = 0;
         rearPosition++;
         queue[rearPosition] = value;
         printf("Inserted %d into the queue.\n", value);
     }
 }

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

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

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

     do {
         printf("\n--- Linear 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:


← Previous 🏠 Homepage Next Chapter →

29.10.25

21.Reverse Traversal of Linked List The One Where Aarav Wants a “Back Button” in Real Life


hello everyone

Today’s DSU lab started with a very heavy dialogue from sir: “Create a singly linked list and print it in reverse using recursion.” Before anyone could even react, Aarav sighed dramatically and said: “Why can’t life also have a reverse button?” Rahul didn’t miss a beat: “Bro, forget your life. Reverse the linked list first.” And that’s how chaos began.

What’s Going On Here?

A singly linked list normally moves forward-only just like Aarav walking into problems. So printing it in reverse is tricky because there’s no backward pointer. But Rahul, our group’s human CPU, explained: Use recursion, Travel all the way to the end of the list, Then, while, coming back, print each node’s data, No pointer reversal, No extra arrays, Just pure recursion magic, Aarav summed it up confidently: “Means the function goes ahead but the printing comes back?” Honestly, he’s slowly becoming dangerous.

How We Played Our Roles

  • Rahul built the linked list and the recursion logic
    (and kept flexing that recursion is “just stack behavior, bro”)

  • Aarav kept asking if he could reverse his exam marks the same way
    (sadly, no)

  • I (Daksh) displayed the output and pretended I wasn’t losing my mind

Why This Program Is Fun

  • The list remains completely unchanged

  • Recursion handles everything automatically

  • No need for complex pointer manipulations

  • Makes you feel like you’re doing black magic in C

And the best part? Aarav started calling it the “Reverse Playback Linked List.” Someone please stop him.

Code:

 #define _CRT_SECURE_NO_WARNINGS
 #include <stdio.h>
 #include <stdlib.h>
 
 struct Node {
     int data;
     struct Node* next;
 };
 
 struct Node* createNewNode(int dataValue) {
     struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
     newNode->data = dataValue;
     newNode->next = NULL;
     return newNode;
 }
 
 void insertNodeAtEnd(struct Node** headReference, int dataValue) {
     struct Node* newNode = createNewNode(dataValue);
     if (*headReference == NULL) {
         *headReference = newNode;
         return;
     }
     struct Node* temporaryNode = *headReference;
     while (temporaryNode->next != NULL)
         temporaryNode = temporaryNode->next;
     temporaryNode->next = newNode;
 }
 
 void printListInReverse(struct Node* headReference) {
     if (headReference == NULL)
         return;
     printListInReverse(headReference->next);
     printf("%d ", headReference->data);
 }
 
 int main() {
     struct Node* headReference = NULL;
     int numberOfNodes, dataValue;
     printf("Enter number of nodes: ");
     scanf("%d", &numberOfNodes);
 
     for (int index = 0; index < numberOfNodes; index++) {
         printf("Enter data for node %d: ", index + 1);
         scanf("%d", &dataValue);
         insertNodeAtEnd(&headReference, dataValue);
     }
 
     printf("Nodes in reverse order: ");
     printListInReverse(headReference);
     printf("\n");
     return 0;
 }

Output:


← Previous 🏠 Homepage Next Chapter →

20. Reverse String Using Recursion The One Where Aarav Thinks He’s a DJ

    

hello everyone 

So today's DSU coding session started peacefully until sir said: “Write a program to reverse a string using recursion.” And before anyone could even open their compiler, Aarav jumped up like he’s performing at Sunburn: “DUDE, I REVERSE TRACKS DAILY. STRING REVERSE IS NOTHING.” Yes he really compared recursion to DJ-ing. Rahul’s soul left his body for 2 seconds.

What’s Actually Happening?

Rahul, our unofficial group teacher, explained:

  1. Recursion keeps moving forward in the string
  2. It stops when it hits '\0'
  3. Then the real magic begins
  4. As the function returns step-by-step (unwinding),
  5. it prints each character backward
  6. No loops
  7. No extra arrays
  8. Pure logical elegance (unlike our group discussions)

Aarav said it as:
“Means go ahead, but print while coming back?”
He actually understood it. Miracles do happen.

How We Divided the Work

  • Rahul handled the recursion logic
    (and reminded Aarav 5 times that DJ skills don’t count as coding skills)

  • Aarav kept giving commentary like:
    “Reverse it like a drop! Backward drop incoming!”

  • I (Daksh) just printed the reversed string
    and maintained peace as usual.

Why This Program Is Cool

Works without loops, Uses the call stack to reverse automatically, Simple, elegant, and extremely clean, Makes you look smarter than you feel

In simple words recursion does all the heavy lifting while we pretend to work.

Code:

 #define _CRT_SECURE_NO_WARNINGS
 #include <stdio.h>
 
 void printReverseString(char givenString[]) {
     if (*givenString == '\0')
         return;
     printReverseString(givenString + 1);
     printf("%c", *givenString);
 }
 
 int main() {
     char givenString[100];
     printf("Enter a string: ");
     scanf("%s", givenString);
     printf("Reversed string: ");
     printReverseString(givenString);
     printf("\n");
     return 0;
 }

Output:


← Previous 🏠 Homepage Next Chapter →

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