Question: 1. (8 pts.) The following stack stores a stack of integers. Draw the stacks and determine what is the output of the following program?
1. (8 pts.) The following stack stores a stack of integers. Draw the stacks and determine what is the output of the following program? import java.util.*; public class TestStack1 { public static void main(String... args) Output: { } Stack s = new Stack (): s.push(-54): s.push(27); s.push(31); System.out.println(s.pop()); Integer v = s.pop()+ s.pop() % 3; s.push(v): s.push(21): int w = s.pop(); int x = s.pop(): s.push(x % w-v); System.out.println(s.pop()); Integer a=2, b=5, c=22, d=4; s.push(a); s.push(b); s.push(c): s.push(d): s.push(s.pop()+s.pop()); s.push(s.pop()*s.pop()): while(!s.empty()) {System.out.println(s.pop());}
Step by Step Solution
3.42 Rating (158 Votes )
There are 3 Steps involved in it
Answer Lets visualize the stack operations step by step Initial stack markdown 31 27 54 Pop the top ... View full answer
Get step-by-step solutions from verified subject matter experts
