Question: JAVA | Using the NewLinkedStack class, create an application that finds the largest integer value in the stack. Use the following approach: Prompt the user

JAVA | 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. 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

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 { // TODO Auto-generated method stub

} @Override public T bottom() throws StackUnderflowException { // TODO Auto-generated method stub return null; } @Override public String toString() { // TODO Auto-generated method stub return null; } @Override public int size() { // TODO Auto-generated method stub return 0; }

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!