My learning!
20.11.25
18.11.25
28. BST Traversals The One Where We Explore the Tree in Every Possible Direction
hello everyone
So after we finished building the Binary Search Tree, Aarav confidently said,
“Traversal? Easy. It’s just printing stuff.” Rahul looked at him like he just failed recursion 101 and replied, “Bro… there are THREE types of tree traversals. THREE. Try not to break the tree.” And that’s how today’s adventure of Preorder, Inorder, and Postorder began.
What’s Happening Here?
We’re still working with the same BST, but now we’re exploring it in different ways:
Preorder (Root - Left - Right)
Rahul took this one because he likes being first everywhere . He literally starts by printing the node itself before checking anything else like entering a room and announcing, “Rahul has arrived!”
Inorder (Left - Root - Right)
Aarav said this one is “the most peaceful” because it gives sorted output. He liked it because he doesn’t need to think, just go left then root then right. Simple enough for him .
Postorder (Left - Right - Root)
I (Daksh) handled this one. Why? Because I always clean up last and Postorder prints the node at the end. Left subtree, right subtree, and THEN the node. Perfect for someone who finishes everything “later” .
How We Divided the Work
Rahul: Did Preorder instantly
“ROOT first, everything else later. Just like my priorities.”
Aarav: Showed off Inorder
“Sorted output, thank you very much.”
Me (Daksh): Printed Postorder
“This feels like rewriting an exam last minute.
Code:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
struct node {
int dataValue;
struct node *leftChild;
struct node *rightChild;
};
struct node* createNode(int value) {
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->dataValue = value;
newNode->leftChild = NULL;
newNode->rightChild = NULL;
return newNode;
}
struct node* insertElement(struct node *rootNode, int value) {
if (rootNode == NULL) {
return createNode(value);
}
if (value < rootNode->dataValue) {
rootNode->leftChild = insertElement(rootNode->leftChild, value);
} else {
rootNode->rightChild = insertElement(rootNode->rightChild, value);
}
return rootNode;
}
void preorderTraversal(struct node *rootNode) {
if (rootNode != NULL) {
printf("%d ", rootNode->dataValue);
preorderTraversal(rootNode->leftChild);
preorderTraversal(rootNode->rightChild);
}
}
void inorderTraversal(struct node *rootNode) {
if (rootNode != NULL) {
inorderTraversal(rootNode->leftChild);
printf("%d ", rootNode->dataValue);
inorderTraversal(rootNode->rightChild);
}
}
void postorderTraversal(struct node *rootNode) {
if (rootNode != NULL) {
postorderTraversal(rootNode->leftChild);
postorderTraversal(rootNode->rightChild);
printf("%d ", rootNode->dataValue);
}
}
int main() {
struct node *rootNode = NULL;
int userChoice, elementValue;
char userDecision;
do {
printf("\n1. Insert\n2. Preorder\n3. Inorder\n4. Postorder\nEnter choice: ");
scanf("%d", &userChoice);
switch (userChoice) {
case 1:
printf("Enter value to insert: ");
scanf("%d", &elementValue);
rootNode = insertElement(rootNode, elementValue);
break;
case 2:
printf("Preorder Traversal: ");
preorderTraversal(rootNode);
printf("\n");
break;
case 3:
printf("Inorder Traversal: ");
inorderTraversal(rootNode);
printf("\n");
break;
case 4:
printf("Postorder Traversal: ");
postorderTraversal(rootNode);
printf("\n");
break;
default:
printf("Invalid choice.\n");
}
printf("Do you want to continue? (y/n): ");
scanf(" %c", &userDecision);
} while (userDecision == 'y' || userDecision == 'Y');
return 0;
}
| ← Previous | 🏠 Homepage |
27. Binary Search Tree (BST) The One Where Rahul Thinks Trees Can Sort Themselves
hello everyone
Today’s coding session started with Rahul saying, “Bro, why do we even need sorting? The tree will do it for us.” Aarav rolled his eyes so hard he almost saw his brain. And I (Daksh)… well, I just quietly opened my laptop because I knew drama + data structures = perfect blog material. And that’s how we ended up learning Binary Search Trees.
What’s Going On Here?
A Binary Search Tree (BST) is that one organized friend who keeps everything in the right place.
Every node follows 3 golden rules:
-
Left child has smaller value
-
Right child has larger value
-
And the tree stays sorted just by inserting things properly
Rahul kept saying, “Bro this is magical, it’s sorting itself!” Aarav corrected him: “No Rahul… YOU are sorting it by inserting it correctly.” Rahul didn’t listen.
How It Works
Insert Operation Rahul Style
Rahul was in charge of insertion.
Every time a number came in, he’d decide:
-
“Small? Go left.”
-
“Big? Go right.”
-
“Equal? Also right. No arguments.”
He behaved like the bouncer outside a club, sending numbers left or right without hesitation. Inorder Traversal Aarav’s Favorite Moment Aarav proudly explained: “Inorder traversal (Left - Root - Right) gives sorted output. Because nature likes order. And Rahul doesn’t.” And that’s exactly what happened the moment we printed inorder traversal, the numbers appeared perfectly sorted. Rahul gasped like he saw a ghost. I (Daksh) Just Observed Everything I kept printing messages like: “Here’s your BST, sorted and peaceful unlike our group WhatsApp chat.” Honestly, the tree handled order better than we ever will.
Code:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
struct node {
int dataValue;
struct node *leftChild;
struct node *rightChild;
};
struct node* createNode(int value) {
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->dataValue = value;
newNode->leftChild = NULL;
newNode->rightChild = NULL;
return newNode;
}
struct node* insertElement(struct node *rootNode, int value) {
if (rootNode == NULL) {
return createNode(value);
}
if (value < rootNode->dataValue) {
rootNode->leftChild = insertElement(rootNode->leftChild, value);
} else {
rootNode->rightChild = insertElement(rootNode->rightChild, value);
}
return rootNode;
}
void inorderTraversal(struct node *rootNode) {
if (rootNode != NULL) {
inorderTraversal(rootNode->leftChild);
printf("%d ", rootNode->dataValue);
inorderTraversal(rootNode->rightChild);
}
}
int main() {
struct node *rootNode = NULL;
int userChoice, elementValue;
char userDecision;
do {
printf("\n1. Insert\n2. Inorder Traversal\nEnter your choice: ");
scanf("%d", &userChoice);
switch (userChoice) {
case 1:
printf("Enter value to insert: ");
scanf("%d", &elementValue);
rootNode = insertElement(rootNode, elementValue);
break;
case 2:
printf("Inorder Traversal: ");
inorderTraversal(rootNode);
printf("\n");
break;
default:
printf("Invalid choice.\n");
}
printf("Do you want to continue? (y/n): ");
scanf(" %c", &userDecision);
} while (userDecision == 'y' || userDecision == 'Y');
return 0;
}
| ← Previous | 🏠 Homepage | Next Chapter → |
26. Priority Queue Using Linked List The One Where Friends Let “Priority Decide Everything”
hello everyone
So today’s coding session wasn’t normal because Aarav wanted everything to be first, Rahul wanted only high-priority items, and I (Daksh) just watched them fight while writing the program And that’s exactly how we ended up learning Priority Queue using Linked List.
What’s Going On Here?
A Priority Queue is like real life the most important (or smallest numbered priority) thing gets done first. Not like Aarav, who thinks he should always go first even when his priority is 100 .
We use a linked list so that:
-
each node stores a value and its priority,
-
new elements are inserted in sorted priority order,
-
deletion always removes the highest priority element,
-
and the list grows easily using
malloc.
How It Works
Rahul took over insertions he made sure every new element was placed exactly at the right priority spot, and honestly he was too strict about it “Low priority? Back of the line!” he kept saying. Aarav handled deletions and of course, he only removed the one with the highest priority, bragging every time he “deleted responsibly” I (Daksh) kept displaying the queue after each operation, like: “Here’s your Priority Queue, sorted and drama-free unlike our group decisions.” With all three of us, the priority queue behaved perfectly more perfectly than our plans ever do
Code:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
struct node {
int dataValue;
int priorityValue;
struct node *nextNode;
};
struct node *frontNode = NULL;
void insertElement(int value, int priority) {
struct node *temporaryNode = (struct node*)malloc(sizeof(struct node));
temporaryNode->dataValue = value;
temporaryNode->priorityValue = priority;
temporaryNode->nextNode = NULL;
if (frontNode == NULL || priority < frontNode->priorityValue) {
temporaryNode->nextNode = frontNode;
frontNode = temporaryNode;
} else {
struct node *currentNode = frontNode;
while (currentNode->nextNode != NULL &&
currentNode->nextNode->priorityValue <= priority) {
currentNode = currentNode->nextNode;
}
temporaryNode->nextNode = currentNode->nextNode;
currentNode->nextNode = temporaryNode;
}
printf("Inserted %d with priority %d\n", value, priority);
}
void deleteElement() {
if (frontNode == NULL) {
printf("Queue Underflow! No element to delete.\n");
return;
}
struct node *temporaryNode = frontNode;
printf("Deleted %d (Priority %d)\n",
temporaryNode->dataValue,
temporaryNode->priorityValue);
frontNode = frontNode->nextNode;
free(temporaryNode);
}
void displayQueue() {
if (frontNode == NULL) {
printf("Queue is empty.\n");
return;
}
struct node *currentNode = frontNode;
printf("Priority Queue: ");
while (currentNode != NULL) {
printf("(%d, P=%d) ",
currentNode->dataValue,
currentNode->priorityValue);
currentNode = currentNode->nextNode;
}
printf("\n");
}
int main() {
int userChoice;
int elementValue;
int elementPriority;
char userDecision;
do {
printf("\n1. Insert\n2. Delete\n3. Display\nEnter your choice: ");
scanf("%d", &userChoice);
switch (userChoice) {
case 1:
printf("Enter value to insert: ");
scanf("%d", &elementValue);
printf("Enter priority: ");
scanf("%d", &elementPriority);
insertElement(elementValue, elementPriority);
break;
case 2:
deleteElement();
break;
case 3:
displayQueue();
break;
default:
printf("Invalid choice.\n");
}
printf("Do you want to continue? (y/n): ");
scanf(" %c", &userDecision);
} while (userDecision == 'y' || userDecision == 'Y');
printf("Program ended.\n");
return 0;
}
| ← Previous | 🏠 Homepage | Next Chapter → |
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:
-
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 → |
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
frontPosition - where deletion happens
-
rearPosition - where insertion happens
-
When rear reaches end, it jumps to index 0 (circle vibes)
-
Overflow happens when queue is full
-
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; }
#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 ((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;elserearPosition++;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;elsefrontPosition++;}}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:
| ← Previous | 🏠 Homepage | Next Chapter → |
Subscribe to:
Comments (Atom)
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 ...
-
🌟 CHAPTER INDEX: LEARN 'C' (8 Chapters) Chapter Topic Link Page Date Chapter 1 First ...
