Professional Documents
Culture Documents
Stack
CSE Department
National Institute of Technology Rourkela
Reference
Most of the content of this presentation belongs to the following
book:
MAX=10
PUSH 6
POP
PEAK
Returns 5
Search: O(n)
Insertion: O(1)
Deletion: O(1)
() [(( )
( { } [ ]) ( { } [ ]))
({[][]}) (( { [ ] [ ] } )
[ { { { } ( )} [ ] } [ ] ( ) { } ([ { { { } ( )} [ ] } }[ ] ( ) { } ]
]
1
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Reverse-Polish Notation
Push 1 onto the stack
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
2
1
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Reverse-Polish Notation
Push 3 onto the stack
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
3
2
1
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Reverse-Polish Notation
Pop 3 and 2 and push 2 + 3 = 5
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
5
1
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Reverse-Polish Notation
Push 4 onto the stack
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
4
5
1
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Reverse-Polish Notation
Push 5 onto the stack
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
5
4
5
1
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Reverse-Polish Notation
Push 6 onto the stack
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
6
5
4
5
1
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Reverse-Polish Notation
Pop 6 and 5 and push 5 × 6 = 30
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
30
4
5
1
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Reverse-Polish Notation
Pop 30 and 4 and push 4 – 30 = –26
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
–26
5
1
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Reverse-Polish Notation
Push 7 onto the stack
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
7
–26
5
1
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Reverse-Polish Notation
Pop 7 and –26 and push –26 × 7 = –182
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
–182
5
1
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Reverse-Polish Notation
Pop –182 and 5 and push –182 + 5 = –177
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
–177
1
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Reverse-Polish Notation
Pop –177 and 1 and push 1 – (–177) = 178
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
178
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Reverse-Polish Notation
Push 8 onto the stack
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
8
178
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Reverse-Polish Notation
Push 1 onto the stack
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
9
8
178
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Reverse-Polish Notation
Pop 9 and 8 and push 8 × 9 = 72
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
72
178
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Reverse-Polish Notation
Pop 72 and 178 and push 178 + 72 = 250
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
250
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Reverse-Polish Notation
Thus
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
evaluates to the value on the top: 250
The equivalent in-fix notation is
((1 – ((2 + 3) + ((4 – (5 × 6)) × 7))) + (8 × 9))
Incidentally,
1 – 2 + 3 + 4 – 5 × 6 × 7 + 8 × 9 = – 132
which has the reverse-Polish notation of
1 2 – 3 + 4 + 5 6×7× – 8 9 × +
Example:
int Fact(int n)
{
if(n==1) //Base case
return 1;
Else // Recursive case
return (n * Fact(n–1));
}
A B C
• n > 0 gold disks to be moved from A to C using B
• move top n-1 disks from A to B using C
Recursive Solution
A B C
• move top disk from A to C
Recursive Solution
A B C
• move top n-1 disks from B to C using A
Recursive Solution
A B C
• moves(n) = 0 when n = 0
• moves(n) = 2*moves(n-1) + 1 = 2n-1 when n > 0
• End of chapter
Indirect Recursion
A function is said to be indirectly recursive if it
contains a call to another function which
ultimately calls it.
Tail Recursion
A recursive function is said to be tail recursive
if no operations are pending to be performed
when the recursive function returns to its
caller. when the called function returns, the
returned value is immediately returned from
the calling function. Tail recursive functions are
highly desirable because they are much more
efficient to use as the amount of information
that has to be stored on the system stack is
independent of the number of recursive calls.
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Non-Tail recursion to tail recusrion
Converting Recursive Functions to Tail Recursive