SlideShare a Scribd company logo
1 of 77
Prepared By: 
Harekrushna Patel 
Tejas Patel
 A Stack is a Linear Data Structure. 
 The Data Items in the stack are inserted and 
deleted from one end. That is called Top of the 
stack. 
 Stack follows LIFO (Last In First Out) 
Mechanism. 
 Stacks are mostly used in system software. 
Like compilers and Operating Systems, etc..
 Insert values into stack is called push 
operation. 
 Reading Values from stack is called pop 
operation. 
 The Stack starts with position of 0(zero) . 
 The Maximum Position of Stack is n-1.
 When The position of Stack is zero. Then, the 
stack is underflow. 
 When The position of Stack is grater than n-1. 
Then, the stack is called overflow. 
 Once an item is popped from stack, it is no 
longer available.
The bunch of plates in the kitchen 
Books on a floor
When TOS=-1 and we try to operate pop 
operation ‘empty stack’– ‘Underflow’ 
TOS=-1
Push Operation 
Here Top Of Stack=-1,Max=10 
TOS=-1
Push Operation 
Here Top Of Stack will become 0,Max=10, 
To insert some values like 
12, 
12 
TOS=0
Push Operation 
Here Top Of Stack will become 1,Max=10, 
To insert some values like 
12,34 
12 34 
TOS=1
Push Operation 
Here Top Of Stack will become 2,Max=10, 
To insert some values like 
12,34,17 
12 34 17 
TOS=2
Push Operation 
Here Top Of Stack will become 3,Max=10, 
To insert some values like 
12,34,17,26 
12 34 17 26 
TOS=3
Push Operation 
Here Top Of Stack will become 4,Max=10, 
To insert some values like 
12,34,17,26,46 
12 34 17 26 46 
TOS=4
Push Operation 
Here Top Of Stack will become 5,Max=10, 
To insert some values like 
12,34,17,26,46,53 
12 34 17 26 46 53 
TOS=5
Pop Operation 
Here Top Of Stack will become 4,Max=10 
12 34 17 26 46 
TOS=4
Push Operation 
Here Top Of Stack will become 5,Max=10 
Let’s insert value say 
76 
12 34 17 26 46 76 
TOS=5
Push Operation 
Here Top Of Stack will become 6,Max=10 
Let’s insert value say 
76,28 
12 34 17 26 46 76 28 
TOS=6
Push Operation 
Here Top Of Stack will become 7,Max=10 
Let’s insert value say 
76,28,39 
12 34 17 26 46 76 28 39 
TOS=7
• Push Operation 
Here Top Of Stack will become 8,Max=10 
Let’s insert value say 
76,28,39,20 
12 34 17 26 46 76 28 39 20 
TOS=8
Push Operation 
Here Top Of Stack will become 9,Max=10 
Let’s insert value say 
76,28,39,20,9 
12 34 17 26 46 76 28 39 20 9 
TOS=9
Push Operation 
Here Top Of Stack will become 10 and Max is 
also 10 
Now, When we are inserting element say 87 
‘stack is full’ Called ‘Overflow’ 
12 34 17 26 46 76 28 39 20 9 
TOS=10
Completely parenthesize the infix expression 
Move each operator to the space held by the 
corresponding right parenthesis 
Remove all parentheses
infixExp 
( a + b - c ) * d – ( e + f ) 
postfixExp
infixExp 
a + b - c ) * d – ( e + f ) 
postfixExp 
( 
stackExp
infixExp 
+ b - c ) * d – ( e + f ) 
postfixExp 
( 
a 
stackExp
infixExp 
b - c ) * d – ( e + f ) 
postfixExp 
( 
a 
+ 
stackExp
infixExp 
- c ) * d – ( e + f ) 
postfixExp 
( 
a b 
+ 
stackExp
infixExp 
c ) * d – ( e + f ) 
postfixExp 
( 
a b + 
- 
stackExp
infixExp 
) * d – ( e + f ) 
postfixExp 
( 
a b + c 
- 
stackExp
infixExp 
* d – ( e + f ) 
postfixExp 
a b + c - 
stackExp
infixExp 
d – ( e + f ) 
postfixExp 
a b + c - 
* 
stackExp
infixExp 
– ( e + f ) 
postfixExp 
a b + c - d 
* 
stackExp
infixExp 
( e + f ) 
postfixExp 
a b + c – d * 
- 
stackExp
infixExp 
e + f ) 
postfixExp 
a b + c – d * 
( 
- 
stackExp
infixExp 
+ f ) 
postfixExp 
a b + c – d * e 
( 
- 
stackExp
infixExp 
f ) 
postfixExp 
a b + c – d * e 
+ 
( 
- 
stackExp
infixExp 
) 
postfixExp 
a b + c – d * e f 
+ 
( 
- 
stackExp
infixExp 
postfixExp 
a b + c – d * e f + 
- 
stackExp
infixExp 
postfixExp 
a b + c – d * e f + - 
stackExp
Steps: 
1. Scan the expression from left to right. 
2. Initialise an empty stack. 
3. If the scanned character is an operand, add it to 
the stack. If the scanned character is an 
operator, there will be at least two operands in 
the stack.
4. If the scanned character is an Operator, then we 
store the top most element of the stack(top 
Stack) in a variable temp. Pop the stack. Now 
evaluate top Stack(Operator) temp. Let the 
result of this operation be ret Val. Pop the stack 
and Push ret Val into the stack. 
 Repeat this step till all the characters are 
scanned.
6. After all characters are scanned, we will have 
only one element in the stack. Return top Stack.
636+5*9/- // original expression
636+5*9/- // original expression 
695*9/- // 3+6 evaluated
636+5*9/- // original expression 
695*9/- // 3+6 evaluated 
6459/- // 6*9 evaluated
636+5*9/- // original expression 
695*9/- // 3+6 evaluated 
6459/- // 6*9 evaluated 
65- // 45/9 evaluated
636+5*9/- // original expression 
695*9/- // 3+6 evaluated 
6459/- // 6*9 evaluated 
65- // 45/9 evaluated 
1 // 6-5 evaluated
Steps: 
1. Scan the expression from right to left. 
2. An operand is pushed on top of the stack. 
3. In case of operator, two operand are popped 
from the stack, evaluated and pushed back on to 
stack. 
4. Final result is found on top of stack.
* - + 4 3 5 / + 2 4 3 // original expression
* - + 4 3 5 / + 2 4 3 // original expression 
* - 7 5 / + 2 4 3 // 4+3 evaluated
* - + 4 3 5 / + 2 4 3 // original expression 
* - 7 5 / + 2 4 3 // 4+3 evaluated 
* 2 / + 2 4 3 // 7-5 evaluated
* - + 4 3 5 / + 2 4 3 // original expression 
* - 7 5 / + 2 4 3 // 4+3 evaluated 
* 2 / + 2 4 3 // 7-5 evaluated 
* 2 / 6 3 // 2+4 evaluated
* - + 4 3 5 / + 2 4 3 // original expression 
* - 7 5 / + 2 4 3 // 4+3 evaluated 
* 2 / + 2 4 3 // 7-5 evaluated 
* 2 / 6 3 // 2+4 evaluated 
* 2 2 // 6/3 evaluated
* - + 4 3 5 / + 2 4 3 // original expression 
* - 7 5 / + 2 4 3 // 4+3 evaluated 
* 2 / + 2 4 3 // 7-5 evaluated 
* 2 / 6 3 // 2+4 evaluated 
* 2 2 // 6/3 evaluated 
4 // 2*2 evaluated
• Any sort of nesting (such as parentheses) 
• Evaluating arithmetic expressions (and other 
sorts of expression) 
• Implementing function or method calls 
• Keeping track of previous choices (as in 
backtracking) 
• Keeping track of choices yet to be made (as in 
creating a maze)
Steps: 
1. Scan the Infix string from left to right. 
2. Initialise an empty stack. 
3. If the scanned character is an operand, add it to 
the Postfix string. If the scanned character is an 
operator and if the stack is empty Push the 
character to stack.
4. If the scanned character is an Operator and the 
stack is not empty, compare the precedence of 
the character with the element on top of the 
stack (top Stack). If top Stack has higher 
precedence over the scanned character Pop the 
stack else Push the scanned character to stack. 
Repeat this step as long as stack is not empty 
and top Stack has precedence over the 
character.
5.Repeat this step till all the characters are 
scanned. 
6. After all characters are scanned, we have to add 
any character that the stack may have to the 
Postfix string. If stack is not empty add top Stack 
to Postfix string and Pop the stack. Repeat this 
step as long as stack is not empty.
Infix 
A+B 
A+B-C 
(A+B)*(C-D) 
A$B*C-D+E/F/(G+H) 
((A+B)*C(DE))$ 
(F+G) 
A-B/(C*D$E) 
Postfix 
AB+ 
AB+C-AB+ 
CD-* 
AB$C*D-EF/GH+/+ 
AB+C*DE--FG+$ 
ABCDE$*/-
Steps: 
1. Reverse the infix expression. 
2. Make every ‘(‘ (opening bracket) as ‘)’ (closing 
bracket) and vice versa. 
3. Convert the modified expression to postfix form. 
4. Reverse the postfix expression.
Infix 
A+B 
A+B-C 
(A+B)*(C-D) 
A$B*C-D+E/F/(G+H) 
((A+B)*C(DE))$ 
(F+G) 
A-B/(C*D$E) 
Prefix 
+AB 
-+ABC 
*+AB-CD 
+-*$ABCD//EF+GH 
$-*+ABC-DE+FG 
-A/B*C$DE
Goal: Move stack of rings to another peg 
 Rule 1: May move only 1 ring at a time 
 Rule 2: May never have larger ring on top of 
smaller ring
A B C
A B C
A B C
A B C
A B C
A B C
A B C
A B C
A B C
A B C
A B C
A B C
A B C
A B C
A B C
A B C

More Related Content

What's hot

Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7
Kumar
 
Stack data structure
Stack data structureStack data structure
Stack data structure
Tech_MX
 

What's hot (20)

Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTURE
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7
 
Stack Data Structure V1.0
Stack Data Structure V1.0Stack Data Structure V1.0
Stack Data Structure V1.0
 
Conversion of Infix To Postfix Expressions
Conversion of Infix To Postfix Expressions Conversion of Infix To Postfix Expressions
Conversion of Infix To Postfix Expressions
 
stack
stackstack
stack
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
Stack
StackStack
Stack
 
Stacks
StacksStacks
Stacks
 
Stack Operation In Data Structure
Stack Operation In Data Structure Stack Operation In Data Structure
Stack Operation In Data Structure
 
Stack - Data Structure - Notes
Stack - Data Structure - NotesStack - Data Structure - Notes
Stack - Data Structure - Notes
 
Unit 3 stack
Unit   3 stackUnit   3 stack
Unit 3 stack
 
Polish
PolishPolish
Polish
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 
Stacks
StacksStacks
Stacks
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
Ds stack 03
Ds stack 03Ds stack 03
Ds stack 03
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
Stacks in Data Structure
Stacks in Data StructureStacks in Data Structure
Stacks in Data Structure
 
Stack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADTStack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADT
 

Viewers also liked

Add creativity to your decision process1
Add creativity to your decision process1Add creativity to your decision process1
Add creativity to your decision process1
Siddharth Kumar Kadamb
 
Fungsi tombol pada keyboard
Fungsi tombol pada keyboardFungsi tombol pada keyboard
Fungsi tombol pada keyboard
Dancha GazeRockz
 
Slide share
Slide shareSlide share
Slide share
hmcorona
 
AtlasCopco_Partner
AtlasCopco_PartnerAtlasCopco_Partner
AtlasCopco_Partner
Ayca Arabaci
 
Pendidikan sebagai ilmu
Pendidikan sebagai ilmuPendidikan sebagai ilmu
Pendidikan sebagai ilmu
Naning I. F
 

Viewers also liked (20)

Add creativity to your decision process1
Add creativity to your decision process1Add creativity to your decision process1
Add creativity to your decision process1
 
Club penguin
Club penguinClub penguin
Club penguin
 
Leap Motion
Leap MotionLeap Motion
Leap Motion
 
Fungsi tombol pada keyboard
Fungsi tombol pada keyboardFungsi tombol pada keyboard
Fungsi tombol pada keyboard
 
Writing Inspiration
Writing InspirationWriting Inspiration
Writing Inspiration
 
Ltc pp mgt307_wk4 Copyright 2013 Edward F. T. Charfauros. Reference, www.Your...
Ltc pp mgt307_wk4 Copyright 2013 Edward F. T. Charfauros. Reference, www.Your...Ltc pp mgt307_wk4 Copyright 2013 Edward F. T. Charfauros. Reference, www.Your...
Ltc pp mgt307_wk4 Copyright 2013 Edward F. T. Charfauros. Reference, www.Your...
 
Getting started quick tips
Getting started quick tipsGetting started quick tips
Getting started quick tips
 
Presentació del treball final
Presentació del treball finalPresentació del treball final
Presentació del treball final
 
Сборка Котлов-2015. Этиологическое знание
Сборка Котлов-2015. Этиологическое знаниеСборка Котлов-2015. Этиологическое знание
Сборка Котлов-2015. Этиологическое знание
 
Slide share
Slide shareSlide share
Slide share
 
Templete puppy
Templete puppyTemplete puppy
Templete puppy
 
Онтологический верстак 21 декабря 2014 года. Тектоника плит
Онтологический верстак 21 декабря 2014 года. Тектоника плитОнтологический верстак 21 декабря 2014 года. Тектоника плит
Онтологический верстак 21 декабря 2014 года. Тектоника плит
 
AtlasCopco_Partner
AtlasCopco_PartnerAtlasCopco_Partner
AtlasCopco_Partner
 
Transcript+Cer
Transcript+CerTranscript+Cer
Transcript+Cer
 
الذكاءات المتعددة والفهم تأليف د.جابر عبدالحميدجابر
الذكاءات المتعددة والفهم تأليف د.جابر عبدالحميدجابرالذكاءات المتعددة والفهم تأليف د.جابر عبدالحميدجابر
الذكاءات المتعددة والفهم تأليف د.جابر عبدالحميدجابر
 
Comments on "Large Estimates of the Elasticity of Intertemporal Substitution:...
Comments on "Large Estimates of the Elasticity of Intertemporal Substitution:...Comments on "Large Estimates of the Elasticity of Intertemporal Substitution:...
Comments on "Large Estimates of the Elasticity of Intertemporal Substitution:...
 
Slide hm 2015 indo
Slide hm 2015 indoSlide hm 2015 indo
Slide hm 2015 indo
 
Seite in magazin- Active Citizens - Dusseldorf (Germany)
Seite in magazin- Active Citizens - Dusseldorf (Germany)Seite in magazin- Active Citizens - Dusseldorf (Germany)
Seite in magazin- Active Citizens - Dusseldorf (Germany)
 
Marketing Infographic
Marketing InfographicMarketing Infographic
Marketing Infographic
 
Pendidikan sebagai ilmu
Pendidikan sebagai ilmuPendidikan sebagai ilmu
Pendidikan sebagai ilmu
 

Similar to Stack

Lec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptxLec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptx
haaamin01
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
chandankumar364348
 
The concept of stack is extremely important in computer science and .pdf
The concept of stack is extremely important in computer science and .pdfThe concept of stack is extremely important in computer science and .pdf
The concept of stack is extremely important in computer science and .pdf
arihantsherwani
 

Similar to Stack (20)

Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
 
Stack
StackStack
Stack
 
Stack in Data Structure
Stack in Data StructureStack in Data Structure
Stack in Data Structure
 
Data structures stacks
Data structures   stacksData structures   stacks
Data structures stacks
 
DS MOD2 (1) (1).pptx
DS MOD2 (1) (1).pptxDS MOD2 (1) (1).pptx
DS MOD2 (1) (1).pptx
 
Stack
StackStack
Stack
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
 
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptxApplication of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
 
Stack
StackStack
Stack
 
DS UNIT1_STACKS.pptx
DS UNIT1_STACKS.pptxDS UNIT1_STACKS.pptx
DS UNIT1_STACKS.pptx
 
Lec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptxLec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptx
 
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
 
Applicationofstack by Ali F.RAshid
Applicationofstack  by Ali F.RAshid Applicationofstack  by Ali F.RAshid
Applicationofstack by Ali F.RAshid
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
 
Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)
 
stacks and queues
stacks and queuesstacks and queues
stacks and queues
 
358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9
 
The concept of stack is extremely important in computer science and .pdf
The concept of stack is extremely important in computer science and .pdfThe concept of stack is extremely important in computer science and .pdf
The concept of stack is extremely important in computer science and .pdf
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
ssuserdda66b
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 

Recently uploaded (20)

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 

Stack

  • 1. Prepared By: Harekrushna Patel Tejas Patel
  • 2.  A Stack is a Linear Data Structure.  The Data Items in the stack are inserted and deleted from one end. That is called Top of the stack.  Stack follows LIFO (Last In First Out) Mechanism.  Stacks are mostly used in system software. Like compilers and Operating Systems, etc..
  • 3.  Insert values into stack is called push operation.  Reading Values from stack is called pop operation.  The Stack starts with position of 0(zero) .  The Maximum Position of Stack is n-1.
  • 4.  When The position of Stack is zero. Then, the stack is underflow.  When The position of Stack is grater than n-1. Then, the stack is called overflow.  Once an item is popped from stack, it is no longer available.
  • 5. The bunch of plates in the kitchen Books on a floor
  • 6. When TOS=-1 and we try to operate pop operation ‘empty stack’– ‘Underflow’ TOS=-1
  • 7. Push Operation Here Top Of Stack=-1,Max=10 TOS=-1
  • 8. Push Operation Here Top Of Stack will become 0,Max=10, To insert some values like 12, 12 TOS=0
  • 9. Push Operation Here Top Of Stack will become 1,Max=10, To insert some values like 12,34 12 34 TOS=1
  • 10. Push Operation Here Top Of Stack will become 2,Max=10, To insert some values like 12,34,17 12 34 17 TOS=2
  • 11. Push Operation Here Top Of Stack will become 3,Max=10, To insert some values like 12,34,17,26 12 34 17 26 TOS=3
  • 12. Push Operation Here Top Of Stack will become 4,Max=10, To insert some values like 12,34,17,26,46 12 34 17 26 46 TOS=4
  • 13. Push Operation Here Top Of Stack will become 5,Max=10, To insert some values like 12,34,17,26,46,53 12 34 17 26 46 53 TOS=5
  • 14. Pop Operation Here Top Of Stack will become 4,Max=10 12 34 17 26 46 TOS=4
  • 15. Push Operation Here Top Of Stack will become 5,Max=10 Let’s insert value say 76 12 34 17 26 46 76 TOS=5
  • 16. Push Operation Here Top Of Stack will become 6,Max=10 Let’s insert value say 76,28 12 34 17 26 46 76 28 TOS=6
  • 17. Push Operation Here Top Of Stack will become 7,Max=10 Let’s insert value say 76,28,39 12 34 17 26 46 76 28 39 TOS=7
  • 18. • Push Operation Here Top Of Stack will become 8,Max=10 Let’s insert value say 76,28,39,20 12 34 17 26 46 76 28 39 20 TOS=8
  • 19. Push Operation Here Top Of Stack will become 9,Max=10 Let’s insert value say 76,28,39,20,9 12 34 17 26 46 76 28 39 20 9 TOS=9
  • 20. Push Operation Here Top Of Stack will become 10 and Max is also 10 Now, When we are inserting element say 87 ‘stack is full’ Called ‘Overflow’ 12 34 17 26 46 76 28 39 20 9 TOS=10
  • 21. Completely parenthesize the infix expression Move each operator to the space held by the corresponding right parenthesis Remove all parentheses
  • 22. infixExp ( a + b - c ) * d – ( e + f ) postfixExp
  • 23. infixExp a + b - c ) * d – ( e + f ) postfixExp ( stackExp
  • 24. infixExp + b - c ) * d – ( e + f ) postfixExp ( a stackExp
  • 25. infixExp b - c ) * d – ( e + f ) postfixExp ( a + stackExp
  • 26. infixExp - c ) * d – ( e + f ) postfixExp ( a b + stackExp
  • 27. infixExp c ) * d – ( e + f ) postfixExp ( a b + - stackExp
  • 28. infixExp ) * d – ( e + f ) postfixExp ( a b + c - stackExp
  • 29. infixExp * d – ( e + f ) postfixExp a b + c - stackExp
  • 30. infixExp d – ( e + f ) postfixExp a b + c - * stackExp
  • 31. infixExp – ( e + f ) postfixExp a b + c - d * stackExp
  • 32. infixExp ( e + f ) postfixExp a b + c – d * - stackExp
  • 33. infixExp e + f ) postfixExp a b + c – d * ( - stackExp
  • 34. infixExp + f ) postfixExp a b + c – d * e ( - stackExp
  • 35. infixExp f ) postfixExp a b + c – d * e + ( - stackExp
  • 36. infixExp ) postfixExp a b + c – d * e f + ( - stackExp
  • 37. infixExp postfixExp a b + c – d * e f + - stackExp
  • 38. infixExp postfixExp a b + c – d * e f + - stackExp
  • 39. Steps: 1. Scan the expression from left to right. 2. Initialise an empty stack. 3. If the scanned character is an operand, add it to the stack. If the scanned character is an operator, there will be at least two operands in the stack.
  • 40. 4. If the scanned character is an Operator, then we store the top most element of the stack(top Stack) in a variable temp. Pop the stack. Now evaluate top Stack(Operator) temp. Let the result of this operation be ret Val. Pop the stack and Push ret Val into the stack.  Repeat this step till all the characters are scanned.
  • 41. 6. After all characters are scanned, we will have only one element in the stack. Return top Stack.
  • 42. 636+5*9/- // original expression
  • 43. 636+5*9/- // original expression 695*9/- // 3+6 evaluated
  • 44. 636+5*9/- // original expression 695*9/- // 3+6 evaluated 6459/- // 6*9 evaluated
  • 45. 636+5*9/- // original expression 695*9/- // 3+6 evaluated 6459/- // 6*9 evaluated 65- // 45/9 evaluated
  • 46. 636+5*9/- // original expression 695*9/- // 3+6 evaluated 6459/- // 6*9 evaluated 65- // 45/9 evaluated 1 // 6-5 evaluated
  • 47. Steps: 1. Scan the expression from right to left. 2. An operand is pushed on top of the stack. 3. In case of operator, two operand are popped from the stack, evaluated and pushed back on to stack. 4. Final result is found on top of stack.
  • 48. * - + 4 3 5 / + 2 4 3 // original expression
  • 49. * - + 4 3 5 / + 2 4 3 // original expression * - 7 5 / + 2 4 3 // 4+3 evaluated
  • 50. * - + 4 3 5 / + 2 4 3 // original expression * - 7 5 / + 2 4 3 // 4+3 evaluated * 2 / + 2 4 3 // 7-5 evaluated
  • 51. * - + 4 3 5 / + 2 4 3 // original expression * - 7 5 / + 2 4 3 // 4+3 evaluated * 2 / + 2 4 3 // 7-5 evaluated * 2 / 6 3 // 2+4 evaluated
  • 52. * - + 4 3 5 / + 2 4 3 // original expression * - 7 5 / + 2 4 3 // 4+3 evaluated * 2 / + 2 4 3 // 7-5 evaluated * 2 / 6 3 // 2+4 evaluated * 2 2 // 6/3 evaluated
  • 53. * - + 4 3 5 / + 2 4 3 // original expression * - 7 5 / + 2 4 3 // 4+3 evaluated * 2 / + 2 4 3 // 7-5 evaluated * 2 / 6 3 // 2+4 evaluated * 2 2 // 6/3 evaluated 4 // 2*2 evaluated
  • 54. • Any sort of nesting (such as parentheses) • Evaluating arithmetic expressions (and other sorts of expression) • Implementing function or method calls • Keeping track of previous choices (as in backtracking) • Keeping track of choices yet to be made (as in creating a maze)
  • 55. Steps: 1. Scan the Infix string from left to right. 2. Initialise an empty stack. 3. If the scanned character is an operand, add it to the Postfix string. If the scanned character is an operator and if the stack is empty Push the character to stack.
  • 56. 4. If the scanned character is an Operator and the stack is not empty, compare the precedence of the character with the element on top of the stack (top Stack). If top Stack has higher precedence over the scanned character Pop the stack else Push the scanned character to stack. Repeat this step as long as stack is not empty and top Stack has precedence over the character.
  • 57. 5.Repeat this step till all the characters are scanned. 6. After all characters are scanned, we have to add any character that the stack may have to the Postfix string. If stack is not empty add top Stack to Postfix string and Pop the stack. Repeat this step as long as stack is not empty.
  • 58. Infix A+B A+B-C (A+B)*(C-D) A$B*C-D+E/F/(G+H) ((A+B)*C(DE))$ (F+G) A-B/(C*D$E) Postfix AB+ AB+C-AB+ CD-* AB$C*D-EF/GH+/+ AB+C*DE--FG+$ ABCDE$*/-
  • 59. Steps: 1. Reverse the infix expression. 2. Make every ‘(‘ (opening bracket) as ‘)’ (closing bracket) and vice versa. 3. Convert the modified expression to postfix form. 4. Reverse the postfix expression.
  • 60. Infix A+B A+B-C (A+B)*(C-D) A$B*C-D+E/F/(G+H) ((A+B)*C(DE))$ (F+G) A-B/(C*D$E) Prefix +AB -+ABC *+AB-CD +-*$ABCD//EF+GH $-*+ABC-DE+FG -A/B*C$DE
  • 61. Goal: Move stack of rings to another peg  Rule 1: May move only 1 ring at a time  Rule 2: May never have larger ring on top of smaller ring
  • 62. A B C
  • 63. A B C
  • 64. A B C
  • 65. A B C
  • 66. A B C
  • 67. A B C
  • 68. A B C
  • 69. A B C
  • 70. A B C
  • 71. A B C
  • 72. A B C
  • 73. A B C
  • 74. A B C
  • 75. A B C
  • 76. A B C
  • 77. A B C