Stack in Data Structures
and Algorithms (DSA)
Presented by Sadavipra Vyas, 0834AL241060
Shushila Devi Bansal College of Engineering
Introduction to Data Structures and Algorithms (D
Data Structures and Algorithms (DSA) are fundamental concepts in computer science that form the backbone of
efficient programming. A Data Structure is a particular way of organising data in a computer so that it can be used
effectively. An Algorithm is a set of well-defined instructions to solve a particular problem.
Mastering DSA is crucial for developing high-performance software, optimising resource usage, and solving complex
computational challenges. It empowers us to write code that is not only correct but also efficient in terms of time
and space.
Classification of Data Structures
Linear Data Structures Non-Linear Data Structures
Elements are arranged sequentially, with each Elements are not arranged sequentially; instead,
element connected to its previous and next they are organised in a hierarchical or network
element. They can be traversed in a single run. structure. Traversal is more complex.
• Arrays • Trees
• Linked Lists • Graphs
• Stacks • Hash Tables
• Queues
Introduction to Stack
A Stack is a linear data structure that follows a particular order in
which operations are performed. The order can be described as
Last In, First Out (LIFO) or First In, Last Out (FILO).
Imagine a stack of plates: you can only add a new plate to the top,
and you can only remove the topmost plate. This simple principle
governs all operations in a stack, making it highly efficient for
specific tasks.
Stack Representation
Using Array Using Linked List
Key Stack Operations
Push Pop Peek / Top
Adds an element to the top Removes the topmost Returns the topmost
of the stack. If the stack is element from the stack. If element of the stack
full, it's called an Overflow the stack is empty, it's without removing it. Useful
condition. called an Underflow for checking the next item
condition. to be processed.
IsEmpty IsFull
Checks if the stack contains Checks if the stack has
any elements. Returns true reached its maximum
if empty, false otherwise. capacity (relevant for array-
based stacks). Returns true
if full.
Applications of Stack
1 2 3
Expression Evaluation Recursion Backtracking
Used to evaluate arithmetic Function calls are managed Algorithms like solving mazes or
expressions (infix to using an implicit stack to store N-Queens problem use stacks to
postfix/prefix conversion). local variables and return explore paths and revert if a
addresses. path leads to a dead end.
4 5
Undo/Redo Operations Browser History
Text editors use stacks to store previous states of a Web browsers use stacks to store the URLs of visited
document, allowing users to undo changes. pages, enabling the 'back' button functionality.
Algorithm & Flowchart: Push Operation
Algorithm (Push) Flowchart (Push)
1. IF TOP == MAX_SIZE - 1 THEN2. PRINT "Stack Overflow"3.
RETURN4. ELSE5. TOP = TOP + 16. STACK[TOP] = ITEM7. END IF
Algorithm (Pop)
1. IF TOP == -1 THEN2. PRINT "Stack Underflow"3. RETURN
NULL4. ELSE5. ITEM = STACK[TOP]6. TOP = TOP - 17. RETURN
ITEM8. END IF
Advantages & Limitations of Stacks
Advantages Limitations
• Simplicity: Easy to understand and implement. • Fixed Size (Array): Array-based stacks have a pre-
• Efficiency: Push and Pop operations are O(1) time defined capacity, leading to overflow if exceeded.
complexity. • Random Access: Does not allow random access to
• Memory Management: Efficiently handles function elements; only the top element is accessible.
call states in recursion. • Underflow: Popping from an empty stack results in
• Restricted Access: Enforces LIFO, preventing an error.
accidental data modification. • Limited Functionality: Not suitable for problems
requiring access to elements other than the top.
Conclusion
Stacks are vital linear data structures adhering to the LIFO principle, fundamental for efficient problem-solving in
DSA. They offer quick operations and are widely used in critical applications like expression handling, recursion, and
managing undo functionalities.
Understanding stacks is a stepping stone to mastering more complex data structures and algorithms, essential
for any aspiring software developer.
Thank You!
Sadavipra Vyas
Roll Number: 0834AL241060
Shushila Devi Bansal College of Engineering