SlideShare a Scribd company logo
1 of 18
STACK
Seminar By Naresh
SKU_CST_M.Sc.
DATA STRUCTURES
A Pseudo code Approach with C++
Stack
 A stack is linear list in which all additions and deletions are restricted to one end,
called top. If you inserted a data series into stack and then removed it, the order
of the data would be reversed, it also called as Last- in- First out (LIFO) data
structure.
SKU_CST_M.Sc.
SKU_CST_M.Sc.
Example
SKU_CST_M.Sc.
Stack Applications can be classified into four categories
They are:
Reversing Data
Parsing Data
Postponing data
backtracking
SKU_CST_M.Sc.
 Reversing data requires that a given set of data be reordered so that
first and last elements are exchanged. For example {1 ,2,3,4}
becomes {4,3,2,1}
 We examine two different reversing applications:
 Reverse a list
 Convert decimal to binary
SKU_CST_M.Sc.
Postponement: Evaluating arithmetic expressions.
Expression: expression is a legal combination of operands and the operators.
 Operand: is the quantity (unit of data) on which a mathematical operation is performed.
 Operand may be a variable like x, y, z or a constant like 5, 4,0,9,1 etc.
 The difference between variables and constants is that variables can change their value at any time
but constants can never change their value. (The constants value is locked for the duration of the
program)
 Operator: is a symbol which is a mathematical or logical operation between the operands.
Example of familiar operators include +,-,*, /, ^
 Considering these definitions of operands and operators now we can write an example of
expression as x+y*z.
SKU_CST_M.Sc.
We develop two stack postponement applications in this section:
Infix to Postfix transformation and postfix expression evaluation.
An arithmetic expression can be represented in three different formats they are
Infix, Prefix, Postfix
Which we write an operator in between two operands is called Infix Notation
<Operands><Operator><Operands>
2 + 3
Example
2+3=5
4+6*2=?
SKU_CST_M.Sc.
Order of the Operations
Parentheses { }, ( ), [ ]
Exponents ^, 2^3^2 = 512(right to left)
Multiplication and Division (left to right)
Addition and Subtraction (left to right)
Example:
2 * 6 / 2 – 3 + 7 = ?
12 / 2 – 3 + 7
6-3+7= 3+7=10
SKU_CST_M.Sc.
{(2 * 6 ) / 2 } – (3 + 7) = ?
{12 / 2} – ( 3+7)= 6-10= -4
Prefix notation was proposed earlier in 1924 by polish legition. In Prefix Notation the operator is placed
before two operands
<Operator><Operands> <Operand>
+ 2 3
a + (b * c)=
a + ( * bc)
+a * bc
SKU_CST_M.Sc.
 Postfix notation is another way of writing arithmetic expressions.
 In postfix notation, the operator is written after the two operands.
 infix: 2+5 postfix: 2 5 +
 Expressions are evaluated from left to right.
Postfix notation are also Known as Reverse Polish Notation.
They are different from the infix and prefix notations in the sense that in the postfix notation,
operator comes after the operands, e.g. xy+, xyz+* etc.
<Operands> <Operand> <Operator>
2 3 +
Examples of infix to prefix and post fix
Infix PostFix Prefix
A+B AB+ +AB
(A+B) * (C + D) AB+CD+* *+AB+CD
A-B/(C*D^E) ABCDE^*/- -A/B*C^DE
Postfix Examples
Infix Postfix Evaluation
2 - 3 * 4 + 5 2 3 4 * - 5 + -5
(2 - 3) * (4 + 5) 2 3 - 4 5 + * -9
2- (3 * 4 +5) 2 3 4 * 5 + - -15
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.
Suppose we want to convert 2*3/(2-1)+5*3 into Postfix form,
So, the Postfix Expression is 23*21-/53*+
2 Empty 2
* * 2
3 * 23
/ / 23*
( /( 23*
2 /( 23*2
- /(- 23*2
1 /(- 23*21
) / 23*21-
+ + 23*21-/
5 + 23*21-/5
3 +* 23*21-/53
Expression Stack Output
* +* 23*21-/53
Empty 23*21-/53*+
SKU_CST_M.Sc.
Example: postfix expressions
SKU_CST_M.Sc.
Another application of stack is Parsing. Parsing is any logic that breaks data into independent
pieces for further processing.
For example
To translate a source program to machine language, a compiler must parse the program into
individual parts such as key words, names and tokens.
One common programming problem is unmatched parentheses in an algebraic
expression. When parentheses are unmatched, two types of errors can occur: The
opening parentheses can be missing or the closing parentheses can be messing.
( (A + B) / C (A + B) / C )
(a) Operating Parentheses not matched (b) Operating Parentheses not matched
SKU_CST_M.Sc.
Backtracking is another stack use found in applications such as computer gaming, decision
analysis, and expert system.
We examine backtracking applications in this section: Goal Seeking and Eight Queens
Problem
Backtracking is a methodical way of trying out various sequences of decisions, until you
find one that “works”
SKU_CST_M.Sc.

More Related Content

What's hot

Chapter Eight(1)
Chapter Eight(1)Chapter Eight(1)
Chapter Eight(1)
bolovv
 
Stacks & Queues
Stacks & QueuesStacks & Queues
Stacks & Queues
tech4us
 

What's hot (20)

Three address code generation
Three address code generationThree address code generation
Three address code generation
 
Lecture 12 intermediate code generation
Lecture 12 intermediate code generationLecture 12 intermediate code generation
Lecture 12 intermediate code generation
 
Generating code from dags
Generating code from dagsGenerating code from dags
Generating code from dags
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
 
Three Address code
Three Address code Three Address code
Three Address code
 
C standard library functions
C standard library functionsC standard library functions
C standard library functions
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler Design
 
Directed Acyclic Graph Representation of basic blocks
Directed Acyclic Graph Representation of basic blocksDirected Acyclic Graph Representation of basic blocks
Directed Acyclic Graph Representation of basic blocks
 
Polish
PolishPolish
Polish
 
Chapter Eight(1)
Chapter Eight(1)Chapter Eight(1)
Chapter Eight(1)
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
 
Ch8a
Ch8aCh8a
Ch8a
 
Pointers
PointersPointers
Pointers
 
Stacks and Queue - Data Structures
Stacks and Queue - Data StructuresStacks and Queue - Data Structures
Stacks and Queue - Data Structures
 
The dag representation of basic blocks
The dag representation of basic blocksThe dag representation of basic blocks
The dag representation of basic blocks
 
C programming part4
C programming part4C programming part4
C programming part4
 
Stacks & Queues
Stacks & QueuesStacks & Queues
Stacks & Queues
 
R Programming: Introduction to Vectors
R Programming: Introduction to VectorsR Programming: Introduction to Vectors
R Programming: Introduction to Vectors
 
Principle source of optimazation
Principle source of optimazationPrinciple source of optimazation
Principle source of optimazation
 
Compiler unit 5
Compiler  unit 5Compiler  unit 5
Compiler unit 5
 

Similar to STACK Applications in DS

Although people may be very accustomed to reading and understanding .docx
Although people may be very accustomed to reading and understanding .docxAlthough people may be very accustomed to reading and understanding .docx
Although people may be very accustomed to reading and understanding .docx
milissaccm
 

Similar to STACK Applications in DS (20)

Topic 2_revised.pptx
Topic 2_revised.pptxTopic 2_revised.pptx
Topic 2_revised.pptx
 
ForLoopandUserDefinedFunctions.pptx
ForLoopandUserDefinedFunctions.pptxForLoopandUserDefinedFunctions.pptx
ForLoopandUserDefinedFunctions.pptx
 
Mycasestudy
MycasestudyMycasestudy
Mycasestudy
 
Although people may be very accustomed to reading and understanding .docx
Although people may be very accustomed to reading and understanding .docxAlthough people may be very accustomed to reading and understanding .docx
Although people may be very accustomed to reading and understanding .docx
 
Ch-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdfCh-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdf
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
 
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
 
Lecture_04.2.pptx
Lecture_04.2.pptxLecture_04.2.pptx
Lecture_04.2.pptx
 
Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)
 
Applications of Stack
Applications of StackApplications of Stack
Applications of Stack
 
01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo
 
additional.pptx
additional.pptxadditional.pptx
additional.pptx
 
Operators
OperatorsOperators
Operators
 
14 operator overloading
14 operator overloading14 operator overloading
14 operator overloading
 
C++ Interview Question And Answer
C++ Interview Question And AnswerC++ Interview Question And Answer
C++ Interview Question And Answer
 
C++ questions And Answer
C++ questions And AnswerC++ questions And Answer
C++ questions And Answer
 
Conversion of in fix pre fix,infix by sarmad baloch
Conversion of in fix pre fix,infix by sarmad balochConversion of in fix pre fix,infix by sarmad baloch
Conversion of in fix pre fix,infix by sarmad baloch
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 

Recently uploaded

MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MysoreMuleSoftMeetup
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
httgc7rh9c
 

Recently uploaded (20)

MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
PANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxPANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptx
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 

STACK Applications in DS

  • 1. STACK Seminar By Naresh SKU_CST_M.Sc. DATA STRUCTURES A Pseudo code Approach with C++
  • 2. Stack  A stack is linear list in which all additions and deletions are restricted to one end, called top. If you inserted a data series into stack and then removed it, the order of the data would be reversed, it also called as Last- in- First out (LIFO) data structure. SKU_CST_M.Sc.
  • 4. SKU_CST_M.Sc. Stack Applications can be classified into four categories They are: Reversing Data Parsing Data Postponing data backtracking
  • 5. SKU_CST_M.Sc.  Reversing data requires that a given set of data be reordered so that first and last elements are exchanged. For example {1 ,2,3,4} becomes {4,3,2,1}  We examine two different reversing applications:  Reverse a list  Convert decimal to binary
  • 6. SKU_CST_M.Sc. Postponement: Evaluating arithmetic expressions. Expression: expression is a legal combination of operands and the operators.  Operand: is the quantity (unit of data) on which a mathematical operation is performed.  Operand may be a variable like x, y, z or a constant like 5, 4,0,9,1 etc.  The difference between variables and constants is that variables can change their value at any time but constants can never change their value. (The constants value is locked for the duration of the program)  Operator: is a symbol which is a mathematical or logical operation between the operands. Example of familiar operators include +,-,*, /, ^  Considering these definitions of operands and operators now we can write an example of expression as x+y*z.
  • 7. SKU_CST_M.Sc. We develop two stack postponement applications in this section: Infix to Postfix transformation and postfix expression evaluation. An arithmetic expression can be represented in three different formats they are Infix, Prefix, Postfix Which we write an operator in between two operands is called Infix Notation <Operands><Operator><Operands> 2 + 3 Example 2+3=5 4+6*2=?
  • 8. SKU_CST_M.Sc. Order of the Operations Parentheses { }, ( ), [ ] Exponents ^, 2^3^2 = 512(right to left) Multiplication and Division (left to right) Addition and Subtraction (left to right) Example: 2 * 6 / 2 – 3 + 7 = ? 12 / 2 – 3 + 7 6-3+7= 3+7=10
  • 9. SKU_CST_M.Sc. {(2 * 6 ) / 2 } – (3 + 7) = ? {12 / 2} – ( 3+7)= 6-10= -4 Prefix notation was proposed earlier in 1924 by polish legition. In Prefix Notation the operator is placed before two operands <Operator><Operands> <Operand> + 2 3 a + (b * c)= a + ( * bc) +a * bc
  • 10. SKU_CST_M.Sc.  Postfix notation is another way of writing arithmetic expressions.  In postfix notation, the operator is written after the two operands.  infix: 2+5 postfix: 2 5 +  Expressions are evaluated from left to right. Postfix notation are also Known as Reverse Polish Notation. They are different from the infix and prefix notations in the sense that in the postfix notation, operator comes after the operands, e.g. xy+, xyz+* etc. <Operands> <Operand> <Operator> 2 3 +
  • 11. Examples of infix to prefix and post fix Infix PostFix Prefix A+B AB+ +AB (A+B) * (C + D) AB+CD+* *+AB+CD A-B/(C*D^E) ABCDE^*/- -A/B*C^DE
  • 12. Postfix Examples Infix Postfix Evaluation 2 - 3 * 4 + 5 2 3 4 * - 5 + -5 (2 - 3) * (4 + 5) 2 3 - 4 5 + * -9 2- (3 * 4 +5) 2 3 4 * 5 + - -15
  • 13. 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.
  • 14. Suppose we want to convert 2*3/(2-1)+5*3 into Postfix form, So, the Postfix Expression is 23*21-/53*+ 2 Empty 2 * * 2 3 * 23 / / 23* ( /( 23* 2 /( 23*2 - /(- 23*2 1 /(- 23*21 ) / 23*21- + + 23*21-/ 5 + 23*21-/5 3 +* 23*21-/53 Expression Stack Output * +* 23*21-/53 Empty 23*21-/53*+
  • 16. SKU_CST_M.Sc. Another application of stack is Parsing. Parsing is any logic that breaks data into independent pieces for further processing. For example To translate a source program to machine language, a compiler must parse the program into individual parts such as key words, names and tokens. One common programming problem is unmatched parentheses in an algebraic expression. When parentheses are unmatched, two types of errors can occur: The opening parentheses can be missing or the closing parentheses can be messing. ( (A + B) / C (A + B) / C ) (a) Operating Parentheses not matched (b) Operating Parentheses not matched
  • 17. SKU_CST_M.Sc. Backtracking is another stack use found in applications such as computer gaming, decision analysis, and expert system. We examine backtracking applications in this section: Goal Seeking and Eight Queens Problem Backtracking is a methodical way of trying out various sequences of decisions, until you find one that “works”