Professional Documents
Culture Documents
Stack Problems
Stack Problems
Problem 1) Draw the resultant stack after executing the code below:
A)
myStack.push(5);
myStack.push(3);
myStack.push(2);
myStack.peek();
myStack.peek();
myStack.pop();
myStack.push(myStack.pop());
myStack.push(6);
B)
myStack.push(5);
myStack.push(3);
myStack.push(2);
s.clear();
s.push(7);
s.push(1);
s.push(3);
s.pop();
s.push(s.peek());
Problem 2) Assuming that an empty stack called “myStack” already exists, write code
to make the stack have the same data as shown below:
A)
-8
9
3
5
B)
Haneen
Yousef
Omar
Problem 3) Add a new method to the linked stack class and the array stack class that
prints all the elements in the stack. You are free to remove all elements if you need to.
Problem 4) Add a new method to the linked stack class and the array stack class that
sums all the elements in the stack. You are free to remove all elements if you need to.
Problem 5) Add a new method to the linked stack class and the array stack class that
returns the original stack ipped. For example, if the stack has 2 —> 5 —> 9, then you
should return 9 —> 5 —> 2. The code needs to work for both array and linked stack!
Problem 6) Given the following equations, run the checkBalance algorithm and con rm
if the equation is balanced. Show all your steps. Also show the resultant stack once the
algorithm terminates.
A) (a + b ( -c * q) {f * z[W + M]})
B) ((()})