SlideShare a Scribd company logo
Linear Data Structures

 Stack
Topics Covered in This Lecture
• Applications of stack
  – Reversing the string
  – Balancing symbols
  – Postfix expression evaluation
  – Translating infix expression to postfix expression
Applications of Stack
• Reversing the string

  – push each character on to a stack as it is read.

  – When the line is finished, we then pop characters off the
    stack, and they will come off in the reverse order.
Applications of Stack
Reversing the string
void ReverseRead(void)
{ //using static Stack class of Lecture#3
  Stack<char> stack;//The Stack ‘stack’ is created
  char item;
  cin>>item;
  while (!stack.isFull()&&item!='$')//type in $ from keyboard to stop input
  {      stack.push(item); // push each character onto the stack
         cin>>item;
  }
  while(!stack.isEmpty() )
  {      item=stack.pop ();//Pop an element from stack
          cout<<item;
  }
}
Applications of Stack
•    Balancing Symbols
     Compilers use a program that checks whether every symbol (like braces,
     parenthesis, brackets, etc) in a program is balanced.
         The simple algorithm for this purpose is:
1.   Make an empty stack.
2.   Read the characters until end of file.
3.   If the character is an open any thing, push it onto the stack.
4.   If it is a close any thing, then
        if the stack is empty report an error.
        Otherwise Pop the Stack.
        If the popped symbol is not the corresponding opening symbol, then
        report an error.
5.   At the end of the file, if the stack is not empty report an error.
Polish Notation

a–b/c+d*e

Precedence?

1.   b/c
2.   d*e
3.   a – a1      /a1 = b/c /
4.   t2 + a2    / t2 = a – b/c /   /a2 = d*e/
Infix, Suffix, Prefix
Infix = a * b + c
((a*b) +c)
Priority:
1. a * b
2. a1 + c / a1 = a * b /
Prefix =
* a b , +a1 c
+*abc
Suffix =
ab* , a1c+
ab*c+
Infix, Suffix, Prefix
infix = a- b * c / d + e / f
suffix =a – bc* / d + e / f
       a – bc*d/ + e / f
       a – bc*d/ + ef/
       abc*d/- + ef/
       abc*d/-ef/+
prefix =a - *bc / d + e / f
       a - /*bcd + e / f
       a - /*bcd + /ef
       -a/*bcd + /ef
       +-a/*bcd/ef
Infix, Suffix, Prefix
Infix:
  a+b*c–d/f+e
Suffix:
  abc*+df/-e+
Prefix:
  +-+a*bc/dfe
Applications of Stack
         Postfix Expression Evaluation

     –     When a number is seen, it is pushed onto the stack

     –     When an operator is seen, then pop two elements from
           stack and push the result onto the stack.
         Now we evaluate the following postfix expression.
         6523+8*+3+*                                       3

1.   The first four are placed on the stack. The resulting stack is    2
                                                                       5
                                                                       6
                                                                      stack
Applications of Stack
•        evaluating the following postfix
         expression.
                                   3
         6523+8*+3+*               2
                                                    5
                                                    6
                                                  stack
    2.   Next a + is read, so 3 and 2 are popped from the stack and their sum 5 is
         pushed.                                                                   5
                                                                                  5
                                                                                  6
                                                                                stack
Applications of Stack
•    evaluating the following postfix
     expression.
     6523+8*+3+*                5
                                   5
                                   6
                                  stack
                                           8
3.   Next 8 is read and pushed.
                                           5
                                           5
                                           6
                                          stack
Applications of Stack
•    evaluating the following postfix
     expression.
                              8
     6523+8*+3+*              5
                                               5
                                               6
                                             stack
4.   Next a * is seen so 8 and 5 are popped as 8 * 5 = 40 is pushed
                                                                       40
                                                                        5
                                                                        6
                                                                      stack
Applications of Stack
•    evaluating the following postfix
     expression.
     6523+8*+3+*              40
                                              5
                                              6
                                            stack
5.   Next a + is read so 40 and 5 are popped and 40 + 5 = 45 is pushed.


                                                                          45
                                                                           6
                                                                          stack
Applications of Stack
•    evaluating the following postfix
     expression.
     6523+8*+3+*
                            45
                             6
                            stack
6.   Now 3 is pushed
                                        3
                                        45
                                        6
                                    stack
Applications of Stack
•    evaluating the following postfix
     expression.
     6523+8*+3+*                  3
                                                    45
                                                    6
                                                  stack
7.   Next symbol is + so pops 3 and 45 and pushes 45 + 3 = 48, so push 48 in stack.


                                                                         48
                                                                         6
                                                                       stack
Applications of Stack
•    evaluating the following postfix
     expression.
     6523+8*+3+*              48
                                                 6
                                               stack
7.   Finally a * is seen and 48 and 6 are popped, the result 6 * 48 = 288 is pushed.

8.   As there is no input, so pop the stack and we get the result.


                                                                                 288
                                                                               stack
Applications of Stack
•       Translating infix expressions to postfix expression
    –      When an operand is read, it is immediately placed onto the output.
    –      When an operator or left parenthesis comes then save it in the stack
           initially stack is empty.
    –      If we see a right parenthesis, then we pop the stack, writing symbols
           until we encounter a (corresponding) left parenthesis, which is
           popped but not output.
    –      If we see any other symbol (‘+’, ‘*’, ‘(‘, etc) then we pop entries form
           the stack until we find an entry of lower priority. One exception is
           that we never remove a ‘(‘ from the stack except when processing a
           ‘)’.

    –      When the popping is done, we push the operand onto the stack.

    –      Finally, if we read the end of input, we pop the stack until it is empty,
           writing symbols onto the output.
Applications of Stack
•     Translating infix expressions to postfix expression
Convert the following infix expression to postfix expression.
a+b*c+(d*e+f)*g

1.   First the symbol a is read, so it is passed through to the output a

2.   Then + is read and pushed onto the stack.                             output
                                                             +
                                                           stack
3.   Next b is read and passed through to the output.      ab
                                                             output             *
4.   Next a * is read. The top entry on the operator stack has lower            +
     precedence than *, so nothing is output and * is put on the .
                                                                              stack
Applications of Stack
Converting the following infix expression to postfix
    expression.
a+b*c+(d*e+f)*g
5. Next, c is read and output.    abc
                                    output
6.   The next symbol is a +. Checking the stack, we find that priority of stack top
     symbol * is higher than + . So we pop a * and place it on the output, Pop the
     other +, which is not of lower but equal priority, and then push +.

              *           abc*+
                                                        +
              +             output
           stack                                      stack
Applications of Stack
Converting the following infix expression to postfix
    expression.
a+b*c+(d*e+f)*g
 7.   The next symbol read is an ‘(‘, which, being of highest precedence, is placed on
      the stack.
                            (
                           +
                         stack
 8.   Then d is read and output.          abc*+d
                                            output
Applications of Stack
Converting the following infix expression to postfix
    expression.
a+b*c+(d*e+f)*g
9.   We continue by reading a *. Since open parenthesis do not get removed except
     when a closed parenthesis is being processed, there is no output and we push *
     in stack
                                      *
                                     (
                                     +
                                   stack
10. Next, e is read and output.
                                     abc*+de
                                       output
Applications of Stack
Converting the following infix expression to postfix
    expression.
a+b*c+(d*e+f)*g
11. The next symbol read is a +, since priority of stack top value is higher so we pop
    * and push +.

                        +               abc*+de*
                        (                 output
                        +
                      stack
12. Now we read f and output f.
                                      abc*+de*f
                                        output
Applications of Stack
Converting the following infix expression to postfix
    expression.
a+b*c+(d*e+f)*g
 13. Now we read a ‘)’, so the stack is emptied back to the ‘(‘, we output a +.

                                         abc*+de*f+
                   +
                                                 output
                 stack
 14. We read a * next; it is pushed onto the stack.
                                                           *
                                                           +
                                                        stack
 15. Now, g is read and output.      abc*+de*f+g
                                            output
Applications of Stack
Converting the following infix expression to postfix
    expression.
                           *
a+b*c+(d*e+f)*g
                                  +
                               stack
16. The input is now empty, so pop output symbols from the stack until it is empty.

                                       abc*+de*f+g*+
                                              output
                stack
Assignment 1
          Group Size: 2, Due Date : Feb 27, 2012

•     Implement Applications of stack
     1.   Balancing of symbols
     2.   Decimal to Binary Conversion
     3.   Infix to Postfix conversion
     4.   Postfix expression evaluation
     5.   Infix to Prefix conversion
     6.   Prefix expression evaluation
1.    Each application will be implemented as a separate
      function. Function will use stack, which is already
      implemented in a header file and is available for reuse.
2.    Difference between CGPAs of both group members
      should not be more than ONE
Data Structure Lecture 3

More Related Content

What's hot

Thrashing allocation frames.43
Thrashing allocation frames.43Thrashing allocation frames.43
Thrashing allocation frames.43myrajendra
 
Dbms Notes Lecture 9 : Specialization, Generalization and Aggregation
Dbms Notes Lecture 9 : Specialization, Generalization and AggregationDbms Notes Lecture 9 : Specialization, Generalization and Aggregation
Dbms Notes Lecture 9 : Specialization, Generalization and Aggregation
BIT Durg
 
01 Knapsack using Dynamic Programming
01 Knapsack using Dynamic Programming01 Knapsack using Dynamic Programming
01 Knapsack using Dynamic Programming
Fenil Shah
 
Operating system 34 contiguous allocation
Operating system 34 contiguous allocationOperating system 34 contiguous allocation
Operating system 34 contiguous allocation
Vaibhav Khanna
 
Macro-processor
Macro-processorMacro-processor
Macro-processor
Temesgen Molla
 
Market Basket Analysis of bakery Shop
Market Basket Analysis of bakery ShopMarket Basket Analysis of bakery Shop
Market Basket Analysis of bakery Shop
VarunSahdev2
 
Fundamentals of Neural Networks
Fundamentals of Neural NetworksFundamentals of Neural Networks
Fundamentals of Neural Networks
Gagan Deep
 
Shift reduce parser
Shift reduce parserShift reduce parser
Shift reduce parser
TEJVEER SINGH
 
Memory Management & Garbage Collection
Memory Management & Garbage CollectionMemory Management & Garbage Collection
Memory Management & Garbage Collection
Abhishek Sur
 
10. Search Tree - Data Structures using C++ by Varsha Patil
10. Search Tree - Data Structures using C++ by Varsha Patil10. Search Tree - Data Structures using C++ by Varsha Patil
10. Search Tree - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
directory structure and file system mounting
directory structure and file system mountingdirectory structure and file system mounting
directory structure and file system mounting
rajshreemuthiah
 
Operator precedence
Operator precedenceOperator precedence
Operator precedence
Akshaya Arunan
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic Notations
Rishabh Soni
 
Memory Organization
Memory OrganizationMemory Organization
Memory Organization
Kamal Acharya
 
Threaded Binary Tree
Threaded Binary TreeThreaded Binary Tree
Threaded Binary Tree
khabbab_h
 
Inter Process Communication
Inter Process CommunicationInter Process Communication
Inter Process Communication
Adeel Rasheed
 
File and directory
File and directoryFile and directory
File and directory
Sunil Kafle
 
Intermediate code generator
Intermediate code generatorIntermediate code generator
Intermediate code generator
sanchi29
 
Operating system 25 classical problems of synchronization
Operating system 25 classical problems of synchronizationOperating system 25 classical problems of synchronization
Operating system 25 classical problems of synchronization
Vaibhav Khanna
 
8086 instructions
8086 instructions8086 instructions
8086 instructions
Ravi Anand
 

What's hot (20)

Thrashing allocation frames.43
Thrashing allocation frames.43Thrashing allocation frames.43
Thrashing allocation frames.43
 
Dbms Notes Lecture 9 : Specialization, Generalization and Aggregation
Dbms Notes Lecture 9 : Specialization, Generalization and AggregationDbms Notes Lecture 9 : Specialization, Generalization and Aggregation
Dbms Notes Lecture 9 : Specialization, Generalization and Aggregation
 
01 Knapsack using Dynamic Programming
01 Knapsack using Dynamic Programming01 Knapsack using Dynamic Programming
01 Knapsack using Dynamic Programming
 
Operating system 34 contiguous allocation
Operating system 34 contiguous allocationOperating system 34 contiguous allocation
Operating system 34 contiguous allocation
 
Macro-processor
Macro-processorMacro-processor
Macro-processor
 
Market Basket Analysis of bakery Shop
Market Basket Analysis of bakery ShopMarket Basket Analysis of bakery Shop
Market Basket Analysis of bakery Shop
 
Fundamentals of Neural Networks
Fundamentals of Neural NetworksFundamentals of Neural Networks
Fundamentals of Neural Networks
 
Shift reduce parser
Shift reduce parserShift reduce parser
Shift reduce parser
 
Memory Management & Garbage Collection
Memory Management & Garbage CollectionMemory Management & Garbage Collection
Memory Management & Garbage Collection
 
10. Search Tree - Data Structures using C++ by Varsha Patil
10. Search Tree - Data Structures using C++ by Varsha Patil10. Search Tree - Data Structures using C++ by Varsha Patil
10. Search Tree - Data Structures using C++ by Varsha Patil
 
directory structure and file system mounting
directory structure and file system mountingdirectory structure and file system mounting
directory structure and file system mounting
 
Operator precedence
Operator precedenceOperator precedence
Operator precedence
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic Notations
 
Memory Organization
Memory OrganizationMemory Organization
Memory Organization
 
Threaded Binary Tree
Threaded Binary TreeThreaded Binary Tree
Threaded Binary Tree
 
Inter Process Communication
Inter Process CommunicationInter Process Communication
Inter Process Communication
 
File and directory
File and directoryFile and directory
File and directory
 
Intermediate code generator
Intermediate code generatorIntermediate code generator
Intermediate code generator
 
Operating system 25 classical problems of synchronization
Operating system 25 classical problems of synchronizationOperating system 25 classical problems of synchronization
Operating system 25 classical problems of synchronization
 
8086 instructions
8086 instructions8086 instructions
8086 instructions
 

Viewers also liked

Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
Bilal Amjad
 
Assembly Language Programming By Ytha Yu, Charles Marut Chap 7 (Logic, Shift,...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 7 (Logic, Shift,...Assembly Language Programming By Ytha Yu, Charles Marut Chap 7 (Logic, Shift,...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 7 (Logic, Shift,...
Bilal Amjad
 
Stack Applications
Stack ApplicationsStack Applications
Assembly Language Lecture 1
Assembly Language Lecture 1Assembly Language Lecture 1
Assembly Language Lecture 1
Motaz Saad
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
eShikshak
 
Stack
StackStack
organization structure
organization structureorganization structure
organization structure
Mukesh Gupta
 

Viewers also liked (7)

Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
 
Assembly Language Programming By Ytha Yu, Charles Marut Chap 7 (Logic, Shift,...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 7 (Logic, Shift,...Assembly Language Programming By Ytha Yu, Charles Marut Chap 7 (Logic, Shift,...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 7 (Logic, Shift,...
 
Stack Applications
Stack ApplicationsStack Applications
Stack Applications
 
Assembly Language Lecture 1
Assembly Language Lecture 1Assembly Language Lecture 1
Assembly Language Lecture 1
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
Stack
StackStack
Stack
 
organization structure
organization structureorganization structure
organization structure
 

Similar to Data Structure Lecture 3

Stack Algorithm
Stack AlgorithmStack Algorithm
Stack Algorithm
Kamal Singh Lodhi
 
sweetu stacks.pdf
sweetu stacks.pdfsweetu stacks.pdf
sweetu stacks.pdf
mohit476551
 
Introduction to programming in MATLAB
Introduction to programming in MATLABIntroduction to programming in MATLAB
Introduction to programming in MATLAB
mustafa_92
 
Fortran & Link with Library & Brief Explanation of MKL BLAS
Fortran & Link with Library & Brief Explanation of MKL BLASFortran & Link with Library & Brief Explanation of MKL BLAS
Fortran & Link with Library & Brief Explanation of MKL BLASJongsu "Liam" Kim
 
Stack
StackStack
The Stack And Recursion
The Stack And RecursionThe Stack And Recursion
The Stack And Recursion
Ashim Lamichhane
 
02 Stack
02 Stack02 Stack
lecture10trsgfchjvxgfzfdchgdchgcgshyjh.ppt
lecture10trsgfchjvxgfzfdchgdchgcgshyjh.pptlecture10trsgfchjvxgfzfdchgdchgcgshyjh.ppt
lecture10trsgfchjvxgfzfdchgdchgcgshyjh.ppt
partho5958
 
Lesson 4 stacks and queues
Lesson 4  stacks and queuesLesson 4  stacks and queues
Lesson 4 stacks and queues
MLG College of Learning, Inc
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7Kumar
 
Stack and queue
Stack and queueStack and queue
Stack and queue
LavanyaJ28
 
Stack unit-ii
Stack unit-iiStack unit-ii
Stack unit-ii
MamtaBhattarai3
 
lect- 3&4.ppt
lect- 3&4.pptlect- 3&4.ppt
lect- 3&4.ppt
mrizwan38
 

Similar to Data Structure Lecture 3 (13)

Stack Algorithm
Stack AlgorithmStack Algorithm
Stack Algorithm
 
sweetu stacks.pdf
sweetu stacks.pdfsweetu stacks.pdf
sweetu stacks.pdf
 
Introduction to programming in MATLAB
Introduction to programming in MATLABIntroduction to programming in MATLAB
Introduction to programming in MATLAB
 
Fortran & Link with Library & Brief Explanation of MKL BLAS
Fortran & Link with Library & Brief Explanation of MKL BLASFortran & Link with Library & Brief Explanation of MKL BLAS
Fortran & Link with Library & Brief Explanation of MKL BLAS
 
Stack
StackStack
Stack
 
The Stack And Recursion
The Stack And RecursionThe Stack And Recursion
The Stack And Recursion
 
02 Stack
02 Stack02 Stack
02 Stack
 
lecture10trsgfchjvxgfzfdchgdchgcgshyjh.ppt
lecture10trsgfchjvxgfzfdchgdchgcgshyjh.pptlecture10trsgfchjvxgfzfdchgdchgcgshyjh.ppt
lecture10trsgfchjvxgfzfdchgdchgcgshyjh.ppt
 
Lesson 4 stacks and queues
Lesson 4  stacks and queuesLesson 4  stacks and queues
Lesson 4 stacks and queues
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Stack unit-ii
Stack unit-iiStack unit-ii
Stack unit-ii
 
lect- 3&4.ppt
lect- 3&4.pptlect- 3&4.ppt
lect- 3&4.ppt
 

More from Teksify

HCTE C&C08(TDM)
HCTE C&C08(TDM) HCTE C&C08(TDM)
HCTE C&C08(TDM)
Teksify
 
Data Structure Lecture 7
Data Structure Lecture 7Data Structure Lecture 7
Data Structure Lecture 7Teksify
 
Data Structure Lecture 6
Data Structure Lecture 6Data Structure Lecture 6
Data Structure Lecture 6
Teksify
 
Data Structure Lecture 5
Data Structure Lecture 5Data Structure Lecture 5
Data Structure Lecture 5
Teksify
 
Data Structure Lecture 4
Data Structure Lecture 4Data Structure Lecture 4
Data Structure Lecture 4
Teksify
 
Data Structure Lecture 2
Data Structure Lecture 2Data Structure Lecture 2
Data Structure Lecture 2
Teksify
 
data Structure Lecture 1
data Structure Lecture 1data Structure Lecture 1
data Structure Lecture 1
Teksify
 
Variable power supply
Variable power supplyVariable power supply
Variable power supply
Teksify
 
Use of rib tool in pro e
Use of rib tool in pro eUse of rib tool in pro e
Use of rib tool in pro e
Teksify
 
Make lens of mobile by pro e
Make lens of mobile by pro eMake lens of mobile by pro e
Make lens of mobile by pro eTeksify
 

More from Teksify (12)

HCTE C&C08(TDM)
HCTE C&C08(TDM) HCTE C&C08(TDM)
HCTE C&C08(TDM)
 
Data Structure Lecture 7
Data Structure Lecture 7Data Structure Lecture 7
Data Structure Lecture 7
 
Data Structure Lecture 6
Data Structure Lecture 6Data Structure Lecture 6
Data Structure Lecture 6
 
Data Structure Lecture 5
Data Structure Lecture 5Data Structure Lecture 5
Data Structure Lecture 5
 
Data Structure Lecture 4
Data Structure Lecture 4Data Structure Lecture 4
Data Structure Lecture 4
 
Data Structure Lecture 2
Data Structure Lecture 2Data Structure Lecture 2
Data Structure Lecture 2
 
data Structure Lecture 1
data Structure Lecture 1data Structure Lecture 1
data Structure Lecture 1
 
Ch3
Ch3Ch3
Ch3
 
Ch1 2
Ch1 2Ch1 2
Ch1 2
 
Variable power supply
Variable power supplyVariable power supply
Variable power supply
 
Use of rib tool in pro e
Use of rib tool in pro eUse of rib tool in pro e
Use of rib tool in pro e
 
Make lens of mobile by pro e
Make lens of mobile by pro eMake lens of mobile by pro e
Make lens of mobile by pro e
 

Data Structure Lecture 3

  • 2. Topics Covered in This Lecture • Applications of stack – Reversing the string – Balancing symbols – Postfix expression evaluation – Translating infix expression to postfix expression
  • 3. Applications of Stack • Reversing the string – push each character on to a stack as it is read. – When the line is finished, we then pop characters off the stack, and they will come off in the reverse order.
  • 4. Applications of Stack Reversing the string void ReverseRead(void) { //using static Stack class of Lecture#3 Stack<char> stack;//The Stack ‘stack’ is created char item; cin>>item; while (!stack.isFull()&&item!='$')//type in $ from keyboard to stop input { stack.push(item); // push each character onto the stack cin>>item; } while(!stack.isEmpty() ) { item=stack.pop ();//Pop an element from stack cout<<item; } }
  • 5. Applications of Stack • Balancing Symbols Compilers use a program that checks whether every symbol (like braces, parenthesis, brackets, etc) in a program is balanced. The simple algorithm for this purpose is: 1. Make an empty stack. 2. Read the characters until end of file. 3. If the character is an open any thing, push it onto the stack. 4. If it is a close any thing, then if the stack is empty report an error. Otherwise Pop the Stack. If the popped symbol is not the corresponding opening symbol, then report an error. 5. At the end of the file, if the stack is not empty report an error.
  • 6. Polish Notation a–b/c+d*e Precedence? 1. b/c 2. d*e 3. a – a1 /a1 = b/c / 4. t2 + a2 / t2 = a – b/c / /a2 = d*e/
  • 7. Infix, Suffix, Prefix Infix = a * b + c ((a*b) +c) Priority: 1. a * b 2. a1 + c / a1 = a * b / Prefix = * a b , +a1 c +*abc Suffix = ab* , a1c+ ab*c+
  • 8. Infix, Suffix, Prefix infix = a- b * c / d + e / f suffix =a – bc* / d + e / f a – bc*d/ + e / f a – bc*d/ + ef/ abc*d/- + ef/ abc*d/-ef/+ prefix =a - *bc / d + e / f a - /*bcd + e / f a - /*bcd + /ef -a/*bcd + /ef +-a/*bcd/ef
  • 9. Infix, Suffix, Prefix Infix: a+b*c–d/f+e Suffix: abc*+df/-e+ Prefix: +-+a*bc/dfe
  • 10. Applications of Stack Postfix Expression Evaluation – When a number is seen, it is pushed onto the stack – When an operator is seen, then pop two elements from stack and push the result onto the stack. Now we evaluate the following postfix expression. 6523+8*+3+* 3 1. The first four are placed on the stack. The resulting stack is 2 5 6 stack
  • 11. Applications of Stack • evaluating the following postfix expression. 3 6523+8*+3+* 2 5 6 stack 2. Next a + is read, so 3 and 2 are popped from the stack and their sum 5 is pushed. 5 5 6 stack
  • 12. Applications of Stack • evaluating the following postfix expression. 6523+8*+3+* 5 5 6 stack 8 3. Next 8 is read and pushed. 5 5 6 stack
  • 13. Applications of Stack • evaluating the following postfix expression. 8 6523+8*+3+* 5 5 6 stack 4. Next a * is seen so 8 and 5 are popped as 8 * 5 = 40 is pushed 40 5 6 stack
  • 14. Applications of Stack • evaluating the following postfix expression. 6523+8*+3+* 40 5 6 stack 5. Next a + is read so 40 and 5 are popped and 40 + 5 = 45 is pushed. 45 6 stack
  • 15. Applications of Stack • evaluating the following postfix expression. 6523+8*+3+* 45 6 stack 6. Now 3 is pushed 3 45 6 stack
  • 16. Applications of Stack • evaluating the following postfix expression. 6523+8*+3+* 3 45 6 stack 7. Next symbol is + so pops 3 and 45 and pushes 45 + 3 = 48, so push 48 in stack. 48 6 stack
  • 17. Applications of Stack • evaluating the following postfix expression. 6523+8*+3+* 48 6 stack 7. Finally a * is seen and 48 and 6 are popped, the result 6 * 48 = 288 is pushed. 8. As there is no input, so pop the stack and we get the result. 288 stack
  • 18. Applications of Stack • Translating infix expressions to postfix expression – When an operand is read, it is immediately placed onto the output. – When an operator or left parenthesis comes then save it in the stack initially stack is empty. – If we see a right parenthesis, then we pop the stack, writing symbols until we encounter a (corresponding) left parenthesis, which is popped but not output. – If we see any other symbol (‘+’, ‘*’, ‘(‘, etc) then we pop entries form the stack until we find an entry of lower priority. One exception is that we never remove a ‘(‘ from the stack except when processing a ‘)’. – When the popping is done, we push the operand onto the stack. – Finally, if we read the end of input, we pop the stack until it is empty, writing symbols onto the output.
  • 19. Applications of Stack • Translating infix expressions to postfix expression Convert the following infix expression to postfix expression. a+b*c+(d*e+f)*g 1. First the symbol a is read, so it is passed through to the output a 2. Then + is read and pushed onto the stack. output + stack 3. Next b is read and passed through to the output. ab output * 4. Next a * is read. The top entry on the operator stack has lower + precedence than *, so nothing is output and * is put on the . stack
  • 20. Applications of Stack Converting the following infix expression to postfix expression. a+b*c+(d*e+f)*g 5. Next, c is read and output. abc output 6. The next symbol is a +. Checking the stack, we find that priority of stack top symbol * is higher than + . So we pop a * and place it on the output, Pop the other +, which is not of lower but equal priority, and then push +. * abc*+ + + output stack stack
  • 21. Applications of Stack Converting the following infix expression to postfix expression. a+b*c+(d*e+f)*g 7. The next symbol read is an ‘(‘, which, being of highest precedence, is placed on the stack. ( + stack 8. Then d is read and output. abc*+d output
  • 22. Applications of Stack Converting the following infix expression to postfix expression. a+b*c+(d*e+f)*g 9. We continue by reading a *. Since open parenthesis do not get removed except when a closed parenthesis is being processed, there is no output and we push * in stack * ( + stack 10. Next, e is read and output. abc*+de output
  • 23. Applications of Stack Converting the following infix expression to postfix expression. a+b*c+(d*e+f)*g 11. The next symbol read is a +, since priority of stack top value is higher so we pop * and push +. + abc*+de* ( output + stack 12. Now we read f and output f. abc*+de*f output
  • 24. Applications of Stack Converting the following infix expression to postfix expression. a+b*c+(d*e+f)*g 13. Now we read a ‘)’, so the stack is emptied back to the ‘(‘, we output a +. abc*+de*f+ + output stack 14. We read a * next; it is pushed onto the stack. * + stack 15. Now, g is read and output. abc*+de*f+g output
  • 25. Applications of Stack Converting the following infix expression to postfix expression. * a+b*c+(d*e+f)*g + stack 16. The input is now empty, so pop output symbols from the stack until it is empty. abc*+de*f+g*+ output stack
  • 26. Assignment 1 Group Size: 2, Due Date : Feb 27, 2012 • Implement Applications of stack 1. Balancing of symbols 2. Decimal to Binary Conversion 3. Infix to Postfix conversion 4. Postfix expression evaluation 5. Infix to Prefix conversion 6. Prefix expression evaluation 1. Each application will be implemented as a separate function. Function will use stack, which is already implemented in a header file and is available for reuse. 2. Difference between CGPAs of both group members should not be more than ONE