Data Structures: The Stack
Data Structures: The Stack
Data Structures
Lecture 4 :
The Stack
The Stack
The Stack
3.4.1. RECURSION
called recursive function. The general algorithm model for any recursive function
2. Body: If the base criterion has been reached, then perform the final
3. Epilogue: Restore the most recently saved parameters, local variables, and
return address.
4
The Stack
5
The Stack
and other arguments and its data(s) for future use) are pushed onto the
program stack. When the procedure or function returns, this frame of data
The Stack
When the function completes its execution these parameters are popped
The Stack
The Stack
Recursion of course is an elegant programming technique, but not the best way
reasons:
3. Moreover it also slows down execution speed, as function calls require jumps,
and saving the current state of program onto stack before jump.
9
The Stack
Given below are some of the important points, which differentiate
iteration from recursion.
The Stack
3.4.3. EXPRESSION
1. Infix notation
2. Prefix notation
3. Postfix notation
11
The Stack
operands, like: + A B
operands, like: A B +
12
The Stack
The Stack
rules.
14
The Stack
Notation Conversions
The Stack
3.3.1 CONVERTING INFIX TO POSTFIX EXPRESSION
having higher precedence are first parenthesized. For example in the above
The Stack
Algorithm
1. Push “(” onto stack, and add “)” to the end .
2. Scan from left to right and repeat Steps 3 to 6 for each element until the stack is
empty.
3. If an operand is encountered, add it to Postfix_Stack (Q).
4. If a left parenthesis is encountered, push it onto stack.
5. If an operator ⊗ is encountered, then:
(a) Repeatedly pop from stack, if not its precedence higher precedence than ⊗.
(b) Else Add ⊗ to stack.
6. If a right parenthesis is encountered, then:
(a) Repeatedly pop from stack until a left parenthesis is encountered.
(b) Remove the left parenthesis.
7. Exit.
17
The Stack
For, example consider the following arithmetic Infix to Postfix expression
I =(A+(B*C-(D/E^F)*G)*H)
Sr. No Symbol Scanned STACK Postfix_Stack (Q)
1 A ( A
2 + (+ A
3 ( (+( A
4 B (+( AB
5 * (+(* AB
6 C (+(* ABC
7 - (+(- ABC*
8 ( (+(-( ABC*D
9 D (+(-( ABC*D
18
The Stack
Sr. No Symbol Scanned STACK Expression
10 / (+(-(/ ABC*D
11 E (+(-(/ ABC*DE
12 ^ (+(-(/^ ABC*DE
13 F (+(-(/^ ABC*DEF
14 ) (+(- ABC*DEF^/
15 * (+(-* ABC*DEF^/
16 G (+(-* ABC*DEF^/G
17 ) (+ ABC*DEF^/G*
18 * (+* ABC*DEF^/G*-
19 H (+(* ABC*DEF^/G*-H
20 ) - ABC*DEF^/G*-H*+
19
The Stack
3.3.2 CONVERTING POSTFIX TO INFIX EXPRESSION
3. Write the Postfix expression like this Left most Char at top or Right most
5. Apply POP on all elements one by one starting from TOP of stack
20
The Stack
For, example consider the following arithmetic Postfix to Infix expression
ABC*+DE/F*-
21
The Stack
For, example consider the following arithmetic Postfix to Infix expression
ABC*+DE/F*-
22
The Stack
For, example consider the following arithmetic Postfix to Infix expression
ABC*+DE/F*-
23
The Stack
For, example consider the following arithmetic Postfix to Infix expression
ABC*+DE/F*-
24
The Stack
For, example consider the following arithmetic Postfix to Infix expression
ABC*+DE/F*-
25
The Stack
For, example consider the following arithmetic Postfix to Infix expression
ABC*+DE/F*-
26
The Stack
For, example consider the following arithmetic Postfix to Infix expression
ABC*+DE/F*-
27
The Stack
For, example consider the following arithmetic Postfix to Infix expression
ABC*+DE/F*-
28
The Stack
For, example consider the following arithmetic Postfix to Infix expression
ABC*+DE/F*-
29
The Stack
For, example consider the following arithmetic Postfix to Infix expression
ABC*+DE/F*-
30
The Stack
For, example consider the following arithmetic Postfix to Infix expression
ABC*+DE/F*-
31
The Stack
For, example consider the following arithmetic Postfix to Infix expression
ABC*+DE/F*-
32
The Stack
For, example consider the following arithmetic Postfix to Infix expression
ABC*+DE/F*-
33
The Stack
For, example consider the following arithmetic Postfix to Infix expression
ABC*+DE/F*-
34
The Stack
3.3.3 CONVERTING INFIX TO PREFIX EXPRESSION
The Stack
For, example consider the following arithmetic Infix to Pretfix expression
The Stack
Prefix expression is
+/*+AB+CD–EFG
38
The Stack
The Stack
Compulsory:
The Stack
Any Questions?