[Follow here] for daily coding insights.
DSA SERIES — Chapter 5 STACKS (IN C)
1. INTRODUCTION TO STACK
A stack organizes data such that the last inserted element is the first one to be removed.
Example: A stack of plates — the last plate placed on top is the first to be taken out.
CHECK LIST OF REAL LIFE EXAMPLES FOR STACKS
LIFO(Last In First Out) Rule:
-
Push → Insert element
-
Pop → Remove most recently inserted element
2. STACK REPRESENTATION IN C
We can implement stacks in two ways:
-
Using Arrays
-
Using Linked Lists
In this post, we focus on Array-based Stack first.
3. STACK STRUCTURE IN C (Array-Based)
struct Stack {
int arr[100]; // Stores the actual stack elements
int top; // Points to index of the top element
int size; // Maximum capacity of the stack
};
Explanation of Structure Members:
-
arr[100] → Fixed-size array to store stack data.
-
top →
-
Initially -1 (means stack is empty)
-
After push: increases
-
After pop: decreases
-
-
size → Total allowed capacity.
-
Helps check overflow (top == size - 1)
-
4. INITIALIZING A STACK
void initStack(struct Stack *s, int capacity) {
s->top = -1;
s->size = capacity;
}
5. CHECK OPERATIONS
Check if Stack is Full
int isFull(struct Stack *s) {
return s->top == s->size - 1;
}
Check if Stack is Empty
int isEmpty(struct Stack *s) {
return s->top == -1;
}
6. PUSH OPERATION (INSERTION)
Adds element to the top of the stack.
void push(struct Stack *s, int value) {
if (isFull(s)) {
printf("Stack Overflow! Cannot push %d\n", value);
return;
}
s->arr[++(s->top)] = value;
printf("%d pushed to stack\n", value);
}
Detailed Explanation:
-
isFull()prevents overflow error -
++(s->top)increments the top pointer first -
Stores value at the new top position
7. POP OPERATION (DELETION)
Removes and returns the top element.
int pop(struct Stack *s) {
if (isEmpty(s)) {
printf("Stack Underflow!\n");
return -1;
}
return s->arr[(s->top)--];
}
Explanation:
-
Checks for empty condition
-
Returns current top value
-
Decrements top pointer
8. PEEK / TOP ELEMENT
Returns top element without removing it.
int peek(struct Stack *s) {
if (isEmpty(s)) {
printf("Stack is Empty\n");
return -1;
}
return s->arr[s->top];
}
9. DISPLAY STACK
void display(struct Stack *s) {
if (isEmpty(s)) {
printf("Stack is Empty\n");
return;
}
printf("Stack elements (Top to Bottom):\n");
for (int i = s->top; i >= 0; i--) {
printf("%d ", s->arr[i]);
}
printf("\n");
}
10. COMPLETE PROGRAM
#include <stdio.h>
struct Stack {
int arr[100];
int top;
int size;
};
void initStack(struct Stack *s, int capacity) {
s->top = -1;
s->size = capacity;
}
int isFull(struct Stack *s) {
return s->top == s->size - 1;
}
int isEmpty(struct Stack *s) {
return s->top == -1;
}
void push(struct Stack *s, int value) {
if (isFull(s)) {
printf("Stack Overflow! Cannot push %d\n", value);
return;
}
s->arr[++(s->top)] = value;
printf("%d pushed to stack\n", value);
}
int pop(struct Stack *s) {
if (isEmpty(s)) {
printf("Stack Underflow!\n");
return -1;
}
return s->arr[(s->top)--];
}
int peek(struct Stack *s) {
if (isEmpty(s)) {
printf("Stack is Empty\n");
return -1;
}
return s->arr[s->top];
}
void display(struct Stack *s) {
if (isEmpty(s)) {
printf("Stack is Empty\n");
return;
}
printf("Stack elements (Top to Bottom):\n");
for (int i = s->top; i >= 0; i--) {
printf("%d ", s->arr[i]);
}
printf("\n");
}
int main() {
struct Stack s;
initStack(&s, 100);
push(&s, 10);
push(&s, 20);
push(&s, 30);
display(&s);
printf("Top element: %d\n", peek(&s));
printf("Popped: %d\n", pop(&s));
display(&s);
return 0;
}
ALSO CHECK
ALSO CHECK
.png)
Comments
Post a Comment