Unit4 Stack
Unit4 Stack
Applications of queue-BFS
Definition
Subtraction
A stack is an ordered collection of items into which new items
may inserted and item may be deleted at one end called TOP of
the stack.
A stack is a homogeneous collection of items of any one type.
Data can be added or removed from only the top.
2. Pop :Return the item at the top of the stack and then remove
it. If pop is called when stack is empty, it is in an
underflow state.
3. Top: Access the item at the top of the stack, but do not
remove it.
Top is an integer value, which contains the array index for the top of
the stack.
factorial(n) = n * factorial(n-1)
factorial(5)
= 5 * factorial(4)
= 5 * (4 * factorial(3))
= 5 * (4 * (3 * factorial(2)))
= 5 * (4 * (3 * (2 * factorial(1))))
= 5 * (4 * (3 * (2 * 1)))
= 5 * (4 * (3 * 2))
= 5 * (4 * 6)
= 5 * 24
= 120
int factorial(5) int factorial(4) int factorial(3) int factorial(2) int factorial(1)
{ { { { {
} } } }
1
return address to 2
1*1=1
2
return address to 3
2*1=2
3
return address to 4
3*2=6
4
return address to 5
4*6=24
5
return address to main 5*24=120
In recursive function,
1. Save parameters, local variables, and return address on
stack.
2. If the base criteria(termination condition) has been
reached, perform computation and go to step 3.
Otherwise perform partial computation and go to step
1.
3. Restore most recently saved parameters, local variables,
and return address from stack. Go to this return
address.
Infix to postfix conversion
While no error occurs and end of infix expression has not been reached, do:
a) Get next input Token (constant, variable, arithmetic operator, left and
right parenthesis) from the infix expression.
b) If Token is
A left parenthesis: Push onto stack
c) On end of infix expression, pop and display stack item until stack is empty.
Operator precedence
* / . Left to right
(A+B)*C-D*E+F : AB+C*DE*-F+
(A-2*(B+C)-D*E)*F : A2BC+*-DE*-F*
Create an empty stack that will contain operands.
Take one by one token from the left to right.
If token is an operand push it onto the stack.
If token is an operator op
Pop the top item from the stack as operand2.
Pop again the top item from the stack as operand1.
Perform operation operand1 op operand2.
Push result back to stack.
When all tokens in input expression are processed stack
should contain a single item, which is the value of expression.
Evaluate: 7 5 - 4 *
7 Push 7 7
5 Push 5 75
- Pop 5 as second operand, pop 2
7 as first operand, 7-5 is 2.
Push 2.
4 Push 4 24
* Pop 4 as second operand, pop 8
2 as first operand, 2*4 is 8.
Push 8.
Determine value of following postfix expressions
when A =2, B=3, C=4, D=5, E=6, F=7
AB*C-D+
ABC+*D-
AB+C*DE*-F+
1. 7
2. 9
Initial State
Final State
1. if (n=1)
1. writeln('Move the plate from ', source, ' to ', dest)
2. else
1. Hanoi(n-1, source, aux, dest)
2. Hanoi(1, source, dest, aux)
3. Hanoi(n-1, aux, dest, source)
Implementing Stacks: Array
Advantages
best performance
Disadvantage
fixed size
Basic implementation
initially empty array
field to record where the next data gets placed into
if array is full, push() returns false
otherwise adds it into the correct spot
if array is empty, pop() returns null
otherwise removes the next item in the stack
Questions: