hello everyone
Today’s DSU coding chaos started exactly how you’d expect: Aarav kept stacking snacks on the table, Rahul kept removing them, and I (Daksh) somehow turned this into code and just like that, we ended up learning Stacks using Linked Lists.
What’s Going On Here?
A stack works on the rule we all follow when checking exam results: Last opened - First closed That's LIFO - Last In, First Out. But instead of using arrays, we’re using a linked list, because:
-
It grows dynamically (so no “Stack Overflow” like arrays)
-
Memory gets allocated only when needed
-
Nodes can be added or removed super easily
Each node stores: the data, a pointer to the next node and the top pointer always points to the newest item.
How We Worked as a Team
Rahul – PUSH Master
Rahul took over the PUSH operation. Every time he added a node he yelled: “New item, new top move aside Aarav!” He made sure every new element was placed right at the top of the stack.
Aarav – POP Expert
Aarav loved POP because he got to remove things. Snacks? Removed. My pen? Removed. Stack elements? ALSO removed. He popped the top node safely and freed memory like a responsible coder .
Me (Daksh) – The Display Guy
I kept printing the stack after every operation: “Here’s your stack, updated and stable unlike our group.” From top to bottom, everything was clean and readable.
Why Linked List Stack Is Cool
-
No fixed size - grows with your needs
-
No overflow issues
-
Perfect for memory-efficient programs
-
PUSH and POP become super smooth
Code:
#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; struct Node* top = NULL; void push(int value) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); if (!newNode) { printf("Stack Overflow! Memory not available.\n"); return; } newNode->data = value; newNode->next = top; top = newNode; printf("%d pushed into stack.\n", value); } void pop() { if (top == NULL) { printf("Stack Underflow! Nothing to pop.\n"); return; } struct Node* tempNode = top; printf("%d popped from stack.\n", top->data); top = top->next; free(tempNode); } void display() { if (top == NULL) { printf("Stack is empty.\n"); return; } struct Node* current = top; printf("Current Stack: "); while (current != NULL) { printf("%d ", current->data); current = current->next; } printf("\n"); } int main() { push(10); push(20); push(30); display(); pop(); display(); push(40); display(); return 0; }
#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; struct Node* top = NULL; void push(int value) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); if (!newNode) { printf("Stack Overflow! Memory not available.\n"); return; } newNode->data = value; newNode->next = top; top = newNode; printf("%d pushed into stack.\n", value); } void pop() { if (top == NULL) { printf("Stack Underflow! Nothing to pop.\n"); return; } struct Node* tempNode = top; printf("%d popped from stack.\n", top->data); top = top->next; free(tempNode); } void display() { if (top == NULL) { printf("Stack is empty.\n"); return; } struct Node* current = top; printf("Current Stack: "); while (current != NULL) { printf("%d ", current->data); current = current->next; } printf("\n"); } int main() { push(10); push(20); push(30); display(); pop(); display(); push(40); display(); return 0; }
Output:
| ← Previous | 🏠 Homepage | Next Chapter → |
No comments:
Post a Comment