SlideShare a Scribd company logo
1 of 27
Stack
By
Nilesh Dalvi
Lecturer, Patkar-Varde College.Lecturer, Patkar-Varde College.
http://www.slideshare.net/nileshdalvi01
Java and DataJava and Data
StructuresStructures
Stack
• A Stack is a list of elements in which an element may
be inserted or deleted at one end which is known as
TOP of the stack.
• Operation Performed On Array:
1. Push: add an element in stack
2. Pop: remove an element in stack
3. Peek :Current processed element
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
a
b
c TOP
Array representation of stacks:
• We can represent a stack in computer in various ways, by
means of one-way-list or linear array.
• Consider Linear array stack:
• TOP : Location of the top element of the stack
• MAXSTK : max elements that can be held by stack
• If TOP = 0 or TOP = NULL indicates that stack is empty.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
XX YY ZZ
1 2 3 4 5 6 7 8
TOP MAXSTK
Adding element: PUSH()
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Algorithm PUSH (STACK, ITEM)
{
if (TOP = MAXSTK) then
write ("Overflow");
else
TOP := TOP + 1;
STACK [TOP] := ITEM;
}
Deleting element: POP()
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Algorithm POP (STACK, ITEM)
{
if (TOP = 0) then
write ("Underflow");
else
ITEM = STACK [TOP];
TOP := TOP - 1;
}
Accessing element: PEEK()
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Algorithm PEEK (STACK)
{
if (TOP = 0) then
write ("Underflow");
else
Return STACK [TOP];
}
Implementation of Stack operations
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Applications of Stack:
• Recursion
• Infix to postfix conversion
• Postfix to infix conversion
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
infixVect
postfixVect
( a + b - c ) * d – ( e + f )
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Infix to postfix conversion
infixVect
postfixVect
a + b - c ) * d – ( e + f )
(
Infix to postfix conversion
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
infixVect
postfixVect
+ b - c ) * d – ( e + f )
(
a
Infix to postfix conversion
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
infixVect
postfixVect
b - c ) * d – ( e + f )
(
a
+
Infix to postfix conversion
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
infixVect
postfixVect
- c ) * d – ( e + f )
(
a b
+
Infix to postfix conversion
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
infixVect
postfixVect
c ) * d – ( e + f )
(
a b +
-
Infix to postfix conversion
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
infixVect
postfixVect
) * d – ( e + f )
(
a b + c
-
Infix to postfix conversion
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
infixVect
postfixVect
* d – ( e + f )
a b + c -
Infix to postfix conversion
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
infixVect
postfixVect
d – ( e + f )
a b + c -
*
Infix to postfix conversion
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
infixVect
postfixVect
– ( e + f )
a b + c - d
*
Infix to postfix conversion
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
infixVect
postfixVect
( e + f )
a b + c – d *
-
Infix to postfix conversion
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
infixVect
postfixVect
e + f )
a b + c – d *
-
(
Infix to postfix conversion
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
infixVect
postfixVect
+ f )
a b + c – d * e
-
(
Infix to postfix conversion
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
infixVect
postfixVect
f )
a b + c – d * e
-
(
+
Infix to postfix conversion
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
infixVect
postfixVect
)
a b + c – d * e f
-
(
+
Infix to postfix conversion
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
infixVect
postfixVect
a b + c – d * e f +
-
Infix to postfix conversion
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
infixVect
postfixVect
a b + c – d * e f + -
Infix to postfix conversion
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Algorithm for Infix to Postfix
1) Examine the next element in the input.
2) If it is operand, output it.
3) If it is opening parenthesis, push it on stack.
4) If it is an operator, then
i) If stack is empty, push operator on stack.
ii) If the top of stack is opening parenthesis, push operator on stack
iii) If it has higher priority than the top of stack, push operator on stack.
iv) Else pop the operator from the stack and output it, repeat step 4
5) If it is a closing parenthesis, pop operators from stack and output them
until an opening parenthesis is encountered. pop and discard the opening
parenthesis.
6) If there is more input go to step 1
7) If there is no more input, pop the remaining operators to output.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Q & A

More Related Content

Viewers also liked

6. Exception Handling
6. Exception Handling6. Exception Handling
6. Exception HandlingNilesh Dalvi
 
5. Inheritances, Packages and Intefaces
5. Inheritances, Packages and Intefaces5. Inheritances, Packages and Intefaces
5. Inheritances, Packages and IntefacesNilesh Dalvi
 
3. Data types and Variables
3. Data types and Variables3. Data types and Variables
3. Data types and VariablesNilesh Dalvi
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in javaNilesh Dalvi
 
Chinese Cultural Entailment 中国文化蕴涵
Chinese Cultural Entailment  中国文化蕴涵Chinese Cultural Entailment  中国文化蕴涵
Chinese Cultural Entailment 中国文化蕴涵John Jeffery
 
TDC2016SP - Otimização Prematura: a Raíz de Todo o Mal
TDC2016SP - Otimização Prematura: a Raíz de Todo o MalTDC2016SP - Otimização Prematura: a Raíz de Todo o Mal
TDC2016SP - Otimização Prematura: a Raíz de Todo o Maltdc-globalcode
 
Actividad de aprendizaje 2 SEGUNDO BLOQUE
Actividad de aprendizaje 2 SEGUNDO BLOQUEActividad de aprendizaje 2 SEGUNDO BLOQUE
Actividad de aprendizaje 2 SEGUNDO BLOQUEPaolachable
 
TDC2016SP - Desacoplando suas regras de negócio do Rails
TDC2016SP - Desacoplando suas regras de negócio do RailsTDC2016SP - Desacoplando suas regras de negócio do Rails
TDC2016SP - Desacoplando suas regras de negócio do Railstdc-globalcode
 
Como hacer una pagina en wix
Como hacer una pagina en wixComo hacer una pagina en wix
Como hacer una pagina en wixwiston98
 
TDC2016SP - Finanças Quantitativas com Python
TDC2016SP - Finanças Quantitativas com PythonTDC2016SP - Finanças Quantitativas com Python
TDC2016SP - Finanças Quantitativas com Pythontdc-globalcode
 
取是一種本事捨是一種智慧
取是一種本事捨是一種智慧取是一種本事捨是一種智慧
取是一種本事捨是一種智慧Jaing Lai
 
TDC2016SP - Groovy como você nunca viu
TDC2016SP - Groovy como você nunca viuTDC2016SP - Groovy como você nunca viu
TDC2016SP - Groovy como você nunca viutdc-globalcode
 
Blog diapocitiva ventajas y desventajas
Blog diapocitiva ventajas y desventajasBlog diapocitiva ventajas y desventajas
Blog diapocitiva ventajas y desventajasNELLYS29
 
Exposicion final
Exposicion finalExposicion final
Exposicion finalPerson0001
 
01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seoJinTaek Seo
 

Viewers also liked (19)

7. Multithreading
7. Multithreading7. Multithreading
7. Multithreading
 
6. Exception Handling
6. Exception Handling6. Exception Handling
6. Exception Handling
 
5. Inheritances, Packages and Intefaces
5. Inheritances, Packages and Intefaces5. Inheritances, Packages and Intefaces
5. Inheritances, Packages and Intefaces
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
3. Data types and Variables
3. Data types and Variables3. Data types and Variables
3. Data types and Variables
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
 
Chinese Cultural Entailment 中国文化蕴涵
Chinese Cultural Entailment  中国文化蕴涵Chinese Cultural Entailment  中国文化蕴涵
Chinese Cultural Entailment 中国文化蕴涵
 
TDC2016SP - Otimização Prematura: a Raíz de Todo o Mal
TDC2016SP - Otimização Prematura: a Raíz de Todo o MalTDC2016SP - Otimização Prematura: a Raíz de Todo o Mal
TDC2016SP - Otimização Prematura: a Raíz de Todo o Mal
 
Actividad de aprendizaje 2 SEGUNDO BLOQUE
Actividad de aprendizaje 2 SEGUNDO BLOQUEActividad de aprendizaje 2 SEGUNDO BLOQUE
Actividad de aprendizaje 2 SEGUNDO BLOQUE
 
TDC2016SP - Desacoplando suas regras de negócio do Rails
TDC2016SP - Desacoplando suas regras de negócio do RailsTDC2016SP - Desacoplando suas regras de negócio do Rails
TDC2016SP - Desacoplando suas regras de negócio do Rails
 
Como hacer una pagina en wix
Como hacer una pagina en wixComo hacer una pagina en wix
Como hacer una pagina en wix
 
TDC2016SP - Finanças Quantitativas com Python
TDC2016SP - Finanças Quantitativas com PythonTDC2016SP - Finanças Quantitativas com Python
TDC2016SP - Finanças Quantitativas com Python
 
取是一種本事捨是一種智慧
取是一種本事捨是一種智慧取是一種本事捨是一種智慧
取是一種本事捨是一種智慧
 
Por que sua próxima aplicação web deve ser em Clojure?
Por que sua próxima aplicação web deve ser em Clojure?Por que sua próxima aplicação web deve ser em Clojure?
Por que sua próxima aplicação web deve ser em Clojure?
 
TDC2016SP - Groovy como você nunca viu
TDC2016SP - Groovy como você nunca viuTDC2016SP - Groovy como você nunca viu
TDC2016SP - Groovy como você nunca viu
 
Blog diapocitiva ventajas y desventajas
Blog diapocitiva ventajas y desventajasBlog diapocitiva ventajas y desventajas
Blog diapocitiva ventajas y desventajas
 
Exposicion final
Exposicion finalExposicion final
Exposicion final
 
01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo
 
Stack
StackStack
Stack
 

Similar to 12. Stack

Advanced data structures slide 2 2+
Advanced data structures slide 2 2+Advanced data structures slide 2 2+
Advanced data structures slide 2 2+jomerson remorosa
 
Inheritance : Extending Classes
Inheritance : Extending ClassesInheritance : Extending Classes
Inheritance : Extending ClassesNilesh Dalvi
 
Daksh Sharma ,BCA 2nd Year
Daksh  Sharma ,BCA 2nd YearDaksh  Sharma ,BCA 2nd Year
Daksh Sharma ,BCA 2nd Yeardezyneecole
 
Stacks in algorithems & data structure
Stacks in algorithems & data structureStacks in algorithems & data structure
Stacks in algorithems & data structurefaran nawaz
 

Similar to 12. Stack (9)

Advanced data structures slide 2 2+
Advanced data structures slide 2 2+Advanced data structures slide 2 2+
Advanced data structures slide 2 2+
 
Chapter 6 ds
Chapter 6 dsChapter 6 ds
Chapter 6 ds
 
Inheritance : Extending Classes
Inheritance : Extending ClassesInheritance : Extending Classes
Inheritance : Extending Classes
 
DS MOD2 (1) (1).pptx
DS MOD2 (1) (1).pptxDS MOD2 (1) (1).pptx
DS MOD2 (1) (1).pptx
 
Stack Algorithm
Stack AlgorithmStack Algorithm
Stack Algorithm
 
Stack & Queue
Stack & QueueStack & Queue
Stack & Queue
 
stack & queue
stack & queuestack & queue
stack & queue
 
Daksh Sharma ,BCA 2nd Year
Daksh  Sharma ,BCA 2nd YearDaksh  Sharma ,BCA 2nd Year
Daksh Sharma ,BCA 2nd Year
 
Stacks in algorithems & data structure
Stacks in algorithems & data structureStacks in algorithems & data structure
Stacks in algorithems & data structure
 

More from Nilesh Dalvi

1. Overview of Java
1. Overview of Java1. Overview of Java
1. Overview of JavaNilesh Dalvi
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator OverloadingNilesh Dalvi
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructorsNilesh Dalvi
 
Classes and objects
Classes and objectsClasses and objects
Classes and objectsNilesh Dalvi
 
Introduction to cpp
Introduction to cppIntroduction to cpp
Introduction to cppNilesh Dalvi
 
Introduction to oops concepts
Introduction to oops conceptsIntroduction to oops concepts
Introduction to oops conceptsNilesh Dalvi
 

More from Nilesh Dalvi (10)

2. Basics of Java
2. Basics of Java2. Basics of Java
2. Basics of Java
 
1. Overview of Java
1. Overview of Java1. Overview of Java
1. Overview of Java
 
Templates
TemplatesTemplates
Templates
 
File handling
File handlingFile handling
File handling
 
Strings
StringsStrings
Strings
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Introduction to cpp
Introduction to cppIntroduction to cpp
Introduction to cpp
 
Introduction to oops concepts
Introduction to oops conceptsIntroduction to oops concepts
Introduction to oops concepts
 

Recently uploaded

Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 

Recently uploaded (20)

Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 

12. Stack

  • 1. Stack By Nilesh Dalvi Lecturer, Patkar-Varde College.Lecturer, Patkar-Varde College. http://www.slideshare.net/nileshdalvi01 Java and DataJava and Data StructuresStructures
  • 2. Stack • A Stack is a list of elements in which an element may be inserted or deleted at one end which is known as TOP of the stack. • Operation Performed On Array: 1. Push: add an element in stack 2. Pop: remove an element in stack 3. Peek :Current processed element Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). a b c TOP
  • 3. Array representation of stacks: • We can represent a stack in computer in various ways, by means of one-way-list or linear array. • Consider Linear array stack: • TOP : Location of the top element of the stack • MAXSTK : max elements that can be held by stack • If TOP = 0 or TOP = NULL indicates that stack is empty. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). XX YY ZZ 1 2 3 4 5 6 7 8 TOP MAXSTK
  • 4. Adding element: PUSH() Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Algorithm PUSH (STACK, ITEM) { if (TOP = MAXSTK) then write ("Overflow"); else TOP := TOP + 1; STACK [TOP] := ITEM; }
  • 5. Deleting element: POP() Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Algorithm POP (STACK, ITEM) { if (TOP = 0) then write ("Underflow"); else ITEM = STACK [TOP]; TOP := TOP - 1; }
  • 6. Accessing element: PEEK() Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Algorithm PEEK (STACK) { if (TOP = 0) then write ("Underflow"); else Return STACK [TOP]; }
  • 7. Implementation of Stack operations Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 8. Applications of Stack: • Recursion • Infix to postfix conversion • Postfix to infix conversion Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 9. infixVect postfixVect ( a + b - c ) * d – ( e + f ) Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Infix to postfix conversion
  • 10. infixVect postfixVect a + b - c ) * d – ( e + f ) ( Infix to postfix conversion Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 11. infixVect postfixVect + b - c ) * d – ( e + f ) ( a Infix to postfix conversion Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 12. infixVect postfixVect b - c ) * d – ( e + f ) ( a + Infix to postfix conversion Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 13. infixVect postfixVect - c ) * d – ( e + f ) ( a b + Infix to postfix conversion Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 14. infixVect postfixVect c ) * d – ( e + f ) ( a b + - Infix to postfix conversion Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 15. infixVect postfixVect ) * d – ( e + f ) ( a b + c - Infix to postfix conversion Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 16. infixVect postfixVect * d – ( e + f ) a b + c - Infix to postfix conversion Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 17. infixVect postfixVect d – ( e + f ) a b + c - * Infix to postfix conversion Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 18. infixVect postfixVect – ( e + f ) a b + c - d * Infix to postfix conversion Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 19. infixVect postfixVect ( e + f ) a b + c – d * - Infix to postfix conversion Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 20. infixVect postfixVect e + f ) a b + c – d * - ( Infix to postfix conversion Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 21. infixVect postfixVect + f ) a b + c – d * e - ( Infix to postfix conversion Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 22. infixVect postfixVect f ) a b + c – d * e - ( + Infix to postfix conversion Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 23. infixVect postfixVect ) a b + c – d * e f - ( + Infix to postfix conversion Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 24. infixVect postfixVect a b + c – d * e f + - Infix to postfix conversion Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 25. infixVect postfixVect a b + c – d * e f + - Infix to postfix conversion Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 26. Algorithm for Infix to Postfix 1) Examine the next element in the input. 2) If it is operand, output it. 3) If it is opening parenthesis, push it on stack. 4) If it is an operator, then i) If stack is empty, push operator on stack. ii) If the top of stack is opening parenthesis, push operator on stack iii) If it has higher priority than the top of stack, push operator on stack. iv) Else pop the operator from the stack and output it, repeat step 4 5) If it is a closing parenthesis, pop operators from stack and output them until an opening parenthesis is encountered. pop and discard the opening parenthesis. 6) If there is more input go to step 1 7) If there is no more input, pop the remaining operators to output. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 27. Q & A