28.10.25

17. Stack Using Array The One Where Friends Follow the “Last In, First Out” Rule


hello everyone 

So today’s topic wasn’t just coding it was trust issues. Why? Because Aarav kept saying, “Bro, I put it last, why do I have to remove it first?” Rahul just smacked him and said, “That’s literally how a stack works.”  And boom we learned Stack Implementation using Arrays!

What’s Happening Here?

A stack follows the classic rule:
Last In - First Out (LIFO)

We use:

  • an array to store the stack

  • a variable called top to track the last inserted element

  • PUSH to add items

  • POP to remove the latest item

  • DISPLAY to show what’s inside

How It Works 

Aarav handled all the PUSH operations  he kept adding values proudly until Rahul yelled “Overflow!” because he filled the whole stack . Rahul was in charge of POP he removed items like she was cleaning her room: fast and without warning . I (Daksh) took care of displaying the stack, showing the list every time like a news reporter announcing “This is what’s left!” Together, we made the stack behave exactly like it should organized, controlled, and absolutely drama-free unlike our group chats 

Code:

 #include <stdio.h>
 #define MAX 5
 
 int stack[MAX];
 int top = -1;
 
 void push(int value) {
     if (top == MAX - 1) {
         printf("Stack Overflow! Cannot push %d\n", value);
     } else {
         top++;
         stack[top] = value;
         printf("%d pushed into stack.\n", value);
     }
 }
 
 void pop() {
     if (top == -1) {
         printf("Stack Underflow! Nothing to pop.\n");
     } else {
         printf("%d popped from stack.\n", stack[top]);
         top--;
     }
 }
 
 void display() {
     if (top == -1) {
         printf("Stack is empty.\n");
     } else {
         printf("Current Stack: ");
         for (int position = 0; position <= top; position++)
             printf("%d ", stack[position]);
         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

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