This was a homework assignment given to me and I got the answer (listed later), but I was wondering if anyone could check it over and see if i'm correct:
a)
My result is://Assume that you can store and retrieve variables of type int on stack item1 = 1; item2 = 0; item3 = 4; stack.push(item2); // push puts one item on top of stack stack.push(item1); stack.push(item1 + item3); item2 = stack.top; //retrieves node info at top of stack stack.push(item3*item3); stack.push(item2); stack.push(3); item1 = stack.top(); stack.pop(); // pop() removes top item from stack System.out.println(item1 + " " + item2 + " " + item3); while(!stack.isEmpty()){ item1 = stack.top(); stack.pop(); System.out.println(item1); }
3 5 4
5
16
5
1
0
b)
My result is:item1 = 4; item3 = 0; item2 = item1+1; stack.push(item2); stack.push(item2+1); stack.push(item1); item2 = stack.top(); stack.pop(); item1 = item2+1; stack.push(item1); stack.push(item3); while(!stack.isEmpty()){ item3 = stack.top(); stack.pop(); System.out.println(item3); } System.out.println(item1 + " " + item2 + " " + item3);
0
5
6
5
5 4 5