You are on page 1of 3

CSIS 210 Stacks Practice Problems

Problem 1) Draw the resultant stack after executing the code below:

A)

LinkedStack<Integer> myStack = new LinkedStack<>();

myStack.push(5);

myStack.push(3);

myStack.push(2);

myStack.peek();

myStack.peek();

myStack.pop();

myStack.push(myStack.pop());

myStack.push(6);

B)

ArrayStack<String> s = new ArrayStack<>();

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.

// For linked stack

public void printStack(){

// Complete the code here

// For array stack

public void printStack(){

// Complete the code here

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.

// For linked stack

public int sumStack(){

// Complete the code here

// For array stack

public int sumStack(){

// Complete the code here

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!

// Code for both linked and array stack

public LinkedStack ipStack(){

// Complete the code here

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) ((()})

C) ([{b + c} * d]q - x)}


fl
fl
fi

You might also like