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:
The front index removes elements
-
The rear index inserts elements
-
FIFO first element inserted gets removed first
-
Overflow happens when the queue is full
-
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; }
#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#define MAXIMUM_SIZE 5int 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 → |
No comments:
Post a Comment