Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Add a new method called moveSmallestToTop to it. This method will determine which value in the stack is the smallest and move that value to

Add a new method called moveSmallestToTop to it. This method will determine which value in the stack is the smallest and move that value to the top of the stack while otherwise leaving the remainder of the stack in its original order. This java code returns nothing. For example, suppose myStack contains the integer values (bottom 4 2 8 3 6 5 top), then a call to moveSmallestToTop (stack1) would leave myStack as follows: (bottom 4 8 3 6 5 2 top). In your solution, you may only declare extra stack called tempStack, which is a Java Collections Framework Class Stack, or scalar variables (e.g., ints, - not arrays or linked lists allowed). Write a main method that tests all the methods. ______________________ The interface for myStack is given below: pubic interface stackInterface{ public boolean isEmpty();// return true if stack is empty, false otherwise. public void popAll();//remove all items from the stack. public void push(Object newItem); //Add newItem to the top of the stack //You can assume that any stack (myStack) you use will never become full so you need //not to throw an exception and that the provided stack will not contain duplicate //values. public object pop() throws StackException;//removes the top of a stack //if the stack is empty, throw StackException. public object peek() throws StackException;// retrieves the top of a stack //if the stack is empty, throw StackException. public static void moveSmallestToTop(StackReferenceBased myStack); }//end StackInterface The implementation of the LinkedList based is given below: Public class stackReferenceBased implements StackInterfac else { throw new StackException(StackException on +peek: stack empty); }//end if }//end peek }//end StackReferenceBased public static void moveSmallestToTop(StackReferenceBased myStack){ if(myStack.isEmpty()){ Stack tempStack=new Stack(); //Add your code here } } You also need the StackException class as below: public class stackException extends java.lang.RuntimeException{ public StackException(String s){ super(s); }//end constructor }//end StackException Node class: public class Node { private T item; private Node next; public Node(){ item=null; next=null; }

public Node(T newItem){ item=newItem; next=null; }//end of constructor

public Node(T newItem, Node nextNode){ item=newItem; next=nextNode; }//end of constructor

public void setItem(T newItem){ item=newItem; }

public void setNext(Node newNode){ next=newNode; }

public T getItem(){ return item; } public Node getNext(){ return next; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle 11G SQL

Authors: Joan Casteel

2nd Edition

1133947360, 978-1133947363

More Books