Stack works on LIFO (Last In First Out)
→ Last element added will be removed first.
[Link] Overflow
Overflow happens when you try to PUSH an element into a
FULL stack.
Stack has a fixed size (like 5 elements).
If it is already full and you try to add more → Overflow
1. Stack Underflow
Underflow happens when you try to POP from an EMPTY stack.
No elements are present
Still you try to delete → Underflow
Overflow → "Over full" (Too much data)
Underflow → "Under empty" (No data)
Write a program to implement Push and Pop operations
in Stack.
#include <stdio.h>
#define MAX 5 // Maximum size of stack
int stack[MAX]; // Stack array
int top = -1; // Top initially -1 (stack is empty)
// PUSH operation
void push(int value) {
if (top == MAX - 1) { // Check overflow
printf("Stack Overflow! Cannot insert %d\n", value);
} else {
top++; // Move top forward
stack[top] = value; // Insert value
printf("%d pushed into stack\n", value);
// POP operation
void pop() {
if (top == -1) { // Check underflow
printf("Stack Underflow! No element to delete\n");
} else {
printf("%d popped from stack\n", stack[top]);
top--; // Move top backward
// DISPLAY stack
void display() {
if (top == -1) {
printf("Stack is empty\n");
} else {
printf("Stack elements are:\n");
for (int i = top; i >= 0; i--) {
printf("%d\n", stack[i]);
// MAIN FUNCTION
int main() {
push(10);
push(20);
push(30);
display();
pop();
display();
return 0;
Output
10 pushed into stack
20 pushed into stack
30 pushed into stack
Stack elements are:
30
20
10
30 popped from stack
Stack elements are:
20
10
stack[MAX] → Array to store elements
top → Keeps track of last element
top = -1 → Stack is empty
Why top = -1 and not 0?
top stores the index of the last inserted element in the stack.
Array index starts from 0
If stack is empty → no element exists
So top should point to "no valid index"
-1 means:"There is no element in the stack"
int top = 0;
There is already an element at index 0
top = -1 (empty)
Push 10:
top = top + 1 → 0
stack[0] = 10
Perfect — first element goes to index 0
(top = 0)
Initial:
top = 0
Push 10:
top = top + 1 → 1
stack[1] = 10
Push Operation:
Check if stack is full → top == MAX-1
Increase top
Insert value → stack[top] = value
🔹 Pop Operation:
Check if stack is empty → top == -1
Print top element
Decrease top
🔹 Display:
Print elements from top to 0
Infix, Postfix
Examp
Type Meaning
le
Operator between
Infix A+B
operands
Postfi
AB+ Operator after operands
x
Computers prefer Postfix because:
No brackets needed
Easy to evaluate using stack
Given a string s representing an infix expression
("operand1 operator operand2" ), Convert it into its postfix
notation ("operand1 operand2 operator").
Note: The precedence order is as follows: (^) has the
highest precedence and is evaluated from right to left, (*
and /) come next with left to right associativity, and (+ and
-) have the lowest precedence with left to right
associativity.
Examples:
Input: s = "a*(b+c)/d"
Output: abc+*d/
Explanation: The expression is a * (b + c) / d. First, inside
the brackets, b + c becomes bc+. Now the expression looks
like a * (bc+) / d. Next, multiply a with (bc+), so it becomes
abc+* . Finally, divide this result by d, so it becomes
abc+*d/.
Input: s = "a+b*c+d"
Output: abc*+d+
Explanation: The expression a + b * c + d is converted by
first doing b * c → bc*, then adding a → abc*+, and finally
adding d → abc*+d+.