ADT stack lets you peek at its top entry without removing it. For some applications of stacks, you also need to peek at the entry beneath the top entry without removing it. We will call such an...


ADT stack lets you peek at its top entry without removing it. For some applications of stacks, you
also need to peek at the entry beneath the top entry without removing it. We will call such an operation
peekNxt.If the stack has more than one entry, peekNxt returns the second entry from the top without altering the stack. If the stack has fewer than two entries, peekNxt throws an exception. Write a linked
implementation of a stack class call LinkedStack.java that includes a method peekNxt.






Please make sure that this is included in peekNxt:  If the stack has fewer than two entries, peekNxt throws an exception and if you could please make a main to execute peekNxt, thank you.
There are 2 class, 1 with the peekNxt, and 1 for implementation. You only need to work on the first one with the peekNxt in it





import java.util.EmptyStackException;
import java.util.NoSuchElementException;
public final class LinkedStack implements StackInterface
{
private Node topNode;
public YourFirst_YourLast_LinkedStack()
{
topNode = null;
}
public void push(T newEntry)
{
Node newNode = new Node(newEntry, topNode);
topNode = newNode;
} // end push
public T peek()
{
if (isEmpty())
throw new EmptyStackException();
else
return topNode.getData();
}
public T peekNxt()  // Code here
{





}
public T pop()
{
T top = peek();
topNode = topNode.getNextNode();
return top;
}
public boolean isEmpty()
{
return topNode == null;
}
public void clear()
{
topNode = null;
}
private class Node
{
private T data;
private Node next;
private Node(T dataPortion)












{
this(dataPortion, null);
}
private Node(T dataPortion, Node linkPortion)
{
data = dataPortion;
next = linkPortion;
}
private T getData()
{
return data;
}
private void setData(T newData)
{
data = newData;
}
private Node getNextNode()
{
return next;
}
private void setNextNode(Node nextNode)
{
next = nextNode;
}
}
}
--------------------------------------
Supplemental class


public interface StackInterface
{
/** Adds a new entry to the top of this stack.
@param newEntry An object to be added to the stack. */
public void push(T newEntry);


/** Removes and returns this stack's top entry.
@return The object at the top of the stack.
@throws EmptyStackException if the stack is empty before the operation. */
public T pop();


/** Retrieves this stack's top entry.
@return The object at the top of the stack.
@throws EmptyStackException if the stack is empty. */
public T peek();
/** Retrieves the entry just below this stack's top entry.
@return The object just below the top of the stack.
@throws EmptyStackException if the stack is empty.
@throws NoSuchElementException if the stack contains only one entry.
*/
public T peekNxt();
/** Detects whether this stack is empty.
@return True if the stack is empty. */
public boolean isEmpty();
/** Removes all entries from this stack. */
public void clear();
}




Jun 11, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here