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

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 16PE: The implementation of a queue in an array, as given in this chapter, uses the variable count to...
icon
Related questions
Question

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

Implement p
import java.util.EmptyStackException;
import java.util.NoSuchElementException;
public final class LinkedStack<T> implements StackInterface<T>
{
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();
} // end peek
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;
} // end getData
private void setData(T newData)
{
data = newData;

private Node getNextNode()
{
return next;

private void setNextNode(Node nextNode)
{
next = nextNode;
}


--------------------------------------
public interface StackInterface<T>
{
/** 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();
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Threads in linked list
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning