07 DS and Algorithm Session 10
07 DS and Algorithm Session 10
Ver. 1.0
Session 10
7 6 7 6
Ver. 1.0
Session 10
Ver. 1.0
Session 10
Empty Stack
Element 1
Ver. 1.0
Session 10
3 2 1
Ver. 1.0
Session 10
POP an Element 3 3 2 1 2
Ver. 1.0
Session 10
Answer:
LIFO
Ver. 1.0
Session 10
Session 10
Ver. 1.0
Session 10
{(a + b) (c + d) + (c d)]}
Scan the expression from left to right. The first entry to be scanned is {, which is a left parenthesis. Push it into the stack.
{
Ver. 1.0
Session 10
( {
Ver. 1.0
Session 10
Brackets Matched
( {
Ver. 1.0
Session 10
Session 10
Brackets Matched
( {
Ver. 1.0
Session 10
Session 10
Brackets Matched
( {
Ver. 1.0
Session 10
Ver. 1.0
Session 10
Ver. 1.0
Session 10
1. 2.
Increment top by 1. Store the value to be pushed at index top in the array. Top now contains the index of the topmost element.
0
Stack
Ver. 1.0
Session 10
1. 1.
Increment top by 1. Store the value to be pushed at index top in the array. Top now contains the index of the topmost element.
0
Stack
1 10
Ver. 1.0
Session 10
1. 1.
Increment top by 1. Store the value to be pushed at index top in the array. Top now contains the index of the topmost element.
0
Stack
1 10
top = 0
Ver. 1.0
Session 10
1. 1.
Increment top by 1. Store the value to be pushed at index top in the array. Top now contains the index of the topmost element.
PUSH an element 3
0
Stack 3 10 top = 0
4
Item pushed
Ver. 1.0
Session 10
1. 2.
Increment top by 1. Store the value to be pushed at index top in the array. Top now contains the index of the topmost element.
PUSH an element 8
0
Stack 3 10 top = 0
Ver. 1.0
Session 10
1. 1.
Increment top by 1. Store the value to be pushed at index top in the array. Top now contains the index of the topmost element.
PUSH an element 8
0
Stack 3 10
top = 0 = 1 top
Ver. 1.0
Session 10
1. 1.
Increment top by 1. Store the value to be pushed at index top in the array. Top now contains the index of the topmost element.
PUSH an element 8
0
Stack
4
Item pushed
3 10 8 top = 1
Ver. 1.0
Session 10
1. 2.
Increment top by 1. Store the value to be pushed at index top in the array. Top now contains the index of the topmost element.
PUSH an element 5
0
Stack
3 10 8 top = 1
Ver. 1.0
Session 10
1. 1.
Increment top by 1. Store the value to be pushed at index top in the array. Top now contains the index of the topmost element.
PUSH an element 5
0
Stack
3 10 8 top = top = 2 1
Ver. 1.0
Session 10
1. 1.
Increment top by 1. Store the value to be pushed at index top in the array. Top now contains the index of the topmost element.
PUSH an element 5
0
Stack
2
5 top = 2
4
Item pushed
3 10 8
Ver. 1.0
Session 10
1. 2.
Increment top by 1. Store the value to be pushed at index top in the array. Top now contains the index of the topmost element.
PUSH an element 1
0
Stack
2
5 top = 2
3 10 8
Ver. 1.0
Session 10
1. 1.
Increment top by 1. Store the value to be pushed at index top in the array. Top now contains the index of the topmost element.
PUSH an element 1
0
Stack
2
5
3 10 8
top = 2 = 3 top
Ver. 1.0
Session 10
1. 1.
Increment top by 1. Store the value to be pushed at index top in the array. Top now contains the index of the topmost element.
PUSH an element 1
0
Stack
2
5
3
1 top = 3
4
Item pushed
3 10 8
Ver. 1.0
Session 10
1. 2.
Increment top by 1. Store the value to be pushed at index top in the array. Top now contains the index of the topmost element.
PUSH an element 9
0
Stack
2
5
3
1 top = 3
3 10 8
Ver. 1.0
Session 10
1. 1.
Increment top by 1. Store the value to be pushed at index top in the array. Top now contains the index of the topmost element.
PUSH an element 9
0
Stack
2
5
3
1
3 10 8
top top = 3 = 4
Ver. 1.0
Session 10
1. 1.
Increment top by 1. Store the value to be pushed at index top in the array. Top now contains the index of the topmost element.
PUSH an element 9
0
Stack
2
5
3
1
4
9 top = 4 Item pushed
3 10 8
Ver. 1.0
Session 10
1. 2.
Increment top by 1. Store the value to be pushed at index top in the array. Top now contains the index of the topmost element.
PUSH an element 2
0
Stack
2
5
3
1
4
9 top = 4
3 10 8
Ver. 1.0
Session 10
1. 1.
Increment top by 1. Store the value to be pushed at index top in the array. Top now contains the index of the topmost element.
PUSH an element 2
0
Stack
2
5
3
1
4
9 top = 5 top = 4
3 10 8
Ver. 1.0
Session 10
1. 1.
Increment top by 1. Store the value to be pushed at index top in the array. Top now contains the index of the topmost element.
PUSH an element 2
0
Stack
2
5
3
1
4
9 top = 5 Stack overflow
3 10 8
Ver. 1.0
Session 10
1. 2. 2. 3.
Increment topby 1. If top = MAX 1: a. Display Stack Full Store the value to be b. Exit pushed at index top in the array. Top now Increment top by 1 contains the index of the topmost element. be Store the value to pushed at index top in the array
0
Stack
2
5
3
1
4
9
3 10 8
Ver. 1.0
Session 10
Ver. 1.0
Session 10
Answer:
top
Ver. 1.0
Session 10
Ver. 1.0
Session 10
Ver. 1.0
Session 10
Ver. 1.0
Session 10
Answer:
When a stack is implemented as a linked list, there is no upper bound limit on the size of the stack. Therefore, there will be no stack full condition in this case.
Ver. 1.0
Session 10
Answer:
2. At the beginning of the list
Ver. 1.0
Session 10
Ver. 1.0
Session 10
Ver. 1.0
Session 10
Session 10
Session 10
Session 10
x=5
Session 10
x=5
1103, x = 5
Session 10
x = 10 5
1103, x = 5
Session 10
Session 10
Session 10
x = 10 20
20
Session 10
x = 10 5
1103, x = 5
1103, x = 5
20
10
Session 10
x=5
20
10
5
Session 10
Ver. 1.0
Session 10
Ver. 1.0
Session 10
Ver. 1.0
Session 10
Ver. 1.0
Session 10
Ver. 1.0
Session 10
Ver. 1.0
Session 10