0
/ 1.0
For the question below, assume the following implementation of LinkedStack:
public class LinkedStack<T> implements StackInterface...
For the question below, assume the following implementation of LinkedStack:
public class LinkedStack<T> implements StackInterface<T> {
private Node<T> topNode;
private int numberOfEntries;
public LinkedStack() {
this.topNode = null;
numberOfEntries = 0;
}// end default constructor
@Override
public void push(T newEntry) {
topNode = new Node<T>(newEntry, topNode);
numberOfEntries++;
}
@Override
public T peek() {
if (isEmpty()) {
throw new EmptyStackException();
}
else {
return (T)topNode.getData();
}
}// end peek
@Override
public T pop() {
T top = peek();
topNode = topNode.getNext();
numberOfEntries--;
return top;
}
@Override
public boolean isEmpty() {
return topNode == null;
}
@Override
public void clear() {
topNode = null;
}
} // end LinkedStack
public class Node<E> {
private E data; // Entry in bag
private Node<E> next; // Link to next node
public Node(E dataPortion) {
this(dataPortion, null);
} // end constructor
public Node(E dataPortion, Node<E> nextNode) {
data = dataPortion;
next = nextNode;
} // end constructor
public E getData() {
if (data != null) {
return data;
}
return null;
}
public Node<E> getNext() {
return next;
}
public void setNext(Node<E> newNext) {
next = newNext;
}
} // end Node
Below, you'll see most of a toString method implemented but a piece is missing. Currently the while loop will run infinitely. Add in the code to finish this method and the while loop will terminate when it is supposed to.
Your feedback will appear here when you check your answer.