Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You are given the NewStackInterface interface with some additional abstract methods. The NewLinkedStack class implements this NewStackInterface, and hence you must implement the additional methods

You are given the NewStackInterface interface with some additional abstract methods. The NewLinkedStack class implements this NewStackInterface, and hence you must implement the additional methods in the NewLinkedStack class (indicated by TODO comments). The method descriptions are given below - 1. void popFromBottom() This method removes the element from the bottom of the stack; throws StackUnderflowException if the stack is empty. 2. T bottom() This method returns the element at the bottom of the stack; throws StackUnderflowException if the stack is empty. 3. String toString() This method creates and returns a nicely formatted String representation of the object. Such a method could prove useful for testing and debugging the class and for testing and debugging applications that use the class. 4. int size() This method returns a count of how many elements are currently on the stack. Using the NewLinkedStack class, create an application that finds the largest integer value in the stack. Use the following approach: Prompt the user for the number of elements in a stack. Fill the stack with randomly generated integers (0-999). The topmost element is compared with the bottommost element of the stack. If the top element is greater than the bottom element, then the bottom element is removed from the stack. If the bottom element is greater than the top element, then the top element is removed from the stack. If both the elements are of same value, then either top or bottom element (your choice) is removed from the stack. The process is continued until we are left with the largest element in the stack. Display the result

*DO NOT make any changes to the given method headers, i.e., method return type, method name, and method parameters. *DO NOT add any new instance variables in the NewLinkedStack class (use only one NewLinkedStack object in the driver program) *DO NOT create any copy or temporary stack *DO NOT use any data structure other than NewLinkedStack. ie, no arrays, ArrayList, Stack, List, Node, etc.

Example: image text in transcribed

Currently, I have the code written below. But it doesn't yet include an option to prompt users for the stack number and doesn't fill it with random variables; it needs a main driver code

```java

package SP23; import java.util.Random;

public class NewLinkedStack implements NewStackInterface { protected LLNode top; // reference to the top of this stack

public NewLinkedStack() { top = null; }

public void push(T element) // Places element at the top of this stack. { LLNode newNode = new LLNode(element); newNode.setLink(top); top = newNode; }

public void pop() // Throws StackUnderflowException if this stack is empty, // otherwise removes top element from this stack. { if (isEmpty()) throw new StackUnderflowException("Pop attempted on an empty stack."); else top = top.getLink(); }

public T top() // Throws StackUnderflowException if this stack is empty, // otherwise returns top element of this stack. { if (isEmpty()) throw new StackUnderflowException("Top attempted on an empty stack."); else return top.getInfo(); }

public boolean isEmpty() // Returns true if this stack is empty, otherwise returns false. { return (top == null); }

public boolean isFull() // Returns false - a linked stack is never full { return false; } @Override public void popFromBottom() throws StackUnderflowException { // Throws StackUnderflowException if this stack is empty. if (isEmpty()) { throw new StackUnderflowException("PopFromBottom attempted on an empty stack."); } // If there is only one element in the stack, simply remove it if (top.getLink() == null) { top = null; throw new StackUnderflowException("PopFromBottom attempted on a stack with only one element."); } else { // Traverse the stack to the second-to-last element LLNode secondToLast = top; while (secondToLast.getLink().getLink() != null) { secondToLast = secondToLast.getLink(); } // Remove the last element from the stack secondToLast.setLink(null); } } @Override public T bottom() throws StackUnderflowException { // Throws StackUnderflowException if this stack is empty. if (isEmpty()) { throw new StackUnderflowException("Bottom attempted on an empty stack."); } // Traverse the stack to the bottom element LLNode bottomNode = top; while (bottomNode.getLink() != null) { bottomNode = bottomNode.getLink(); } // Return the bottom element return bottomNode.getInfo(); } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("Top --> "); LLNode current = top; // Traverse the stack and append each element to the StringBuilder while (current != null) { sb.append(current.getInfo()).append(" "); current = current.getLink(); } // Return the formatted string representation of the stack return sb.toString(); } @Override public int size() { int count = 0; LLNode current = top; // Traverse the stack and count each element while (current != null) { count++; current = current.getLink(); } // Return the count of elements in the stack return count; } }

```

How many elements do you want in the stack? 5 Top>945702249852344 Largest number:945

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

Database Administration The Complete Guide To Dba Practices And Procedures

Authors: Craig S. Mullins

2nd Edition

0321822943, 978-0321822949

More Books

Students also viewed these Databases questions

Question

Define and explain the goals of employee orientation/onboarding

Answered: 1 week ago

Question

2. Do you find change a. invigorating? b. stressful? _______

Answered: 1 week ago