SlideShare a Scribd company logo
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

More Related Content

What's hot

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
 
Expression evaluation
Expression evaluationExpression evaluation
Expression evaluation
JeeSa Sultana
 
Bottomupparser
BottomupparserBottomupparser
Unit 3 stack
Unit   3 stackUnit   3 stack
Unit 3 stack
Dabbal Singh Mahara
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler Design
Akhil Kaushik
 
Circular queues
Circular queuesCircular queues
Circular queues
Ssankett Negi
 
Top down parsing(sid) (1)
Top down parsing(sid) (1)Top down parsing(sid) (1)
Top down parsing(sid) (1)
Siddhesh Pange
 
6 normalization
6 normalization6 normalization
6 normalization
Syamsulizar Syamsulizar
 
03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays
tameemyousaf
 
Contravariant functors in scala
Contravariant functors in scalaContravariant functors in scala
Contravariant functors in scala
Piotr Paradziński
 
Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2
Iffat Anjum
 
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
sumitbardhan
 
Lecture8 syntax analysis_4
Lecture8 syntax analysis_4Lecture8 syntax analysis_4
Lecture8 syntax analysis_4
Mahesh Kumar Chelimilla
 
Ch04
Ch04Ch04
Ch04
Hankyo
 
First and follow set
First and follow setFirst and follow set
First and follow set
Dawood Faheem Abbasi
 
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
 
Lecture7 syntax analysis_3
Lecture7 syntax analysis_3Lecture7 syntax analysis_3
Lecture7 syntax analysis_3
Mahesh Kumar Chelimilla
 
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
Dacong (Tony) Yan
 

What's hot (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
 

Viewers also liked

Infix to-postfix examples
Infix to-postfix examplesInfix to-postfix examples
Infix to-postfix examples
mua99
 
8.binry search tree
8.binry search tree8.binry search tree
8.binry search tree
Chandan Singh
 
Polish nootation
Polish nootationPolish nootation
Polish nootation
Srikanth Chennupati
 
Evaluation of postfix expression
Evaluation of postfix expressionEvaluation of postfix expression
Evaluation of postfix expression
Akhil Ahuja
 
Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)
Ahmed Khateeb
 
Infix prefix postfix expression -conversion
Infix  prefix postfix expression -conversionInfix  prefix postfix expression -conversion
Infix prefix postfix expression -conversion
Syed Mustafa
 
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
Haqnawaz Ch
 
Infix to postfix conversion
Infix to postfix conversionInfix to postfix conversion
Infix to postfix conversion
Then Murugeshwari
 

Viewers also liked (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
 

Similar to C applications

week9-prefixinfixandpostfixnotations-191013065821.pptx
week9-prefixinfixandpostfixnotations-191013065821.pptxweek9-prefixinfixandpostfixnotations-191013065821.pptx
week9-prefixinfixandpostfixnotations-191013065821.pptx
ssusere3b1a2
 
1.3- infix-ti-postfix.pdf
1.3- infix-ti-postfix.pdf1.3- infix-ti-postfix.pdf
1.3- infix-ti-postfix.pdf
soniasharmafdp
 
Stack application
Stack applicationStack application
Stack application
Student
 
Stack
StackStack
Stack_Application_Infix_Prefix.pptx
Stack_Application_Infix_Prefix.pptxStack_Application_Infix_Prefix.pptx
Stack_Application_Infix_Prefix.pptx
sandeep54552
 
Stack
StackStack
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
OliverKane3
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
OliverKane3
 
Data Structures and Agorithm: DS 08 Infix to Postfix.pptx
Data Structures and Agorithm: DS 08 Infix to Postfix.pptxData Structures and Agorithm: DS 08 Infix to Postfix.pptx
Data Structures and Agorithm: DS 08 Infix to Postfix.pptx
RashidFaridChishti
 
Stack and queue
Stack and queueStack and queue
Stack and queue
Shakila Mahjabin
 
Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)
mailmerk
 
Stack - Data Structure - Notes
Stack - Data Structure - NotesStack - Data Structure - Notes
Stack - Data Structure - Notes
Omprakash Chauhan
 
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
 
Lecture_04.2.pptx
Lecture_04.2.pptxLecture_04.2.pptx
Lecture_04.2.pptx
RockyIslam5
 
lect- 3&4.ppt
lect- 3&4.pptlect- 3&4.ppt
lect- 3&4.ppt
mrizwan38
 
Lect-5 & 6.pptx
Lect-5 & 6.pptxLect-5 & 6.pptx
Lect-5 & 6.pptx
mrizwan38
 
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
StackStack
Topic 2_revised.pptx
Topic 2_revised.pptxTopic 2_revised.pptx
Topic 2_revised.pptx
JAYAPRIYAR7
 
Stack
StackStack

Similar to C applications (20)

week9-prefixinfixandpostfixnotations-191013065821.pptx
week9-prefixinfixandpostfixnotations-191013065821.pptxweek9-prefixinfixandpostfixnotations-191013065821.pptx
week9-prefixinfixandpostfixnotations-191013065821.pptx
 
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
 
Data Structures and Agorithm: DS 08 Infix to Postfix.pptx
Data Structures and Agorithm: DS 08 Infix to Postfix.pptxData Structures and Agorithm: DS 08 Infix to Postfix.pptx
Data Structures and Agorithm: DS 08 Infix to Postfix.pptx
 
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
 

More from faizankhan260690

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
faizankhan260690
 
sort search in C
 sort search in C  sort search in C
sort search in C
faizankhan260690
 
Polarisation
PolarisationPolarisation
Polarisation
faizankhan260690
 
Friction problems
Friction problemsFriction problems
Friction problems
faizankhan260690
 
Friction
FrictionFriction
Friction [compatibility mode]
Friction [compatibility mode]Friction [compatibility mode]
Friction [compatibility mode]
faizankhan260690
 
Friction (2) [compatibility mode]
Friction (2) [compatibility mode]Friction (2) [compatibility mode]
Friction (2) [compatibility mode]
faizankhan260690
 

More from 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]
 

Recently uploaded

ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
paigestewart1632
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5
sayalidalavi006
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
TechSoup
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 

Recently uploaded (20)

ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 

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