SlideShare uma empresa Scribd logo
1 de 19
Baixar para ler offline
Some Applications of Stack




Spring Semester 2007   Programming and Data Structure   1
Arithmetic Expressions
                     Polish Notation




Spring Semester 2007   Programming and Data Structure   2
What is Polish Notation?

• Conventionally, we use the operator
  symbol between its two operands in an
  arithmetic expression.
          A+B          C–D*E               A*(B+C)
    – We can use parentheses to change the
      precedence of the operators.
    – Operator precedence is pre-defined.
• This notation is called INFIX notation.
    – Parentheses can change the precedence of
      evaluation.
    – Multiple passes required for evaluation.
Spring Semester 2007   Programming and Data Structure   3
• Polish notation
    – Named after Polish mathematician Jan
      Lukasiewicz.
    – Polish POSTFIX notation
          • Refers to the notation in which the operator symbol
            is placed after its two operands.
               AB+     CD*       AB*CD+/
    – Polish PREFIX notation
          • Refers to the notation in which the operator symbol
            is placed before its two operands.
               +AB     *CD      /*AB-CD



Spring Semester 2007   Programming and Data Structure             4
How to convert an infix expression to Polish
                   form?
• Write down the expression in fully parenthesized
  form. Then convert stepwise.
• Example:
      A+(B*C)/D-(E*F)-G

      (((A+((B*C)/D))-(E*F))-G)



• Polish Postfix form:
        A B C * D / + E F * - G -
• Polish Prefix form:
    – Try it out ….

Spring Semester 2007   Programming and Data Structure   5
• Advantages:
    – No concept of operator priority.
          • Simplifies the expression evaluation rule.
    – No need of any parenthesis.
          • Hence no ambiguity in the order of evaluation.
    – Evaluation can be carried out using a single
      scan over the expression string.
          • Using stack.




Spring Semester 2007   Programming and Data Structure        6
Evaluation of a Polish Expression

• Can be done very conveniently using a
  stack.
    – We would use the Polish postfix notation as
      illustration.
          • Requires a single pass through the expression string
            from left to right.
          • Polish prefix evaluation would be similar, but the
            string needs to be scanned from right to left.




Spring Semester 2007   Programming and Data Structure          7
while (not end of string) do
                 {
                   a = get_next_token();
                   if (a is an operand)
                      push (a);
                   if (a is an operator)
                   {
                      y = pop(); x = pop();
                      push (x ‘a’ y);
                   }
                 }
                 return (pop());



Spring Semester 2007   Programming and Data Structure   8
Parenthesis Matching




Spring Semester 2007   Programming and Data Structure   9
The Basic Problem

• Given a parenthesized expression, to test
  whether the expression is properly
  parenthesized.
    – Whenever a left parenthesis is encountered, it
      is pushed in the stack.
    – Whenever a right parenthesis is encountered,
      pop from stack and check if the parentheses
      match.
    – Works for multiple types of parentheses
            ( ), { }, [ ]


Spring Semester 2007     Programming and Data Structure   10
while (not end of string) do
            {
               a = get_next_token();
              if (a is ‘(‘ or ‘{‘ or ‘[‘)
                  push (a);
               if (a is ‘)’ or ‘}’ or ‘]’)
               {
                 if (isempty()) {
                   print (“Not well formed”);
                   exit();
                 }
                  x = pop();
                  if (a and x do not match) {
                    print (“Not well formed”);
                    exit();
                 }
               }
            }
            if (not isempty())
              print (“Not well formed”);
Spring Semester 2007   Programming and Data Structure   11
Converting an INFIX expression to
                POSTFIX




Spring Semester 2007   Programming and Data Structure   12
Basic Idea

• Let Q denote an infix expression.
    – May contain left and right parentheses.
    – Operators are:
          • Highest priority: ^ (exponentiation)
          • Then: * (multiplication), / (division)
          • Then: + (addition), – (subtraction)
    – Operators at the same level are evaluated from
      left to right.
• In the algorithm to be presented:
    – We begin by pushing a ‘(’ in the stack.
    – Also add a ‘)’ at the end of Q.

Spring Semester 2007    Programming and Data Structure   13
The Algorithm (Q:: given infix expression,
       P:: output postfix expression)
push (‘(’);
Add “)” to the end of Q;
while (not end of string in Q do)
{
  a = get_next_token();
  if (a is an operand) add it to P;
  if (a is ‘(’) push(a);
  if (a is an operator)
  {
      Repeatedly pop from stack and add to P each
      operator (on top of the stack) which has the
      same or higher precedence than “a”;
      push(a);
  }


Spring Semester 2007   Programming and Data Structure   14
if (a is ‘)’)
    {
        Repeatedly pop from stack and add to P each
        operator (on the top of the stack) until a
        left parenthesis is encountered;

          Remove the left parenthesis;
    }
}




Spring Semester 2007   Programming and Data Structure   15
Q: A + (B * C – (D / E ^ F) * G) * H )
 Q                 STACK                        Output Postfix String P
 A      (                              A
  +     ( +                            A
  (     ( + (                          A
 B      ( + (                          A B
  *     ( + ( *                        A B
 C      ( + ( *                        A B C
  -     ( + ( -                        A B C *
  (     ( + ( - (                      A B C *
 D      ( + ( - (                      A B C * D
  /     ( + ( - ( /                    A B C * D
  E     ( + ( - ( /                    A B C * D E
  ^     ( + ( - ( / ^                  A B C * D E
  F     ( + ( - ( / ^                  A B C * D E F
  )     ( + ( -                        A B C * D E F ^ /
Spring Semester 2007       Programming and Data Structure                 16
Q                 STACK                       Output Postfix String P


 *      ( + ( - *                     A B C * D E F ^ /
 G      ( + ( - *                     A B C * D E F ^ / G
 )      ( +                           A B C * D E F ^ / G * -
 *      ( + *                         A B C * D E F ^ / G * -
 H      ( + *                         A B C * D E F ^ / G * - H
 )                                    A B C * D E F ^ / G * - H * +




Spring Semester 2007       Programming and Data Structure                17
Some Other Applications




Spring Semester 2007   Programming and Data Structure   18
• Reversing a string of characters.
• Generating 3-address code from Polish
  postfix (or prefix) expressions.
• Handling function calls and returns, and
  recursion.




Spring Semester 2007   Programming and Data Structure   19

Mais conteúdo relacionado

Mais procurados

Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7
Kumar
 
My lecture infix-to-postfix
My lecture infix-to-postfixMy lecture infix-to-postfix
My lecture infix-to-postfix
Senthil Kumar
 
03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays
tameemyousaf
 
Top down and botttom up 2 LATEST.
Top down     and botttom up 2 LATEST.Top down     and botttom up 2 LATEST.
Top down and botttom up 2 LATEST.
Gerwin Ocsena
 

Mais procurados (19)

Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7
 
My lecture infix-to-postfix
My lecture infix-to-postfixMy lecture infix-to-postfix
My lecture infix-to-postfix
 
Expression evaluation
Expression evaluationExpression evaluation
Expression evaluation
 
Bottomupparser
BottomupparserBottomupparser
Bottomupparser
 
Unit 3 stack
Unit   3 stackUnit   3 stack
Unit 3 stack
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler Design
 
Circular queues
Circular queuesCircular queues
Circular queues
 
Top down parsing(sid) (1)
Top down parsing(sid) (1)Top down parsing(sid) (1)
Top down parsing(sid) (1)
 
6 normalization
6 normalization6 normalization
6 normalization
 
03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays
 
Contravariant functors in scala
Contravariant functors in scalaContravariant functors in scala
Contravariant functors in scala
 
Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2
 
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
 
Lecture8 syntax analysis_4
Lecture8 syntax analysis_4Lecture8 syntax analysis_4
Lecture8 syntax analysis_4
 
Ch04
Ch04Ch04
Ch04
 
First and follow set
First and follow setFirst and follow set
First and follow set
 
Top down and botttom up 2 LATEST.
Top down     and botttom up 2 LATEST.Top down     and botttom up 2 LATEST.
Top down and botttom up 2 LATEST.
 
Lecture7 syntax analysis_3
Lecture7 syntax analysis_3Lecture7 syntax analysis_3
Lecture7 syntax analysis_3
 
Demand-Driven Context-Sensitive Alias Analysis for Java
Demand-Driven Context-Sensitive Alias Analysis for JavaDemand-Driven Context-Sensitive Alias Analysis for Java
Demand-Driven Context-Sensitive Alias Analysis for Java
 

Destaque (8)

Infix to-postfix examples
Infix to-postfix examplesInfix to-postfix examples
Infix to-postfix examples
 
8.binry search tree
8.binry search tree8.binry search tree
8.binry search tree
 
Polish nootation
Polish nootationPolish nootation
Polish nootation
 
Evaluation of postfix expression
Evaluation of postfix expressionEvaluation of postfix expression
Evaluation of postfix expression
 
Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)
 
Infix prefix postfix expression -conversion
Infix  prefix postfix expression -conversionInfix  prefix postfix expression -conversion
Infix prefix postfix expression -conversion
 
Conversion from infix to prefix using stack
Conversion from infix to prefix using stackConversion from infix to prefix using stack
Conversion from infix to prefix using stack
 
Infix to postfix conversion
Infix to postfix conversionInfix to postfix conversion
Infix to postfix conversion
 

Semelhante a C applications

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
 
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
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
chandankumar364348
 

Semelhante a C applications (20)

1.3- infix-ti-postfix.pdf
1.3- infix-ti-postfix.pdf1.3- infix-ti-postfix.pdf
1.3- infix-ti-postfix.pdf
 
Stack application
Stack applicationStack application
Stack application
 
Stack
StackStack
Stack
 
Stack_Application_Infix_Prefix.pptx
Stack_Application_Infix_Prefix.pptxStack_Application_Infix_Prefix.pptx
Stack_Application_Infix_Prefix.pptx
 
Stack
StackStack
Stack
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)
 
Stack - Data Structure - Notes
Stack - Data Structure - NotesStack - Data Structure - Notes
Stack - Data Structure - Notes
 
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
 
Lecture_04.2.pptx
Lecture_04.2.pptxLecture_04.2.pptx
Lecture_04.2.pptx
 
lect- 3&4.ppt
lect- 3&4.pptlect- 3&4.ppt
lect- 3&4.ppt
 
Lect-5 & 6.pptx
Lect-5 & 6.pptxLect-5 & 6.pptx
Lect-5 & 6.pptx
 
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
StackStack
Stack
 
Topic 2_revised.pptx
Topic 2_revised.pptxTopic 2_revised.pptx
Topic 2_revised.pptx
 
Stack
StackStack
Stack
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
 

Mais de faizankhan260690 (7)

Science and Tech question for UPSC prelims
Science and Tech question for UPSC prelimsScience and Tech question for UPSC prelims
Science and Tech question for UPSC prelims
 
sort search in C
 sort search in C  sort search in C
sort search in C
 
Polarisation
PolarisationPolarisation
Polarisation
 
Friction problems
Friction problemsFriction problems
Friction problems
 
Friction
FrictionFriction
Friction
 
Friction [compatibility mode]
Friction [compatibility mode]Friction [compatibility mode]
Friction [compatibility mode]
 
Friction (2) [compatibility mode]
Friction (2) [compatibility mode]Friction (2) [compatibility mode]
Friction (2) [compatibility mode]
 

Último

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
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
heathfieldcps1
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 

Último (20)

Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.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
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
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 ...
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
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...
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
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
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
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
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 

C applications

  • 1. Some Applications of Stack Spring Semester 2007 Programming and Data Structure 1
  • 2. Arithmetic Expressions Polish Notation Spring Semester 2007 Programming and Data Structure 2
  • 3. What is Polish Notation? • Conventionally, we use the operator symbol between its two operands in an arithmetic expression. A+B C–D*E A*(B+C) – We can use parentheses to change the precedence of the operators. – Operator precedence is pre-defined. • This notation is called INFIX notation. – Parentheses can change the precedence of evaluation. – Multiple passes required for evaluation. Spring Semester 2007 Programming and Data Structure 3
  • 4. • Polish notation – Named after Polish mathematician Jan Lukasiewicz. – Polish POSTFIX notation • Refers to the notation in which the operator symbol is placed after its two operands. AB+ CD* AB*CD+/ – Polish PREFIX notation • Refers to the notation in which the operator symbol is placed before its two operands. +AB *CD /*AB-CD Spring Semester 2007 Programming and Data Structure 4
  • 5. How to convert an infix expression to Polish form? • Write down the expression in fully parenthesized form. Then convert stepwise. • Example: A+(B*C)/D-(E*F)-G (((A+((B*C)/D))-(E*F))-G) • Polish Postfix form: A B C * D / + E F * - G - • Polish Prefix form: – Try it out …. Spring Semester 2007 Programming and Data Structure 5
  • 6. • Advantages: – No concept of operator priority. • Simplifies the expression evaluation rule. – No need of any parenthesis. • Hence no ambiguity in the order of evaluation. – Evaluation can be carried out using a single scan over the expression string. • Using stack. Spring Semester 2007 Programming and Data Structure 6
  • 7. Evaluation of a Polish Expression • Can be done very conveniently using a stack. – We would use the Polish postfix notation as illustration. • Requires a single pass through the expression string from left to right. • Polish prefix evaluation would be similar, but the string needs to be scanned from right to left. Spring Semester 2007 Programming and Data Structure 7
  • 8. while (not end of string) do { a = get_next_token(); if (a is an operand) push (a); if (a is an operator) { y = pop(); x = pop(); push (x ‘a’ y); } } return (pop()); Spring Semester 2007 Programming and Data Structure 8
  • 9. Parenthesis Matching Spring Semester 2007 Programming and Data Structure 9
  • 10. The Basic Problem • Given a parenthesized expression, to test whether the expression is properly parenthesized. – Whenever a left parenthesis is encountered, it is pushed in the stack. – Whenever a right parenthesis is encountered, pop from stack and check if the parentheses match. – Works for multiple types of parentheses ( ), { }, [ ] Spring Semester 2007 Programming and Data Structure 10
  • 11. while (not end of string) do { a = get_next_token(); if (a is ‘(‘ or ‘{‘ or ‘[‘) push (a); if (a is ‘)’ or ‘}’ or ‘]’) { if (isempty()) { print (“Not well formed”); exit(); } x = pop(); if (a and x do not match) { print (“Not well formed”); exit(); } } } if (not isempty()) print (“Not well formed”); Spring Semester 2007 Programming and Data Structure 11
  • 12. Converting an INFIX expression to POSTFIX Spring Semester 2007 Programming and Data Structure 12
  • 13. Basic Idea • Let Q denote an infix expression. – May contain left and right parentheses. – Operators are: • Highest priority: ^ (exponentiation) • Then: * (multiplication), / (division) • Then: + (addition), – (subtraction) – Operators at the same level are evaluated from left to right. • In the algorithm to be presented: – We begin by pushing a ‘(’ in the stack. – Also add a ‘)’ at the end of Q. Spring Semester 2007 Programming and Data Structure 13
  • 14. The Algorithm (Q:: given infix expression, P:: output postfix expression) push (‘(’); Add “)” to the end of Q; while (not end of string in Q do) { a = get_next_token(); if (a is an operand) add it to P; if (a is ‘(’) push(a); if (a is an operator) { Repeatedly pop from stack and add to P each operator (on top of the stack) which has the same or higher precedence than “a”; push(a); } Spring Semester 2007 Programming and Data Structure 14
  • 15. if (a is ‘)’) { Repeatedly pop from stack and add to P each operator (on the top of the stack) until a left parenthesis is encountered; Remove the left parenthesis; } } Spring Semester 2007 Programming and Data Structure 15
  • 16. Q: A + (B * C – (D / E ^ F) * G) * H ) Q STACK Output Postfix String P A ( A + ( + A ( ( + ( A B ( + ( A B * ( + ( * A B C ( + ( * A B C - ( + ( - A B C * ( ( + ( - ( A B C * D ( + ( - ( A B C * D / ( + ( - ( / A B C * D E ( + ( - ( / A B C * D E ^ ( + ( - ( / ^ A B C * D E F ( + ( - ( / ^ A B C * D E F ) ( + ( - A B C * D E F ^ / Spring Semester 2007 Programming and Data Structure 16
  • 17. Q STACK Output Postfix String P * ( + ( - * A B C * D E F ^ / G ( + ( - * A B C * D E F ^ / G ) ( + A B C * D E F ^ / G * - * ( + * A B C * D E F ^ / G * - H ( + * A B C * D E F ^ / G * - H ) A B C * D E F ^ / G * - H * + Spring Semester 2007 Programming and Data Structure 17
  • 18. Some Other Applications Spring Semester 2007 Programming and Data Structure 18
  • 19. • Reversing a string of characters. • Generating 3-address code from Polish postfix (or prefix) expressions. • Handling function calls and returns, and recursion. Spring Semester 2007 Programming and Data Structure 19