0% found this document useful (0 votes)
3 views10 pages

Implementation of Stack Using Array

This pdf explains how to use a stack data structure using arrays

Uploaded by

eturisharma
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views10 pages

Implementation of Stack Using Array

This pdf explains how to use a stack data structure using arrays

Uploaded by

eturisharma
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Implementation of Stack

Using Array
Data Structures and Algorithms with C

Presented by: Eturu Vishnu Sravan Sharma


What is a Stack?
A Stack is a linear data structure that follows the LIFO principle — Last In,
First Out. The most recently added element is always the first to be removed.

🍽️ Stack of Plates 🔙 Browser Back Button


Add or remove only from the top Navigates through pages in
reverse order

↩️ Undo in MS Word
Reverses actions in reverse sequence
Core Stack Operations

Push Pop Peek


Inserts an element at the top Removes the topmost element Reads the top element without removing
it

IsEmpty / IsFull Display


Checks stack boundary conditions Prints all elements from top to bottom
Array Representation of Stack

#define MAX 20
int a[MAX]; // array stores elements
int top = -1; // -1 means stack is empty

a[MAX] MAX = 20
Fixed-size integer array Maximum capacity

top = -1
Empty stack indicator
Function Declarations & Menu Driver
Function Prototypes Menu Loop in main()

void push(); while(1) {


void pop(); printf("[Link] [Link]\n");
void peek(); printf("[Link] [Link]\n");
void display(); printf("[Link] [Link]\n");
bool isempty(); printf("[Link]\n");
bool isfull(); scanf("%d", &ch);
switch(ch) { ... }
}

The switch dispatches the user's choice. The loop continues


until option 7 is selected.
Push Operation
Algorithm Steps
void push() {
int el; 01
printf("Enter element: ");
Read the element to insert
scanf("%d", &el);
if (isfull())
02
printf("Stack Overflow");
else { Check if stack is full using isfull()
top = top + 1;
a[top] = el; 03
}
Increment top by 1
}

04

Store element at a[top]

Time Complexity: O(1) — constant time insertion


Pop & Peek Operations
Pop — Remove Top Peek — View Top

void pop() { void peek() {


if (isempty()) if (isempty())
printf("Stack Underflow"); printf("Stack Empty");
else else
printf("Removed = %d", printf("Top = %d", a[top]);
a[top--]); }
}

Reads top element without removing it


Checks for empty stack first Stack remains unchanged after call
Prints and removes top element Complexity: O(1)
Complexity: O(1)
Boundary Checks & Display
isEmpty() and isFull() Display Function

bool isempty() { void display() {


if (top == -1) return true; int i;
return false; if (isempty())
} printf("Stack Empty");
bool isfull() { else {
if (top == MAX-1) return true; for (i = top; i >= 0; i--)
return false; printf("%d\n", a[i]);
} }
}

→ Stack is empty
top == -1

top == MAX-1 → Stack is full Iterates from top down to 0, printing elements in LIFO order.
Sample Program Output
Enter Choice : 1
Enter element : 10
Enter Choice : 1
Enter element : 20
Enter Choice : 1
Enter element : 30
Enter Choice : 6
The status of stack is
30
20
10
Enter Choice : 2
Removed element = 30
Enter Choice : 3
Top element = 20

Push 10, 20, 30 Display → 30 20 10


Stack grows upward Top-to-bottom order

Pop → removes 30 Peek → shows 20


LIFO in action Top after pop
Advantages, Limitations & Applications
✅ Advantages 🔧 Real-World Applications
Simple and easy to implement Function Calls & Recursion — Call stack manages
Push and Pop are O(1) — very fast execution

Efficient memory access with arrays

⚠️ Limitations Expression Evaluation — Infix, prefix, postfix conversion

Fixed size — cannot grow dynamically


Undo / Redo — Text editors and IDEs
Stack Overflow possible when full
Wasted memory if pre-allocated size is large

Browser History — Back and forward navigation

Parentheses Matching — Compiler syntax checking

You might also like