Question: **Use the Stack interface to implement the LinkedStack.java class. Stack.java public interface Stack { /** * Push an element onto the stack. * * @param
**Use the Stack interface to implement the LinkedStack.java class.
Stack.java
public interface Stack
***In LinkedStack.java, change only The methods with empty bodies Do not use a sentinel node. In this implementation you use an inner class That is, a class that is declared inside of another class. The Node class isn't needed by any other class, so it is declared as a private class inside the LinkedStack class. The Node class has two fields: element and next. Things to note in the class LinkedStack: The EmptyStackExceptionis being used, same as in the ArrayStack. The class StringJoineris imported for use in the toString()
LinkedStack.java
import java.util.EmptyStackException; import java.util.StringJoiner; public class LinkedStack
@Override public String toString() { StringJoiner joiner = new StringJoiner(", ", "[", "]"); for (Node current = top; current != null; current = current.next) { joiner.add(current.element.toString()); } return joiner.toString(); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
