SlideShare a Scribd company logo
1 of 17
STACK
What is a STACK ?
 Ordered list of elements of same data type
 Linear list
 Top
 Last-In First-Out (LIFO) principle.
1
2
3
4
0
Insertion Deletion
Top
STACK
 Stack is an abstract data type with a bounded(predefined)
capacity.
 It is a simple data structure that allows adding and removing
elements in a particular order.
 The order may be LIFO(Last In First Out) or FILO(First In Last
Out).
 The element which is placed (inserted or added) last, is accessed
first.
 Every time an element is added, it goes on the top of the stack
and the only element that can be removed is the element that is
at the top of the stack, just like a pile of objects.
Basic features of Stack
1.Stack is an ordered list of similar data type.
2.Stack is a LIFO(Last in First out) structure or we can say FILO(First in Last out).
3.push() function is used to insert new elements into the Stack and pop() function is used
to remove an element from the stack. Both insertion and removal are allowed at only one
end of Stack called Top.
4.Stack is said to be in Overflow state when it is completely full and is said to be
in Underflow state if it is completely empty.
operations are performed in the stack:
• Push: Adds an item in the stack. If the stack is full, then it is said to be
an Overflow condition.
• Pop: Removes an item from the stack. The items are popped in the
reversed order in which they are pushed. If the stack is empty, then it
is said to be an Underflow condition.
• Peek or Top: Returns top element of stack.
• isEmpty: Returns true if stack is empty, else false.
Applications of stack
• Balancing of symbols
• Infix to Postfix /Prefix conversion
• Redo-undo features at many places like editors, photoshop.
• Forward and backward feature in web browsers
• Used in many algorithms like Tower of Hanoi, tree traversals, stock span
problem, histogram problem.
• Other applications can be Backtracking, Knight tour problem, rat in a maze, N
queen problem and sudoku solver
• In Graph Algorithms like Topological Sorting and Strongly Connected Components
Push Operation
• Step 1 − Checks if the stack is full.
• Step 2 − If the stack is full, produces an error and exit.
• Step 3 − If the stack is not full, increments top to point next empty space.
• Step 4 − Adds data element to the stack location, where top is pointing.
• Step 5 − Returns success.
The process of putting a new data element onto stack is known as a
Push Operation.
Push: Adds an item in the stack. If the stack is full, then it is said to be an Overflow condition.
B
A
Max= 4
Top= 2
Let us have Max= 4, Top=2 Ele=C
Step 1. Top=Max
2=4
2=4false
Go to Else Part
a. Top=Top+1
=2+1
Top=3
b. Stack[top]=Ele
=C
Stack[3]=C
Step 2. Return
C
B
A
Max= 4
Top= 3
Let us have Max= 4, Top=4 Ele=E
Step 1. Top=Max
4=4  True
Print OVERFLOW
EXIT
Step 2. Return
D
C
B
A
Max= 4
Top= 4
Push( stack, MAX, Top, Ele)
{
IF Top=MAX then
Print OVERFLOW” and
Exit
ELSE
Set Top=Top+1
Stack[Top]= Ele
Return
}
CASE 2:
POP- OPERATION
• Step 1 − Checks if the stack is empty.
• Step 2 − If the stack is empty, produces an error and exit.
• Step 3 − If the stack is not empty, accesses the data element at
which top is pointing.
• Step 4 − Decreases the value of top by 1.
• Step 5 − Returns success.
Accessing the content while removing it from the stack, is known as a Pop
Operation.
POP Procedure: POP(STACK, TOP, ITEM)
1.If TOP=0 then
Print UNDERFLOW and Exit.
Else
Set Item:=STACK[TOP]
Set TOP:=TOP-1
2. Return
C
B
A
Max= 4
Top= 3
Let Max=4 TOP=3
Step 1: Top=0
3=0False
Go to ELSE Part
Item=Stack[Top]
= Stack[3]
Item= C
Top=Top-1
=3-1=2
Top=2
2. Return
B
A
Max= 4
Top= 2
CASE 1
Max= 4
Top= 0
CASE 2
Let Max=4 TOP=0
Step 1: Top=0
0=0True
Print UNDERFLOW
2. Return
EVALUATION OF EXPRESSION
The way to write arithmetic expression is known as a notation.
An arithmetic expression can be written in three different but
equivalent notations.
These notations are −
• Infix Notation
• Prefix (Polish) Notation
• Postfix (Reverse-Polish) Notation
Infix Notation
• We write expression in infix notation, e.g. a - b + c, where operators are used in-
between operands.
Prefix Notation
• In this notation, operator is prefixed to operands, i.e. operator is written ahead of
operands. For example, +ab.
• Prefix notation is also known as Polish Notation.
Postfix Notation
• This is known as Reversed Polish Notation.
• In this notation style, the operator is postfixed to the operands i.e., the operator
is written after the operands. For example, ab+.
Postfix Evaluation Algorithm
• Step 1 − scan the expression from left to right
• Step 2 − if it is an operand push it to stack
• Step 3 − if it is an operator pull operand from stack and perform
operation
• Step 4 − store the output of step 3, back to stack
• Step 5 − scan the expression until all operands are consumed
• Step 6 − pop the stack and perform operation
• Read all the symbols one by one from left to right in the given
Postfix Expression
• If the reading symbol is operand, then push it on to the Stack.
• If the reading symbol is operator (+ , - , * , / etc.,), then perform
TWO pop operations and store the two popped oparands in two
different variables (operand1 and operand2). Then perform reading
symbol operation using operand1 and operand2 and push result
back on to the Stack.
• Finally! perform a pop operation and display the popped value as
final result.
Step 1 − scan the expression from left to right
Step 2 − if it is an operand push it to stack
Step 3 − if it is an operator pull operand from stack and
perform operation
Step 4 − store the output of step 3, back to stack
Step 5 − scan the expression until all operands are consumed
Step 6 − pop the stack and perform operation
Steps to convert infix expression to postfix
1. Scan the given infix expression from left to right.
2. If the scanned character is an operand, then output it.
3. Else,
• If the precedence of the scanned operator is greater than the precedence of the operator in the
top of the stack(or the stack is empty or if the stack contains a ‘(‘ ), push it.
• Else, Pop all operators from the stack whose precedence is greater than or equal to that of the
scanned operator. Then, push the scanned operator to the top of the stack. (If you encounter
parenthesis while popping then stop there and push the scanned operator in the stack.)
4. If the scanned character is ‘(‘, push it to the stack.
5. If the scanned character is ‘)’, pop the stack and output characters until ‘(‘ is encountered, and
discard both the parenthesis.
6. Repeat steps 2-5 until infix expression is scanned completely.
7. Print the output.
8. Pop and output from the stack.
INFIX EXPRESSION: a+(b-c/d)-e
Read char a—operand (output it)
stack->empty
a
Read char  +operator push (+)
Read char  (operator push ( ( )
Read char b—operand (output it)
a b
Read char  - operator push (-)
Read char c—operand (output it)
-
( a b c
-Read char  /operator push (/)
(
+
+
+
(
+
(
+
-
(
+
-
(
+
/
-
(
+
Read char d—operand (output it)
a b c d
Read char  )operator pop ( upto ( )
a b c d / -
Read char  - operator push (-)
a b c d / -
- and + have equal precedence but + have higher order. So
Pop + and Push -
a b c d / - +
Read char e—operand (output it)
-
( a b c d / - + e
- Expression scanned completely ,pop element from stack
until it will empty.
+
+ a b c d / - + e -
1. Scan the given infix expression from left to right.
2. If the scanned character is an operand, then
output it.
3. Else,
• If the precedence of the scanned operator is
greater than the precedence of the operator in
the top of the stack(or the stack is empty or if
the stack contains a ‘(‘ ), push it.
• Else, Pop all operators from the stack whose
precedence is greater than or equal to that of
the scanned operator. Then, push the scanned
operator to the top of the stack. (If you
encounter parenthesis while popping then stop
there and push the scanned operator in the
stack.)
4. If the scanned character is ‘(‘, push it to the stack.
5. If the scanned character is ‘)’, pop the stack and
and output characters until ‘(‘ is encountered, and
discard both the parenthesis.
6. Repeat steps 2-5 until infix expression is scanned
completely.
7. Print the output.
8. Pop and output from the stack.
/
-
(
+
+
-
-
+
-
INFIX EXPRESSION: a+(b-c/d)-e
POSTFIX EXPRESSION: a b c d / - + e -

More Related Content

Similar to Stack in Data Structure

Unit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptxUnit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptxYogesh Pawar
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxchandankumar364348
 
Stacks Data structure.pptx
Stacks Data structure.pptxStacks Data structure.pptx
Stacks Data structure.pptxline24arts
 
Stack organization
Stack organizationStack organization
Stack organizationchauhankapil
 
STACK USING LISTS.pptx
STACK USING LISTS.pptxSTACK USING LISTS.pptx
STACK USING LISTS.pptxBaby81727
 
Stack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparationStack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparationRAtna29
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7Kumar
 
STACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data StructureSTACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data StructureYaksh Jethva
 
Infix-Postfix expression conversion
Infix-Postfix expression conversionInfix-Postfix expression conversion
Infix-Postfix expression conversionRashmiranja625
 
Stack PPT.pptx
Stack PPT.pptxStack PPT.pptx
Stack PPT.pptxUzmaRizvi5
 
Chapter 5 Stack and Queue.pdf
Chapter 5 Stack and Queue.pdfChapter 5 Stack and Queue.pdf
Chapter 5 Stack and Queue.pdfGirT2
 

Similar to Stack in Data Structure (20)

Stack - Operations and Applications
Stack - Operations and ApplicationsStack - Operations and Applications
Stack - Operations and Applications
 
Unit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptxUnit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptx
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
 
Stack
StackStack
Stack
 
Stacks Data structure.pptx
Stacks Data structure.pptxStacks Data structure.pptx
Stacks Data structure.pptx
 
Stack organization
Stack organizationStack organization
Stack organization
 
MO 2020 DS Stacks 3 AB.ppt
MO 2020 DS Stacks 3 AB.pptMO 2020 DS Stacks 3 AB.ppt
MO 2020 DS Stacks 3 AB.ppt
 
STACK USING LISTS.pptx
STACK USING LISTS.pptxSTACK USING LISTS.pptx
STACK USING LISTS.pptx
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 
Stack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparationStack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparation
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
STACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data StructureSTACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data Structure
 
DATA STRUCTURE - STACK
DATA STRUCTURE - STACKDATA STRUCTURE - STACK
DATA STRUCTURE - STACK
 
Chapter 4 stack
Chapter 4 stackChapter 4 stack
Chapter 4 stack
 
5.-Stacks.pptx
5.-Stacks.pptx5.-Stacks.pptx
5.-Stacks.pptx
 
Infix-Postfix expression conversion
Infix-Postfix expression conversionInfix-Postfix expression conversion
Infix-Postfix expression conversion
 
Stack PPT.pptx
Stack PPT.pptxStack PPT.pptx
Stack PPT.pptx
 
Chapter 5 Stack and Queue.pdf
Chapter 5 Stack and Queue.pdfChapter 5 Stack and Queue.pdf
Chapter 5 Stack and Queue.pdf
 
Team 3
Team 3Team 3
Team 3
 

Recently uploaded

AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfngoud9212
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsAndrey Dotsenko
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 

Recently uploaded (20)

AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdf
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 

Stack in Data Structure

  • 2. What is a STACK ?  Ordered list of elements of same data type  Linear list  Top  Last-In First-Out (LIFO) principle. 1 2 3 4 0 Insertion Deletion Top
  • 3. STACK  Stack is an abstract data type with a bounded(predefined) capacity.  It is a simple data structure that allows adding and removing elements in a particular order.  The order may be LIFO(Last In First Out) or FILO(First In Last Out).  The element which is placed (inserted or added) last, is accessed first.  Every time an element is added, it goes on the top of the stack and the only element that can be removed is the element that is at the top of the stack, just like a pile of objects.
  • 4. Basic features of Stack 1.Stack is an ordered list of similar data type. 2.Stack is a LIFO(Last in First out) structure or we can say FILO(First in Last out). 3.push() function is used to insert new elements into the Stack and pop() function is used to remove an element from the stack. Both insertion and removal are allowed at only one end of Stack called Top. 4.Stack is said to be in Overflow state when it is completely full and is said to be in Underflow state if it is completely empty.
  • 5. operations are performed in the stack: • Push: Adds an item in the stack. If the stack is full, then it is said to be an Overflow condition. • Pop: Removes an item from the stack. The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is said to be an Underflow condition. • Peek or Top: Returns top element of stack. • isEmpty: Returns true if stack is empty, else false.
  • 6. Applications of stack • Balancing of symbols • Infix to Postfix /Prefix conversion • Redo-undo features at many places like editors, photoshop. • Forward and backward feature in web browsers • Used in many algorithms like Tower of Hanoi, tree traversals, stock span problem, histogram problem. • Other applications can be Backtracking, Knight tour problem, rat in a maze, N queen problem and sudoku solver • In Graph Algorithms like Topological Sorting and Strongly Connected Components
  • 7. Push Operation • Step 1 − Checks if the stack is full. • Step 2 − If the stack is full, produces an error and exit. • Step 3 − If the stack is not full, increments top to point next empty space. • Step 4 − Adds data element to the stack location, where top is pointing. • Step 5 − Returns success. The process of putting a new data element onto stack is known as a Push Operation.
  • 8. Push: Adds an item in the stack. If the stack is full, then it is said to be an Overflow condition. B A Max= 4 Top= 2 Let us have Max= 4, Top=2 Ele=C Step 1. Top=Max 2=4 2=4false Go to Else Part a. Top=Top+1 =2+1 Top=3 b. Stack[top]=Ele =C Stack[3]=C Step 2. Return C B A Max= 4 Top= 3 Let us have Max= 4, Top=4 Ele=E Step 1. Top=Max 4=4  True Print OVERFLOW EXIT Step 2. Return D C B A Max= 4 Top= 4 Push( stack, MAX, Top, Ele) { IF Top=MAX then Print OVERFLOW” and Exit ELSE Set Top=Top+1 Stack[Top]= Ele Return } CASE 2:
  • 9. POP- OPERATION • Step 1 − Checks if the stack is empty. • Step 2 − If the stack is empty, produces an error and exit. • Step 3 − If the stack is not empty, accesses the data element at which top is pointing. • Step 4 − Decreases the value of top by 1. • Step 5 − Returns success. Accessing the content while removing it from the stack, is known as a Pop Operation.
  • 10. POP Procedure: POP(STACK, TOP, ITEM) 1.If TOP=0 then Print UNDERFLOW and Exit. Else Set Item:=STACK[TOP] Set TOP:=TOP-1 2. Return C B A Max= 4 Top= 3 Let Max=4 TOP=3 Step 1: Top=0 3=0False Go to ELSE Part Item=Stack[Top] = Stack[3] Item= C Top=Top-1 =3-1=2 Top=2 2. Return B A Max= 4 Top= 2 CASE 1 Max= 4 Top= 0 CASE 2 Let Max=4 TOP=0 Step 1: Top=0 0=0True Print UNDERFLOW 2. Return
  • 11. EVALUATION OF EXPRESSION The way to write arithmetic expression is known as a notation. An arithmetic expression can be written in three different but equivalent notations. These notations are − • Infix Notation • Prefix (Polish) Notation • Postfix (Reverse-Polish) Notation
  • 12. Infix Notation • We write expression in infix notation, e.g. a - b + c, where operators are used in- between operands. Prefix Notation • In this notation, operator is prefixed to operands, i.e. operator is written ahead of operands. For example, +ab. • Prefix notation is also known as Polish Notation. Postfix Notation • This is known as Reversed Polish Notation. • In this notation style, the operator is postfixed to the operands i.e., the operator is written after the operands. For example, ab+.
  • 13. Postfix Evaluation Algorithm • Step 1 − scan the expression from left to right • Step 2 − if it is an operand push it to stack • Step 3 − if it is an operator pull operand from stack and perform operation • Step 4 − store the output of step 3, back to stack • Step 5 − scan the expression until all operands are consumed • Step 6 − pop the stack and perform operation
  • 14. • Read all the symbols one by one from left to right in the given Postfix Expression • If the reading symbol is operand, then push it on to the Stack. • If the reading symbol is operator (+ , - , * , / etc.,), then perform TWO pop operations and store the two popped oparands in two different variables (operand1 and operand2). Then perform reading symbol operation using operand1 and operand2 and push result back on to the Stack. • Finally! perform a pop operation and display the popped value as final result.
  • 15. Step 1 − scan the expression from left to right Step 2 − if it is an operand push it to stack Step 3 − if it is an operator pull operand from stack and perform operation Step 4 − store the output of step 3, back to stack Step 5 − scan the expression until all operands are consumed Step 6 − pop the stack and perform operation
  • 16. Steps to convert infix expression to postfix 1. Scan the given infix expression from left to right. 2. If the scanned character is an operand, then output it. 3. Else, • If the precedence of the scanned operator is greater than the precedence of the operator in the top of the stack(or the stack is empty or if the stack contains a ‘(‘ ), push it. • Else, Pop all operators from the stack whose precedence is greater than or equal to that of the scanned operator. Then, push the scanned operator to the top of the stack. (If you encounter parenthesis while popping then stop there and push the scanned operator in the stack.) 4. If the scanned character is ‘(‘, push it to the stack. 5. If the scanned character is ‘)’, pop the stack and output characters until ‘(‘ is encountered, and discard both the parenthesis. 6. Repeat steps 2-5 until infix expression is scanned completely. 7. Print the output. 8. Pop and output from the stack.
  • 17. INFIX EXPRESSION: a+(b-c/d)-e Read char a—operand (output it) stack->empty a Read char  +operator push (+) Read char  (operator push ( ( ) Read char b—operand (output it) a b Read char  - operator push (-) Read char c—operand (output it) - ( a b c -Read char  /operator push (/) ( + + + ( + ( + - ( + - ( + / - ( + Read char d—operand (output it) a b c d Read char  )operator pop ( upto ( ) a b c d / - Read char  - operator push (-) a b c d / - - and + have equal precedence but + have higher order. So Pop + and Push - a b c d / - + Read char e—operand (output it) - ( a b c d / - + e - Expression scanned completely ,pop element from stack until it will empty. + + a b c d / - + e - 1. Scan the given infix expression from left to right. 2. If the scanned character is an operand, then output it. 3. Else, • If the precedence of the scanned operator is greater than the precedence of the operator in the top of the stack(or the stack is empty or if the stack contains a ‘(‘ ), push it. • Else, Pop all operators from the stack whose precedence is greater than or equal to that of the scanned operator. Then, push the scanned operator to the top of the stack. (If you encounter parenthesis while popping then stop there and push the scanned operator in the stack.) 4. If the scanned character is ‘(‘, push it to the stack. 5. If the scanned character is ‘)’, pop the stack and and output characters until ‘(‘ is encountered, and discard both the parenthesis. 6. Repeat steps 2-5 until infix expression is scanned completely. 7. Print the output. 8. Pop and output from the stack. / - ( + + - - + - INFIX EXPRESSION: a+(b-c/d)-e POSTFIX EXPRESSION: a b c d / - + e -