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

Asig Stacks

The document contains 5 questions about stacks and the order objects would be pushed and popped from stacks. It provides the expected contents of stacks after sequences of push and pop operations. The last question evaluates a factorial function using a stack to calculate 4!.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
125 views

Asig Stacks

The document contains 5 questions about stacks and the order objects would be pushed and popped from stacks. It provides the expected contents of stacks after sequences of push and pop operations. The last question evaluates a factorial function using a stack to calculate 4!.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Korallys Rodríguez Ferbuary 6, 2020.

COMP 2900 Prof. Donato

Title: STACKS HOMEWORK

1. If you push the objects x, y, and z onto an initially empty stack, in what order will three
consecutive pop operations remove them from the stack?
 The stacks would look like: Z, Y, and X.

2. What pseudocode statements create a stack of the three strings “Carlos”, “Darius”, and
“Sophia”, in that order with “Carlos” at the top?
 myStack.push(“Sophia”);
 myStack.push(“Darius”);
 myStack.push(“Carlos”);

3. Suppose that s and t are empty stacks and a, b, c, and d are objects. What do the stacks contain
after the following sequence of operation executes?

s.push(a); S: a T:
s.push(b); S: a b T:
s.push(c); S: a b c T:
t.push(d); S: a b c T: d
t.push(s.pop()); S: a b T: d c
t.push(s.peek()); S: a b T: d c b
s.push(t.pop()); S: a b b T: d c
t.pop(); S: a b b T: d

4. What are the contents of the stack pile after the following statements execute? Assume that
MyStack is a class that implements the interface StackInterface.

Pile.push(“Jazmin”);
Pile.push(“Jess”);
Pile.push(“Jack”);
Pile.push(pile.pop()):
Pile.push(pile.peek());
Pile.push(“Seiji”);
String name = pile.pop();
Pile.push(pile.peek());

 Jack, Jack, Jack, Jess, and Jazmin.


5. Consider the following Java statements, assuming that MyStack is a class that implements the
interface StackInterface.

a. What value is displayed when this code executes?


 The value that it displays when it executes is 24.

b. What mathematical function does the code evaluate?


 The mathematical function is factorial.
4! = 1 * 2 * 3 * 4 = 24.

You might also like