.) 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)   {             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( ));}    } }

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 3PE
icon
Related questions
Question
  1. .) 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)

  {

 

          Stack<Integer> 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( ));}

   }

}

 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 5 steps with 2 images

Blurred answer
Knowledge Booster
Stack
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning