Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Application #3 Skeleton of LinkedStack class is provided. The class includes mai

ID: 3753077 • Letter: A

Question

Application #3

Skeleton of LinkedStack class is provided. The class includes main with test cases to test your methods.

SAMPLE RUN

*** Create a stack ***

--> Add to stack to get: Joe Jane Jill Jess Jim

Done adding 5 elements.

--> Testing peek, peek2, and pop:

Joe is at the top of the stack.

Jane is just beneath the top of the stack.

Joe is removed from the stack.

Jane is at the top of the stack.

Jill is just beneath the top of the stack.

Jane is removed from the stack.

Jill is at the top of the stack.

Jess is just beneath the top of the stack.

Jill is removed from the stack.

Jess is at the top of the stack.

Jim is just beneath the top of the stack.

Jess is removed from the stack.

Jim is at the top of the stack.

CORRECT - exception has been thrown: cannot complete peek2() - only one element on the stack

Jim is removed from the stack.

--> The stack should be empty:

isEmpty() returns true

CORRECT - exception has been thrown: cannot complete peek() - stack is empty

CORRECT - exception has been thrown: cannot complete pop() - stack is empty

CORRECT - exception has been thrown: cannot complete peek2() - stack is empty

*** Done ***

Explanation / Answer


Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you



public final class LinkedStack<T> implements TextbookStackInterface<T>
{
private Node<T> topNode; // references the first node in the chain
public LinkedStack()
{
this.topNode = null;
} // end default constructor
public void push(T newEntry)
{
topNode = new Node(newEntry, topNode);
} // end push
public T peek() throws InsufficientNumberOfElementsOnStackException
{
if(isEmpty())
throw new InsufficientNumberOfElementsOnStackException("Stack empty");
return topNode.data;
} // end peek
public T peek2() throws InsufficientNumberOfElementsOnStackException
{
if(isEmpty() || topNode.next == null)
throw new InsufficientNumberOfElementsOnStackException("Stack empty");
return topNode.next.data;
} // end peek2
public T pop() throws InsufficientNumberOfElementsOnStackException
{
if(isEmpty())
throw new InsufficientNumberOfElementsOnStackException("Stack empty");
T val= topNode.data;
topNode = topNode.next;
return val;
} // end pop
public boolean isEmpty()
{
return topNode == null;
} // end isEmpty
public void clear()
{
topNode = null;
} // end clear
// These methods are only for testing of array implementation
public int getTopIndex()
{
return 0;
}
public int getCapacity() { return 0; }
private class Node<S>
{
private S data; // Entry in stack
private Node<S> next; // Link to next node
private Node(S dataPortion)
{
this(dataPortion, null);
} // end constructor
private Node(S dataPortion, Node<S> linkPortion)
{
this.data = dataPortion;
this.next = linkPortion;
} // end constructor
} // end Node
public static void main(String[] args)
{
System.out.println("*** Create a stack ***");
LinkedStack<String> myStack = new LinkedStack<>();
System.out.println("--> Add to stack to get: " +
"Joe Jane Jill Jess Jim ");
myStack.push("Jim");
myStack.push("Jess");
myStack.push("Jill");
myStack.push("Jane");
myStack.push("Joe");
System.out.println("Done adding 5 elements. ");
System.out.println("--> Testing peek, peek2, and pop:");
while (!myStack.isEmpty())
{
String top = myStack.peek();
System.out.println(top + " is at the top of the stack.");
try
{
String beneathTop = myStack.peek2();
System.out.println(beneathTop + " is just beneath the top of the stack.");
} catch (InsufficientNumberOfElementsOnStackException inoeose)
{
System.out.println(" CORRECT - exception has been thrown: " + inoeose.getMessage());
}
top = myStack.pop();
System.out.println(top + " is removed from the stack. ");
} // end while
System.out.println("--> The stack should be empty: ");
System.out.println("isEmpty() returns " + myStack.isEmpty());
try
{
String top = myStack.peek();
System.out.println(top + " is at the top of the stack.");
} catch (InsufficientNumberOfElementsOnStackException inoeose)
{
System.out.println(" CORRECT - exception has been thrown: " + inoeose.getMessage());
}
try
{
String top = myStack.pop();
System.out.println(top + " is at the top of the stack.");
} catch (InsufficientNumberOfElementsOnStackException inoeose)
{
System.out.println(" CORRECT - exception has been thrown: " + inoeose.getMessage());
}
try
{
String beneathTop = myStack.peek2();
System.out.println(beneathTop + " is just beneath the top of the stack.");
} catch (InsufficientNumberOfElementsOnStackException inoeose)
{
System.out.println(" CORRECT - exception has been thrown: " + inoeose.getMessage());
}
System.out.println("*** Done ***");
} // end main
} // end LinkedStack

output
-----
*** Create a stack ***
--> Add to stack to get: Joe Jane Jill Jess Jim
Done adding 5 elements.
--> Testing peek, peek2, and pop:
Joe is at the top of the stack.
Jane is just beneath the top of the stack.
Joe is removed from the stack.
Jane is at the top of the stack.
Jill is just beneath the top of the stack.
Jane is removed from the stack.
Jill is at the top of the stack.
Jess is just beneath the top of the stack.
Jill is removed from the stack.
Jess is at the top of the stack.
Jim is just beneath the top of the stack.
Jess is removed from the stack.
Jim is at the top of the stack.
CORRECT - exception has been thrown: Stack empty
Jim is removed from the stack.
--> The stack should be empty:
isEmpty() returns true
CORRECT - exception has been thrown: Stack empty
CORRECT - exception has been thrown: Stack empty
CORRECT - exception has been thrown: Stack empty
*** Done ***

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote