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

Data Structures Stack PDF

The presentation covers the concept of stacks, a linear data structure that follows the Last In, First Out (LIFO) principle. It details basic operations such as push, pop, peek, isEmpty, and isFull, along with their implementations in C++. Additionally, it discusses the advantages, disadvantages, and real-world applications of stacks in computing.
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)
2 views10 pages

Data Structures Stack PDF

The presentation covers the concept of stacks, a linear data structure that follows the Last In, First Out (LIFO) principle. It details basic operations such as push, pop, peek, isEmpty, and isFull, along with their implementations in C++. Additionally, it discusses the advantages, disadvantages, and real-world applications of stacks in computing.
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

CS DEPARTMENT PRESENTATION

Data Structures – Stack


Concept and Applications

Presented by:

Ashifa Nazrin Bhavadharani


25CS015 25CS022

Divya Dharshini Elakiya


25CS046 25CS048
INTRODUCTION

What is a Stack?
AStackisalineardatastructurethatstoreselements in a specific
[Link]—LastIn,First Out — meaning the
last element added is the first one to be removed.

LIFO Principle Real-Life Example


The last element inserted is the A stack of plates: you always add
first to be removed — just like or remove from the top. You
removing the top plate from a pile. cannot remove from the middle or
bottom directly.

Stack Pointer (TOP)


A pointer called TOP always tracks the topmost element currently in the
stack.
OPERATIONS

Basic Operations of Stack


Astacksupports fivefundamentaloperations thatdefinehow dataisinserted, accessed, and removed.

Push
1 Insert anelementontothetopofthe [Link] foroverflow before pushing.

Pop
2 Remove and return the top element. Check for underflow before popping.

Peek / Top
3 View the top element without removing it. Does not modify the stack.

isEmpty
4 Returns true if the stack has no elements (TOP == -1).

isFull
5 Returns true if the stack has reached its maximum capacity.

Push

Pop
PUSH OPERATION

Push Operation – Explanation & Code


Step-by-Step Explanation C++ Code – Push

1 Check for Overflow voidpush(intstack[], int &top,


If TOP == MAX - 1, the stack is full. Display "Stack intval,int max) {
Overflow" and stop. if(top==max - 1) {
cout<<"Stack Overflow!";
return;
}
2 Increment TOP top++;
Increase the TOP pointer by 1 to point to the next empty stack[top] = val;
slot.
cout<<val<< " pushed to stack";
}

3 Insert Element
Place the new element at
stack[TOP]. TOP starts at -1 when the stack is empty. After each
push, TOP increases by 1.
POP OPERATION

Pop Operation – Explanation & Code


C++ Code – Pop Step-by-Step Explanation

intpop(int stack[], int &top) { 1 Check for Underflow


if(top == -1) { If TOP == -1, the stack is empty. Display "Stack
cout << "Stack Underflow!"; Underflow" and stop.
return -1;
}
intval = stack[top];
top--; 2 Retrieve Top Element
return val;
Save the value at stack[TOP] to return it to the caller.
}

3 Decrement TOP
Decrease TOP by 1 — the element is logically removed
⚠ Stack Underflow occurs when you try to pop an from the stack.
element from an empty stack (TOP == -1).
IMPLEMENTATION

Stack Implementation
Astackcan beimplemented intwo primaryways—eachwithits own trade-offs in memory and flexibility.
G

Array Implementation Linked List Implementation


Uses a fixed-size array and a TOP indexvariable
Simple and fast — O(1) for push & pop Uses nodes with a pointer to the next node
Dynamic size — grows and shrinks as needed
Static memory allocation
No overflow (limited only by system memory)
Limited by a fixed maximum size
May waste memory if stack is not full Slightly more complex with pointer management
Uses extra memory for pointers per node

Array
Implementatio
n
Fixed-size boxes
withTOP index
p ointer.

Linked List
Connectednodes
withTOPpointer to
h ead.
WORKING EXAMPLE

Working of→ Stack



– →Step-by-Step Example
Let's trace through: Push 10 Push 20 Push 30 Pop and observe how the stack changes at each step.

1 2

Step 1: Push 10 Step 2: Push 20


TOP
[ 10 ]= 0 TOP
[ 10, =
201 ]
← TOP ← TOP
Stack has one element: 10 20 is placed above 10

3 4

Step 3: Push 30 Step 4: Pop


TOP
[ 10, = 2 30 ]
20, TOP
[ 10, =
201 ]
← TOP ← TOP
30 is now at the top 30 is removed (LIFO!)

✅ After pop, 30 is returned. Stack now contains: 10, 20 with TOP pointing to 20.
ANALYSIS

Advantages and Disadvantages


Advantages Disadvantages
Simple to Implement StackOverflow Risk
Easytocodeusingarraysorlinked lists with minimal logic. Array-basedstackshaveafixedcapacity; pushing beyond it causes
overflow.
Fast Operations
Limited Access
Pushandpopruninconstanttime — O(1) complexity.
Youcanonlyaccessthetopelement—no random access to
elements below.
Manages Function Calls
Automaticallyhandlesrecursionand local variables via call stack. Fixed Size (Array)
Arrayimplementationhasapredetermined maximum size decided
Memory Efficient at compile time.
Onlyallocatesmemorywhenelements are actually pushed.
Pointer Overhead (Linked List)
Linkedliststacksuseextramemoryfornext-node pointers in each
element.
APPLICATIONS

Real-World Applications of Stack


Stacksareusedeverywhereincomputing—fromhowyourbrowserworkstohowcompilers validate your code.

Function Calls & Recursion Undo / Redo Expression Evaluation


The call stack stores function return Text editors push every action onto a Used to evaluate infix, prefix, and
addresses and local variables. stack. Undo pops the last action; Redo postfix mathematical expressions
Recursion relies entirely on the stack. re-applies it. efficiently.

Parenthesis Checking Browser History


Compilers use a stack to verify that Each visited page is pushed onto a
every opening bracket has a matching stack. Clicking "Back" pops the current
closing bracket. page to return to the previous one.
20
Middle element

30 10
TOP (pointer) Bottom element

Thank You!
Ashifa Nazrin Bhavadharani
25CS015 25CS022

Divya Dharshini Elakiya


25CS046 25CS048

You might also like