Beyond Plates: Unveiling the Power of Stacks in Programming using C
3 min readJun 17, 2024
A stack is a fundamental linear data structure that follows the Last-In-First-Out(LIFO)
principle. It’s analogous to a stack of plates: you can only add or remove plates from the top.
Use Cases :
- Function Call Stack: The system automatically uses a stack to manage function calls. When a function is called, its arguments, local variables, and return address are pushed onto the stack. When the function returns, its information is popped from the stack.
- Expression Evaluation: Stacks are used to evaluate expressions in reverse Polish Notation (RPN). Operands are pushed onto the stack, and operators are applied to the top elements.
- Backtracking Algorithms: Backtracking algorithms often use stacks to store alternative paths to explore and then backtrack by popping from the stack when a dead end is encountered.
- Undo/Redo Functionality: Stacks can be used to implement undo/redo functionality in applications by storing past actions on the stack and allowing users to revert or redo them.
Stack Operations:
- Push: Adding an element to the top of the stack.
- Pop: Removing and returning the element at the top of the stack.
- Peek: Viewing the element at the top of the stack without removing it.
Stack in Computers:
- Function Calls in Computer Programs:
— Push: When a function is called, its arguments, local variables, and return address are pushed onto a stack (creating a new frame on the stack).
— Pop: When a function returns, its information is popped from the stack (its frame is removed).
— Peek: (Not typically used directly) But conceptually, peeking at the stack would be like looking at the arguments and local variables of the currently running function. - Undo/Redo Functionality:
— Push: Each action performed is added (pushed) onto a stack, creating a history.
— Pop: When “undo” is selected, the last action is removed (popped) from the stack, reverting the change.
— Peek: (Not always used) But peeking at the stack could reveal the last action in the history (helpful for understanding what can be undone).
#include <stdio.h>
#include <stdlib.h>
int choice, stack[10], top = 0, element;
void menu();
void push();
void pop();
void showElements();
int main() {
menu();
while (1) {
printf("Enter one of the following options:\n");
printf("PUSH 1\nPOP 2\nSHOW ELEMENTS 3\nEXIT 4\n");
scanf("%d", &choice);
switch (choice) {
case 1:
push();
break;
case 2:
pop();
break;
case 3:
showElements();
break;
case 4:
exit(0); // Terminate the program
default:
printf("Invalid choice. Please enter a number between 1 and 4.\n");
}
}
return 0; // Not strictly necessary in this case, but good practice
}
void menu() {
printf("Stack Operations Menu\n");
}
void push() {
if (top == 9) {
printf("Stack overflow\n");
return;
}
printf("Enter the element to be pushed: ");
scanf("%d", &element);
stack[top++] = element;
}
void pop() {
if (top == 0) {
printf("Stack underflow\n");
return;
}
element = stack[--top];
printf("Popped element: %d\n", element);
}
void showElements() {
if (top == 0) {
printf("Stack is empty\n");
return;
}
printf("Stack elements: ");
for (int i = top - 1; i >= 0; i--) {
printf("%d ", stack[i]);
}
printf("\n");
}