0% found this document useful (0 votes)
1 views

Unit no 5 stack

A stack is a linear data structure that operates on a Last In, First Out (LIFO) principle, allowing access only from the top. It is commonly used for expression evaluation, backtracking algorithms, and can be implemented using arrays or linked lists. Key operations include push, pop, peek, isEmpty, and size, with applications in converting between different expression notations.

Uploaded by

Karan Lasure
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Unit no 5 stack

A stack is a linear data structure that operates on a Last In, First Out (LIFO) principle, allowing access only from the top. It is commonly used for expression evaluation, backtracking algorithms, and can be implemented using arrays or linked lists. Key operations include push, pop, peek, isEmpty, and size, with applications in converting between different expression notations.

Uploaded by

Karan Lasure
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 48

Basic Concepts of

Stack
• A stack is a linear data structure that follows the Last In, First Out
(LIFO) principle, meaning the last element added to the stack is the
first one to be removed. It can be visualized as a collection of items
stacked on top of each other, similar to a stack of plates.
Key Characteristics of a Stack:
• LIFO Structure: The last element added is the first one to be removed.
• Limited Access: Elements can only be accessed from the top of the
stack.
• Dynamic Size: The size of a stack can grow or shrink as elements are
added or removed.
Common Use Cases of Stacks:
• Expression Evaluation: Stacks are used in evaluating arithmetic
expressions, especially in parsing and converting infix expressions to
postfix or prefix notation.
• Backtracking Algorithms: Stacks are employed in algorithms like
depth-first search (DFS) where we need to backtrack to explore other
paths.
Stack Abstract Data Type (ADT)
• An Abstract Data Type (ADT) defines a data structure purely in terms
of its behavior from the point of view of a user, specifically in terms of
the operations that can be performed on it, without specifying the
implementation details.
Basic Operations of Stack ADT:
• Push: Adds an element to the top of the stack.
• Pop: Removes and returns the top element of the stack.
• Peek (Top): Returns the top element without removing it from the
stack.
• IsEmpty: Checks if the stack is empty.
• Size: Returns the number of elements in the stack.
Representation of Stacks Using
Sequential Organization
• Stacks can be implemented in two main ways: Sequential
Organization and Linked Organization.
Stack Representation Using
Arrays:
Stack Operations Using Array
Representation:
Example of Stack Operations:
Multiple Stacks
• Sometimes, a program may require multiple stacks within the same
data structure. Multiple stacks can be implemented in various ways:
1. Using a Single Array:
• ou can use a single array to hold multiple stacks by dividing the array
into segments, with each stack having a fixed size. You can maintain
separate top indices for each stack.
Example:

Assume you want to maintain 3 stacks in a single array:


Applications of Stack
1. Expression Evaluation and
Conversion
• Stacks are crucial for evaluating mathematical expressions and
converting them from one notation to another, such as infix, postfix
(Reverse Polish Notation), and prefix (Polish Notation).
Infix Expressions:
• The standard notation where operators are placed between operands
(e.g., A + B).
Postfix Expressions:
• Operators follow their operands (e.g., AB+).
Prefix Expressions:
• Operators precede their operands (e.g., +AB).
Why Use Stacks for Conversion?
• Stacks help manage the order of operations and parentheses in
expressions efficiently. By using a stack, we can ensure that operators
are applied in the correct sequence according to operator precedence
and associativity.
2. Polish Notation and Expression
Conversion
• Polish Notation (prefix) and Reverse Polish Notation (postfix)
eliminate the need for parentheses by defining a specific order of
operations.
Conversion Process:
• Infix to Postfix Conversion:
Scan the infix expression from left to right.
Use a stack to hold operators and parentheses.
Output the operands in the same order as they appear.
Push operators onto the stack based on their precedence.
Infix to prefix conversion
Postfix evaluation
Example
Linked Stack and Operations
• A Linked Stack is an implementation of a stack using a linked list
instead of an array. This allows for dynamic memory allocation, where
the size of the stack can grow or shrink as needed.
Structure of Linked Stack:
• Each node in a linked stack contains:
Data: The value stored in the stack.
Pointer to Next Node: Points to the next node in the stack
Recursion
• Recursion is a programming technique where a function calls itself
directly or indirectly to solve a problem. It breaks down complex
problems into simpler sub-problems of the same type, allowing for
elegant and concise solutions.
Concept of Recursion
• Base Case: This is a condition that stops the recursion. It prevents
infinite calls by providing a terminating condition.
• Recursive Case: This is where the function calls itself with modified
arguments, gradually moving towards the base case.
Example: Factorial Calculation
• The factorial of a number 𝑛n (denoted as 𝑛!n!) is defined as:
• 𝑛!=𝑛×(𝑛−1)! for 𝑛>0
• 0!=1 (base case)
Variants of Recursion
Indirect recursion
In indirect recursion, a function calls another function that eventually calls the first function again.
Tail recursion call
Tree Recursion
Backtracking Algorithmic Strategy
Example of Backtracking: N-Queens
Problem

You might also like