Data Structures with C Using STL - PowerPoint PPT Presentation

1 / 116
About This Presentation
Title:

Data Structures with C Using STL

Description:

Return a reference to the value of the item at the top of the stack. ... charStack.push(letter); charStack.push( C'); charStack.push( S'); if ( !charStack.empty ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 117
Provided by: ruthu
Category:

less

Transcript and Presenter's Notes

Title: Data Structures with C Using STL


1
Data Structures with CUsing STL
  • Chapter Seven
  • Stacks

2
What is a Stack?
  • A stack is an ordered group of homogeneous items
    (elements), in which the removal and addition of
    stack items can take place only at the top of the
    stack.
  • A stack is a LIFO last in, first out structure.

3
Stack API
CLASS stack Constructors
ltstack.hgt
stack() Create an empty stack.
CLASS stack Operations
ltstack.hgt
bool empty() const Check whether the stack
is empty. Return true if it is empty, false
otherwise. void pop() Remove the item from
the top of the stack. Precond The stack is
not empty. Postcond Either the stack is
empty or the stack has a new topmost item from
the previous push. void push(const T
item) Insert the argument item at the top of
the stack. Postcond The stack has a new
item at the top.
4
Stack API
CLASS stack Operations
ltstack.hgt
int size() const Return the number of items
on the stack. T top() Return a reference
to the value of the item at the top of the
stack. Precond The stack is not
empty. const T top() Constant version of
top.
5
Tracing Client Code
V
letter
char letter V stackltchargt
charStack charStack.push(letter) charStack.push(
C) charStack.push(S) if ( !charStack.empty(
)) letter charStack.top()
charStack.pop() charStack.push(K) whi
le (!charStack.empty( )) letter
charStack.top() charStack.pop()

6
Tracing Client Code
V
letter
char letter V stackltchargt charStack charStac
k.push(letter) charStack.push(C) charStack.pus
h(S) if ( !charStack.empty( ))
letter charStack.top()
charStack.pop() charStack.push(K) whi
le (!charStack.empty( )) letter
charStack.top() charStack.pop()

Private data
4
3
2
Top
1
-1
0
7
Tracing Client Code
V
letter
char letter V stackltchargt charStack charStac
k.push(letter) charStack.push(C) charStack.pus
h(S) if ( !charStack.empty( ))
letter charStack.top()
charStack.pop() charStack.push(K) whi
le (!charStack.empty( )) letter
charStack.top() charStack.pop()

Private data
4
3
2
1
Top
0
'V'
0
8
Tracing Client Code
V
letter
char letter V stackltchargt charStack charStac
k.push(letter) charStack.push(C) charStack.pus
h(S) if ( !charStack.empty( ))
letter charStack.top()
charStack.pop() charStack.push(K) whi
le (!charStack.empty( )) letter
charStack.top() charStack.pop()

Private data
4
3
2
1
Top
'C'
1
'V'
0
9
Tracing Client Code
V
letter
char letter V stackltchargt charStack charStac
k.push(letter) charStack.push(C) charStack.pus
h(S) if ( !charStack.empty( ))
letter charStack.top()
charStack.pop() charStack.push(K) whi
le (!charStack.empty( )) letter
charStack.top() charStack.pop()

Private data
4
3
S'
2
1
Top
'C'
2
'V'
0
10
Tracing Client Code
V
letter
char letter V stackltchargt charStack charStac
k.push(letter) charStack.push(C) charStack.pus
h(S) if ( !charStack.empty( ))
letter charStack.top()
charStack.pop() charStack.push(K) whi
le (!charStack.empty( )) letter
charStack.top() charStack.pop()

Private data
4
3
S'
2
1
Top
'C'
2
'V'
0
11
Tracing Client Code
S
letter
char letter V stackltchargt charStack charStac
k.push(letter) charStack.push(C) charStack.pus
h(S) if ( !charStack.empty( ))
letter charStack.top()
charStack.pop() charStack.push(K) whi
le (!charStack.empty( )) letter
charStack.top() charStack.pop()

Private data
4
3
S
2
1
Top
'C'
1
'V'
0
12
Tracing Client Code
S
letter
char letter V stackltchargt charStack charStac
k.push(letter) charStack.push(C) charStack.pus
h(S) if ( !charStack.empty( ))
letter charStack.top()
charStack.pop() charStack.push(K) whi
le (!charStack.empty( )) letter
charStack.top() charStack.pop()

Private data
4
3
K'
2
1
Top
'C'
2
'V'
0
13
Tracing Client Code
S
letter
char letter V stackltchargt charStack charStack
.push(letter) charStack.push(C) charStack.push
(S) if ( !charStack.empty( )) letter
charStack.top() charStack.pop()
charStack.push(K) while
(!charStack.empty( )) letter
charStack.top() charStack.pop()

Private data
4
3
K'
2
1
Top
'C'
2
'V'
0
14
Tracing Client Code
K
letter
char letter V stackltchargt charStack charStac
k.push(letter) charStack.push(C) charStack.pus
h(S) if ( !charStack.empty( ))
letter charStack.top()
charStack.pop() charStack.push(K) whi
le (!charStack.empty( )) letter
charStack.top() charStack.pop()

Private data
4
3
K'
2
1
Top
'C'
1
'V'
0
15
Tracing Client Code
K
letter
char letter V stackltchargt charStack charStack
.push(letter) charStack.push(C) charStack.push
(S) if ( !charStack.empty( )) letter
charStack.top() charStack.pop()
charStack.push(K) while
(!charStack.empty( )) letter
charStack.top() charStack.pop()

Private data
4
3
K'
2
1
Top
'C'
1
'V'
0
16
Tracing Client Code
C
letter
char letter V stackltchargt charStack charStac
k.push(letter) charStack.push(C) charStack.pus
h(S) if ( !charStack.empty( ))
letter charStack.top()
charStack.pop() charStack.push(K) whi
le (!charStack.empty( )) letter
charStack.top() charStack.pop()

Private data
4
3
K'
2
1
Top
C'
0
'V'
0
17
Tracing Client Code
C
letter
char letter V stackltchargt charStack charStack
.push(letter) charStack.push(C) charStack.push
(S) if ( !charStack.empty( )) letter
charStack.top() charStack.pop()
charStack.push(K) while
(!charStack.empty( )) letter
charStack.top() charStack.pop()

Private data
4
3
K'
2
1
Top
C'
0
'V'
0
18
Tracing Client Code
V
letter
char letter V stackltchargt charStack charStac
k.push(letter) charStack.push(C) charStack.pus
h(S) if ( !charStack.empty( ))
letter charStack.top()
charStack.pop() charStack.push(K) whi
le (!charStack.empty( )) letter
charStack.top() charStack.pop()

Private data
4
3
K'
2
1
Top
C'
-1
'V'
0
19
Tracing Client Code
V
letter
char letter V stackltchargt charStack charStack
.push(letter) charStack.push(C) charStack.push
(S) if ( !charStack.empty( )) letter
charStack.top() charStack.pop()
charStack.push(K) while
(!charStack.empty( )) letter
charStack.top() charStack.pop()

Private data
4
3
K'
2
1
Top
C'
-1
'V'
0
20
Example - Sorting using stacks
45
71
38
62
First value is 45 Put it on the Sorted Stack
Sorted Stack
temp Stack
21
Example - Sorting using stacks
71
38
62
Next value is 71 Since 71 is larger, pop Sorted
Stack to determine where to put it placing each
value popped onto temp Stack.
45
Sorted Stack
temp Stack
22
Example - Sorting using stacks
38
62
Next value is 71 Since 71 is larger, pop Sorted
Stack to determine where to put it placing each
value popped onto temp Stack.
When you insert 71 in correct spot on Sorted
Stack, pop temp Stack until empty putting each
item back onto Sorted Stack
45
71
Sorted Stack
temp Stack
23
Example - Sorting using stacks
38
62
Next value is 71 Since 71 is larger, pop Sorted
Stack to determine where to put it placing each
value popped onto temp Stack. When you insert 71
in correct spot on Sorted Stack, pop temp Stack
until empty putting each item back onto Sorted
Stack
45
71
Sorted Stack
temp Stack
24
Example - Sorting using stacks
38
62
Next value is 38 Since 38 is smaller than 45 (top
of Sorted Stack) Place it on top of Sorted Stack.
45
71
Sorted Stack
temp Stack
25
Example - Sorting using stacks
62
Next value is 62 Pop 38 off Sorted Stack and push
onto tempStack.
38
45
71
Sorted Stack
temp Stack
26
Example - Sorting using stacks
62
Next value is 62 Pop 38 off Sorted Stack and push
onto tempStack. Pop 45 off Sorted Stack and push
onto tempStack.
45
71
38
Sorted Stack
temp Stack
27
Example - Sorting using stacks
62
Next value is 62 Pop 38 off Sorted Stack and push
onto tempStack. Pop 45 off Sorted Stack and push
onto tempStack. Push 62 onto Sorted Stack since
it is smaller than 71.
45
71
38
Sorted Stack
temp Stack
28
Example - Sorting using stacks
Now pop temp Stack until empty pushing items one
at a time onto Sorted Stack.
45
62
71
38
Sorted Stack
temp Stack
29
Example - Sorting using stacks
Now pop temp Stack until empty pushing items one
at a time onto Sorted Stack.
45
62
71
38
Sorted Stack
temp Stack
30
Example - Sorting using stacks
Now pop temp Stack until empty pushing items one
at a time onto Sorted Stack.
38
45
62
71
Sorted Stack
temp Stack
31
Function to sort input data
  • template lttypename Tgt
  • void sortInput(ListltTgt L)
  • stack ltTgt Sorted, temp// stacks initialized to
    empty
  • ListltTgtiterator curr, stop
  • curr L.begin() // initialize iterators
  • stop L.end()

32
  • // loop to process each item in the list
  • while (curr ! stop)
  • T stackTop, item
  • item curr // get curr item of list L
  • while (! Sorted.empty() Sorted.top() lt
    item))
  • stackTop Sorted.top() // get top item
    from stack
  • Sorted.pop() // delete top of Sorted
    stack
  • temp.push(stackTop) // push stackTop
    onto temp stack
  • Sorted.push(item) // put item onto
    Sorted stack
  • // put all items from temp back onto the Sorted
    stack
  • while(! temp.empty())
  • stackTop temp.top() // get top item of
    temp
  • temp.pop() // delete top item of
    temp
  • Sorted.push(stackTop) // put item from
    temp onto Sorted
  • curr // update curr to
    next item in list L

33
  • // move items from stack back into the list
  • while (!Sorted.empty())
  • // save and remove top of Sorted stack
  • stackTop Sorted.top()
  • Sorted.pop()
  • // place top of Sorted stack at rear of list
    L
  • L.push_back(stackTop)

34
Stack Implementation as vector
  • template lttypename Tgt
  • class stack
  • public
  • stack()
  • // constructor, create an empty stack
  • void push(const T item)
  • // push (insert) item onto the stack
  • // Postcond the stack has a new topmost
    element and the
  • // stack size is increased by 1
  • void pop()
  • // remove the item from the top of the stack.
  • // Precond the stack is not empty.

35
Stack Implementation as vector-cont.
  • T top()
  • // return a reference to the element on the top
    of the stack.
  • // Precond the stack is not empty.
  • const T top() const
  • // constant version of top()
  • bool empty() const
  • // determine whether the stack is empty
  • int size() const
  • // return the number of elements in the stack
  • private
  • vectorltTgt stackVector
  • // a vector object maintains the stack items
    and size

36
Stack Implementation as vector-cont.
template lttypename Tgt stackltTgtstack()
template lttypename Tgt void stackltTgtpush(const
T item) stackVector.push_back(item) templa
te lttypename Tgt void stackltTgtpop()
stackVector.pop_back()

37
Stack Implementation as vector-cont.
template lttypename Tgt T stackltTgttop() if
(empty()) return T() return
stackVector.back() template lttypename
Tgt const T stackltTgttop() const if (empty())
return const T() return stackVector.back()

38
Stack Implementation as vector-cont.
template lttypename Tgt bool stackltTgtempty() r
eturn stackVector.empty() template lttypename
Tgt int stackltTgtsize() return
stackVector.size()
39
Representation of Arithmetic Expressions
  • Prefix Notation - operator precedes its
    operands. A B - C D
  • Infix Notation - operator is between its
    operands. ( A B) ( C - D )
  • Postfix Notation - operator follows its operands.
    A B C D -

40
Stack Application 1Evaluation of Postfix
Expressions
  • An Example
  • 6 3 7 2 -
  • 9
  • 9 7 2 -
  • 5
  • 9 5
  • 45

41
Evaluation of Postfix Expressions
  • Operands are placed onto the stack until their
    operator is located.
  • Operators are executed when they are located.
  • Pop stack to remove operands
  • Evaluate expression
  • Place result onto stack

42
An Example - Stack is empty
5 2 - 4 6
stack operands
43
An Example - Push 5
5 2 - 4 6
operands

operands. push (5)
5
44
An Example - Push 2
5 2 - 4 6
operands

operands.push(2)
2
5
45
An Example - Process -
5 2 - 4 6
operands

op2 operands.top() operands.pop() op1
operands.top() operands.pop() ans op1 -
op2 operands.push (ans)
op1 op2 ans
5
2
46
An Example - Process -
5 2 - 4 6
operands

op2 operands.top() operands.pop() op1
operands.top() operands.pop() ans op1 -
op2 operands.push (ans)
Op1 Op2 Ans
5
2
47
An Example - Process -
5 2 - 4 6
operands

op2 operands.top() operands.pop() op1
operands.top() operands.pop() ans op1 -
op2 operands.push (ans)
Op1 Op2 Ans
5
2
3
48
An Example - Process -
5 2 - 4 6
operands
Private Data Top maxItems-1 . . .
3 2
1 items0

op2 operands.top() operands.pop() op1
operands.top() operands.pop() ans op1 -
op2 operands.push (ans)
0
Op1 Op2 Ans
2
3
3
5
2
3
49
An Example - Push 4
5 2 - 4 6
operands

operands.push(4)
4
3
50
An Example - Push 6
5 2 - 4 6
operands

operands.push(6)
6
4
3
51
An Example - Process
5 2 - 4 6
operands

op2 operands.top() operands.pop() op1
operands.top() operands.pop() ans op1
op2 operands.push (ans)
4
Op1 Op2 Ans
3
6
52
An Example - Process
5 2 - 4 6
operands

op2 operands.top() operands.pop() op1
operands.top() operands.pop() ans op1
op2 operands.push (ans)
Op1 Op2 Ans
3
4
6
53
An Example - Process
5 2 - 4 6
operands

op2 operands.top() operands.pop() op1
operands.top() operands.pop() ans op1
op2 operands.push (ans)
Op1 Op2 Ans
3
4
6
10
54
An Example - Process
5 2 - 4 6
operands

op2 operands.top() operands.pop() op1
operands.top() operands.pop() ans op1
op2 operands.push (ans)
10
Op1 Op2 Ans
3
4
6
10
55
An Example - Process
5 2 - 4 6
operands

op2 operands.top() operands.pop() op1
operands.top() operands.pop() ans op1
op2 operands.push (ans)
Op1 Op2 Ans
3
10
56
An Example - Process
5 2 - 4 6
operands

op2 operands.top() operands.pop() op1
operands.top() operands.pop() ans op1
op2 operands.push (ans)
Op1 Op2 Ans
3
10
57
An Example - Process
5 2 - 4 6
operands

op2 operands.top() operands.pop() op1
operands.top() operands.pop() ans op1
op2 operands.push (ans)
Op1 Op2 Ans
3
10
30
58
An Example - Process
5 2 - 4 6
operands

op2 operands.top() operands.pop() op1
operands.top() operands.pop() ans op1
op2 operands.push (ans)
Op1 Op2 Ans
30
3
10
30
59
Stack Application 2Conversion from Infix to
Postfix
  • Operands placed in postfix expression as they are
    encountered.
  • Operators need to be stored on stack until they
    should be executed.
  • Precedence
  • Parentheses
  • Current Operator pushed onto stack
  • Dealing with the stack at end of expression

60
Dealing with operators
  • While the operator at the top of the stack must
    be executed before the current operator, pop the
    stack and place operator into postfix expression.
  • If current token is open parentheses just push it
    onto the stack, it acts as a barrier. The only
    operator to remove it is a close parentheses.
  • If current token is a close parentheses, pop the
    stack while top is not an open parentheses. Then
    pop and discard the open parentheses.
  • Always push all current operators (except close
    parentheses) onto the stack.

61
How to finish processing after last token
  • Pop all remaining operators off the stack after
    the last token has been processed. Place each
    operator onto the postfix expression as it is
    popped.

62
An Example Infix to Postfix
  • A B C - D

stackltchargt operator char token, stTop Cstring
postExpr bool done false
63
An Example Infix to Postfix
  • A B C - D


operator
token
postExprtoken
A
done
false
stTop
postExpr
A
64
An Example Infix to Postfix
  • A B C - D

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false

operator
token

done
false
stTop
postExpr
A
65
An Example Infix to Postfix
  • A B C - D

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
66
An Example Infix to Postfix
  • A B C - D

postExprtoken
67
An Example Infix to Postfix
  • A B C - D

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
68
An Example Infix to Postfix
  • A B C - D

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false

operator
token

done
false
stTop


postExpr
AB
69
An Example Infix to Postfix
  • A B C - D

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
70
An Example Infix to Postfix
  • A B C - D

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false

operator
token

done
true
stTop


postExpr
AB
71
An Example Infix to Postfix
  • A B C - D

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false

72
An Example Infix to Postfix
  • A B C - D

postExprtoken
73
An Example Infix to Postfix
  • A B C - D

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
74
An Example Infix to Postfix
  • A B C - D

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
75
An Example Infix to Postfix
  • A B C - D

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
76
An Example Infix to Postfix
  • A B C - D

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
77
An Example Infix to Postfix
  • A B C - D

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false

operator
token
-
done
false
stTop


postExpr
ABC
78
An Example Infix to Postfix
  • A B C - D

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
79
An Example Infix to Postfix
  • A B C - D

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
80
An Example Infix to Postfix
  • A B C - D

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
-
81
An Example Infix to Postfix
  • A B C - D

postExprtoken
-
82
An Example Infix to Postfix
  • A B C - D

while (!operator.empty()) stTop
operator.top() operator.pop()
postExpr stTop
-
83
An Example Infix to Postfix
  • A B C - D

while (!operator.empty()) stTop
operator.top() operator.pop()
postExpr stTop
84
An Example Infix to Postfix
  • A B C - D

while (!operator.empty()) stTop
operator.top() operator.pop()
postExpr stTop
85
Example 2 Infix to Postfix
  • (A - B) / (D E)

stack operator char token, stTop Cstring
postExpr bool done false
86
Example 2 Infix to Postfix
  • (A - B) / (D E)

if (token '(' ) operator.push(token)
87
Example 2 Infix to Postfix
  • (A - B) / (D E)

postExprtoken
88
Example 2 Infix to Postfix
  • (A - B) / (D E)

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
89
Example 2 Infix to Postfix
  • (A - B) / (D E)

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
operator
90
Example 2 Infix to Postfix
  • (A - B) / (D E)

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
-
true
(
'('
A
91
Example 2 Infix to Postfix
  • (A - B) / (D E)

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
92
Example 2 Infix to Postfix
  • (A - B) / (D E)

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
-
93
Example 2 Infix to Postfix
  • (A - B) / (D E)

postExprtoken
94
Example 2 Infix to Postfix
  • (A - B) / (D E)

if (token ')' ) stTop
operator.top() while (stTop!'(' )
postExprstTop
operator.pop() stTop operator.top()
operator.pop() //
discard '('
95
Example 2 Infix to Postfix
  • (A - B) / (D E)

if (token ')' ) stTop
operator.top() while (stTop!'(' )
postExprstTop
operator.pop() stTop operator.top()
operator.pop() //
discard '('
96
Example 2 Infix to Postfix
  • (A - B) / (D E)

if (token ')' ) stTop
operator.top() while (stTop!'(' )
postExprstTop
operator.pop() stTop operator.top()
operator.pop() //
discard '('
97
Example 2 Infix to Postfix
  • (A - B) / (D E)

if (token ')' ) stTop
operator.top() while (stTop!'(' )
postExprstTop
operator.pop() stTop operator.top()
operator.pop() //
discard '('
)
false
(
('
AB-
98
Example 2 Infix to Postfix
  • (A - B) / (D E)

if (token ')' ) stTop
operator.top() while (stTop!'(' )
postExprstTop
operator.pop() stTop operator.top()
operator.pop() //
discard '('
99
Example 2 Infix to Postfix
  • (A - B) / (D E)

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
100
Example 2 Infix to Postfix
  • (A - B) / (D E)

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false
/
101
Example 2 Infix to Postfix
  • (A - B) / (D E)


operator
token
if (token '(' ) operator.push(token)
'('
done
false
(
stTop
/
('
postExpr
AB-
102
Example 2 Infix to Postfix
  • (A - B) / (D E)


operator
token
postExprtoken
D
done
false
(
stTop
/
('
postExpr
AB-D
103
Example 2 Infix to Postfix
  • (A - B) / (D E)

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false

operator
token

done
false
(
stTop
/
('
postExpr
AB-D
104
Example 2 Infix to Postfix
  • (A - B) / (D E)

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false

operator
token

done
false
(
stTop
/
'('
postExpr
AB-D
105
Example 2 Infix to Postfix
  • (A - B) / (D E)

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false

true
(
/
'('
AB-D
106
Example 2 Infix to Postfix
  • (A - B) / (D E)

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false

true
(
/
'('
AB-D
107
Example 2 Infix to Postfix
  • (A - B) / (D E)

while (!operator.empty() !done) stTop
operator.top() if (precedence(stTop,
token) operator.pop() postExpr
stTop else done true
operator.push(token) done false


false
(
/
'('
AB-D
108
Example 2 Infix to Postfix
  • (A - B) / (D E)

postExprtoken
E

false
(
/
'('
AB-DE
109
Example 2 Infix to Postfix
  • (A - B) / (D E)

if (token ')' ) stTop
operator.top() while (stTop!'(' )
postExprstTop
operator.pop() stTop operator.top()
operator.pop() //
discard '('
)

false
(
/
''
AB-DE
110
Example 2 Infix to Postfix
  • (A - B) / (D E)

if (token ')' ) stTop
operator.top() while (stTop!'(' )
postExprstTop
operator.pop() stTop operator.top()
operator.pop() //
discard '('
)

false
(
/
''
AB-DE
111
Example 2 Infix to Postfix
  • (A - B) / (D E)

if (token ')' ) stTop
operator.top() while (stTop!'(' )
postExprstTop
operator.pop() stTop operator.top()
operator.pop() //
discard '('
)
false
(
/
'('
AB-DE
112
Example 2 Infix to Postfix
  • (A - B) / (D E)


operator
token
if (token ')' ) stTop
operator.top() while (stTop!'(' )
postExprstTop
operator.pop() stTop operator.top()
operator.pop() //
discard '('
)
done
false
(
stTop
/
'('
postExpr
AB-DE
113
Example 2 Infix to Postfix
  • (A - B) / (D E)

if (token ')' ) stTop
operator.top() while (stTop!'(' )
postExprstTop
operator.pop() stTop operator.top()
operator.pop() //
discard '('
)
false
/
'('
AB-DE
114
Example 2 Infix to Postfix
  • (A - B) / (D E)

while (!operator.empty()) stTop
operator.top() operator.pop()
postExpr stTop

operator
token
\0
done
false
stTop
/
'('
postExpr
AB-DE
115
Example 2 Infix to Postfix
  • (A - B) / (D E)

while (!operator.empty()) stTop
operator.top() operator.pop()
postExpr stTop
AB-DE/
116
Example 2 Infix to Postfix
  • (A - B) / (D E)

while (!operator.empty()) stTop
operator.top() operator.pop()
postExpr stTop

operator
token
\0
done
false
stTop
/
postExpr
AB-DE/
Write a Comment
User Comments (0)
About PowerShow.com