The document discusses stacks and their operations including push and pop. It defines a stack as a last-in, first-out data structure where items are stored in memory locations pointed to by a stack pointer. The push operation inserts an item onto the stack and increments the pointer, while the pop operation removes an item and decrements the pointer. Example applications of stacks include expression evaluation, conversion between infix, prefix and postfix notations, parsing, and simulating function calls. Algorithms for push, pop, and evaluating expressions in prefix and postfix notations are also presented.
2. Stack
Stack and its operations: PUSH, POP
Representation of the states of stack
PUSH, POP algorithms
Stack application: prefix, infix, postfix computations
3. What is Stack?
Stack is a storage structure that stores information in last in first out(LIFO)
manner. It means that the item stored at last is retrieved at first . Typically, stack is
a group of memory locations with a register that holds the address of topmost
element. This special register that points to top of stack is known as Stack
Pointer.
4. Stack Operations
Stack supports two operations:
Push:
Inserts an item on top of stack.
Causes stack to grow
Stack pointer gets increment
5. 0
1
2
3
4
5
SP
Empty
Full
A B C D E F
Stack is Full
Stack Operations
PUSH Algorithm
if !(FULL)
SP SP + 1
M[SP] DR
If (SP = 5) then
FULL 1
EMPTY 0
else
Overflow
-1
Repeat the process until SP=5
6. Pop:
Deletes an item from top of stack.
Causes stack to shrink
Stack pointer get decrement
Stack Operations
7. POP Algorithm
if !(EMPTY)
DR M[SP]
SP SP- 1
If (SP = -1) then
FULL0
EMPTY 1
else
Underflow
0
1
2
3
4
5SP
Empty
Full
Stack Operations
-1
A
B
C
D
E
F
Stack is Empty
Repeat the process until SP=-1
8. Applications of stack
1. Expression Evaluation
2. Expression conversion
Infix to Postfix
Infix to Prefix
Postfix to Infix
Prefix to Infix
3. Parsing
4. Simulation of recursion
5. Function call
9. Some examples of infix, prefix and postfix
Infix
Step by step evaluation of 3*2+5
Given 3*2+5-7
= 6+5-7
= 11-7
= 4
Prefix
-+*3257
= - + 657
= -(11)7
= 4
Postfix
32*5+7-
= 65+7-
= (11)7-
= 4
10. Prefix Notation Algorithm
Scan from left to right
If (scanned= operation)
PUSH
Else
if (scanned =1stoperand)
PUSH
Else if (scanned = 2ndoperand)
POP
POP
Evaluate
PUSH.
0
1
2
3
4
5
Empty
Full
-1
+ * 3 4 * 5 6
= 12
Repeat the process
Notation
3042
11. Postfix Notation Algorithm
Scan from left to right
If (scanned=operand)
PUSH
Else
if (scanned=operator)
POP
POP
Evaluate
PUSH
0
1
2
3
4
5
Empty
Full
-1
3 4 * 5 6 * +Notation
=
Repeat the process
123042