0% found this document useful (0 votes)
7 views

Stack Problems

The document contains 6 practice problems related to stacks. Problem 1 asks to draw the resultant stack after executing some code on linked and array stacks. Problem 2 asks to write code to initialize a stack with given data. Problems 3-5 ask to add new methods to print, sum, and flip the elements in linked and array stack classes. Problem 6 asks to use a checkBalance algorithm to determine if equations containing parentheses, brackets and braces are balanced and show the resultant stack.

Uploaded by

ghalia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Stack Problems

The document contains 6 practice problems related to stacks. Problem 1 asks to draw the resultant stack after executing some code on linked and array stacks. Problem 2 asks to write code to initialize a stack with given data. Problems 3-5 ask to add new methods to print, sum, and flip the elements in linked and array stack classes. Problem 6 asks to use a checkBalance algorithm to determine if equations containing parentheses, brackets and braces are balanced and show the resultant stack.

Uploaded by

ghalia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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