Question: Add the following methods to the LinkedStack class, and create a test driver for each to show that they work correctly. String toString() creates and

Add the following methods to the LinkedStack class, and create a test driver for each to show that they work correctly.

String toString()creates and returns a string that correctly represents the current stack. Such a method could prove useful for testing and debugging the class and for testing and debugging applications that use the class. Assume each stacked element already provided its own reasonable toString method.

int size()returns a count of how many items are currently on the stack. Do not add any instance variables to the ArrayBoundedStack class in order to implement this method.

void popSome(int count)removes the top count elements from the stack; throws StackUnderflowException if there are less than count elements on the stack.

boolean swapStart()if there are less than two elements on the stack returns false; otherwise it reverses the order of the top two elements on the stack and returns true.

T poptop( )the classic pop operation, if the stack is empty it throws StackUnderflowException; otherwise it both removes and returns the top element of the stack.

Added methods NEED to extend LinkedStack, not added to the actual class. Test Driver to show LinkedExtended methods work correctly.

------------------------------------------------------------------------------------------------------------------------

LinkedStack.java

// Implements StackInterface using a linked list to hold the elements. //-----------------------------------------------------------------------

package ch02.stacks; import support.LLNode;

public class LinkedStack implements StackInterface { protected LLNode top; // reference to the top of this stack

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

}

import ch02.stacks.*;

public class LinkedExtended extends LinkedStack { //Add methods a through e into this class }

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!