Introduction of stack and its operations in data structure
Alipurduar Government Engineering & Management College
Name : Sarup Mukherjee
Department : Electronics & Communication Engineering
Subject : Data Structure & Algorithms
Subject Code : ES-CS301
Roll No. : 38700324072
Registration No. : 243870120241
Topic : CA1
Session : 2024-2025
Introduction to Stacks
in Data Structures
Fascinating world of stacks, a fundamental data structure in
computer science. Stacks are fundamental data structures
that play a crucial role in computer science. They serve
as a foundation for many algorithms and applications,
enabling efficient organization and manipulation of data.
We will explore the concept of stacks, their operations,
implementations, and applications.
Definition of a Stack
LIFO Principle Analogy: A Stack of Plates
Stacks follow the Last-In, First-Out (LIFO) principle, Think of a stack of plates: you can only remove
meaning the last element added to the stack is the top plate, and you add new plates to the top.
the first one to be removed.
Stack Operations: Push,
Pop, Peek, and IsEmpty
1 Push 2 Pop
Adds an element to Removes and returns
the top of the stack. the top element of the
stack.
3 Peek 4 IsEmpty
Returns the top Checks if the stack is
element of the stack empty and returns a
without removing it. boolean value.
Stack Implementation
Using Arrays
Array Representation Size Limit
A stack can be One limitation of array
implemented using an implementation is the
array, with the top of the fixed size of the array,
stack being the last which can lead to
element in the array. overflow if the stack
exceeds its capacity.
Dynamic Allocation
To overcome this limitation, dynamic allocation
techniques can be used to adjust the array size as
needed.
Stack Implementation
Using Linked Lists
Linked List Dynamic Memory
Stacks can also be This method offers the
implemented using linked advantage of dynamic
lists, where each node memory allocation, allowing
contains an element and a the stack to grow as needed.
pointer to the next node.
Time Complexity of Stack Operations
1 2 3 4
Push Pop Peek IsEmpty
O(1) for both array O(1) for both array O(1) for both array O(1) for both array
and linked list and linked list and linked list and linked list
implementations. implementations. implementations. implementations.
Applications of Stacks
Function Calls
Stacks play a crucial role in managing function calls,
where each function call is pushed onto the stack and
popped when the function returns.
Undo/Redo
In many applications, stacks are used to implement
undo/redo functionality, storing the history of
changes for later retrieval.
Expression Evaluation
Stacks are used in the evaluation of mathematical
expressions, converting infix expressions to postfix
and then evaluating them.
Recursion and Stacks
Recursive Calls
Recursive functions, which call themselves, rely heavily on stacks
1
to manage their execution flow.
Stack Frames
2 Each recursive call creates a new stack frame that
contains local variables and function parameters.
Stack Overflow
3 Excessive recursion without proper termination
conditions can lead to a stack overflow error.
Balanced Parentheses Problem
Problem Statement
1 Determine if a given string of parentheses is balanced,
meaning each opening parenthesis has a corresponding
closing parenthesis.
Stack Approach
2 Use a stack to store opening parentheses and pop them when a
closing parenthesis is encountered, ensuring that the stack
remains empty at the
end.
Validation
3 The stack's state at the end of the process
indicates whether the parentheses are balanced.
Conclusion and Key Takeaways
1
Data Structure
Stacks are a fundamental data structure in computer science.
2
LIFO & Efficient Operations
Stacks follow the Last-In, First-Out (LIFO) principle. Stack operations like push , pop,
and peek offer efficient data manipulation.
3
Applications
Stacks have diverse applications in programming and computing.
THANK YOU