Assignments
1. Given the stack of integers {7,5} , what does the stack look like after a call
to "push" 8? Assume the left-most element is the top element of the stack.
2. Write the algorithm to push an element into a stack.
3. The stack of integers {41,8} undergoes the following operations, in the
following order: "pop", "push 2", "push 15", "pop". What does the stack look like
after all of these operations have been performed? Assume the left-most element is
the top element of the stack.
4. The stack of integers {2,9,5,8,1,3} is "popped" twice. What is the value
returned by the second "pop" operation? Assume the left-most element is the top
element of the stack. Consider a stack of characters, where STACK is allocated N=8
memory cells:
STACK: A, C, D, F, K, __, __, __
Describe the Stack when following operations take place.
Pop (Stack, value)
Pop (Stack, value)
Push (Stack, L)
Push (Stack, P)
Pop (Stack, value)
Push (Stack, R)
Push (Stack, S)
Pop (Stack, value)
5. Suppose STACK is allocated N=6 memory cells and initially stack is empty,
or in other words TOP=0. Find the output of following module.
Set AAA: =2 and BBB: =5
Call PUSH (STACK, AAA).
Call PUSH(STACK,4).
Call PUSH (STACK, BBB+2).
Call PUSH(STACK,9).
Call PUSH (STACK, AAA+BBB).
Repeat while TOP ¹ 0
Call POP (STACK, value).
Write: value.
Return
6. Consider the following stack, where STACK is allocated N=6 memory cells:
STACK: AAA, DDD, EEE, FFF, GGG, __
Describe the stack as the following operations take place:
Push (Stack, KKK)
Pop (Stack, item)
Push (Stack, LLL)
Push (Stack, SSS)
Pop (Stack, item)
Push (Stack, TTT)
7. What are the operations of the stack?
8. Write the algorithm to pop an element from a stack.
9. What are push and pop operations?
10. A letter means push and an asterisk means pop in the following sequence.
Give the sequence of values returned by the pop operations when this sequence of
operations is performed on an initially empty LIFO stack.
EAS*Y*QUE***ST***IO*N***
A*STI*N*FIR*ST**OU*T******
11. Use a stack to evaluate following the postfix expressions. Write the state of
the stack after each token (i.e., an operator or operand) is processed.
12+34*-9/
261-*84/-3*
12. Use two stacks (one for operands and one for operators) to evaluate the fully
parenthesized following infix expressions. Write the state of both the operand and
operator stacks after each token (i.e., operator, operand, or a single parenthesis)
which results in at least one of the stacks changing is processed. To what value
does the expression evaluate?
((9-6)+((2+3)/5))
((5*(1+(4-2)))/(6/2))