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:
Every node holds data + a pointer
-
A new element always goes to the rear
-
Deletion always happens at the front (FIFO forever!)
-
Memory is dynamic, so no fixed size like arrays
-
frontNode - first element
-
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; }
#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 → |
No comments:
Post a Comment