Write a method expunge that accepts a stack of integers as a parameter and makes sure that the stack's elements are in descending order from top to bottom, by removing from the stack any element that is larger than any element(s) on top of it. For example, suppose a variable s stores the following elements: bottom [4, 20, 15, 15, 8, 5, 7, 12, 8, 10, 9, 9, 16] top The element values 10, 12, 5, 8, 15, 15, and 20 should be removed because each has an element above it with a smaller value. So the call of expunge(s); should change the stack to store the following elements in this order: bottom [4, 5, 7, 8, 9, 9, 16] top

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Write a method expunge that accepts a stack of integers as a parameter and makes sure that the stack's elements are in descending order from top to bottom, by removing from the stack any element that is larger than any element(s) on top of it. For example, suppose a variable s stores the following elements:

bottom [4, 20, 15, 15, 8, 5, 7, 12, 8, 10, 9, 9, 16] top

The element values 10, 12, 5, 8, 15, 15, and 20 should be removed because each has an element above it with a smaller value.

So the call of expunge(s); should change the stack to store the following elements in this order:

bottom [4, 5, 7, 8, 9, 9, 16] top

Notice that now the elements are in non-increasing order from top to bottom. If the stack is empty or has just one element, nothing changes. You may assume that the stack passed is not null.

Problem Requirements

  • You should solve this problem using one auxiliary Stack. You may not use other structures (arrays, lists, etc.), but you can have as many simple variables as you like.

  • You should solve this problem using the Stack/Queue methods discussed in class and use them in stack/queue like ways. You should use interface types when appropriate. See below for a list of appropriate methods.

  • The method you write should work with the provided main as written, as this is showing how we expect your method to run in our tests. You may want to edit main to test whether your code works for all expected cases, but make sure you do not change how the method is called.

  • explicitly permitted Stack/Queue methods:

    Stack

    • push

    • pop

    • peek

    • size

    • isEmpty

    Queue

    • add

    • remove

    • peek

    • size

    • isEmpty

    This is the code: 

    import java.util.*;

    public class StackQueueQuestion {
        public static void main(String[] args) {
            Stack<Integer> s = new Stack<>();
            // [4, 20, 15, 15, 8, 5, 7, 12, 8, 10, 9, 9 16]
            s.push(4);
            s.push(20);
            s.push(15);
            s.push(15);
            s.push(8);
            s.push(5);
            s.push(7);
            s.push(12);
            s.push(8);
            s.push(10);
            s.push(9);
            s.push(9);
            s.push(16);

            System.out.println("s before = " + s);
            expunge(s);
            System.out.println("s after  = " + s);
        }

        // TODO write your code here
    }

     

    screeshot the output and the entire code plz once ur finished.

Expert Solution
Step 1

Java:

Java is a general purpose, high level, class based object oriented programming language.

It is simple and secure.

It is robust.

It supports multithreading.

 

trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 4 images

Blurred answer
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

thank you, but when the stack is empty, I dont wanna use return in the public static void. what else can I write 

Solution
Bartleby Expert
SEE SOLUTION
Knowledge Booster
Threads in linked list
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
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education