Question
java code implement/ ......................................... /** * A class of a stack whose entries are stored in a chain of nodes. * * @author YOUR NAME
java code implement/
.........................................
/**
* A class of a stack whose entries are stored in a chain of nodes.
*
* @author YOUR NAME
* @version 02/23/2021
*/
public class ChainOfNodesAndStack
{
private Node
public ChainOfNodesAndStack()
{
// TODO PROJECT #1
} // end default constructor
public void push(T newEntry)
{
// TODO PROJECT #1
} // end push
public T peek() throws InsufficientNumberOfElementsOnStackException
{
// TODO PROJECT #1
return null; // THIS IS A STUB
} // end peek
public T pop() throws InsufficientNumberOfElementsOnStackException
{
// TODO PROJECT #1
return null; // THIS IS A STUB
} // end pop
public boolean isEmpty()
{
// TODO PROJECT #1
return false; // THIS IS A STUB
} // end isEmpty
public void clear()
{
// TODO PROJECT #1
} // end clear
public int remove(int numberOfElements)
{
// TODO PROJECT #1
return 0; // THIS IS A STUB
} // end remove
public void displayStack()
{
// TODO PROJECT #1
} // end displayStack
private class Node
{
private S data; // Entry in stack
private Node next; // Link to next node
private Node(S dataPortion)
{
this(dataPortion, null);
} // end constructor
private Node(S dataPortion, Node linkPortion)
{
this.data = dataPortion;
this.next = linkPortion;
} // end constructor
} // end Node
public static void main(String[] args)
{
System.out.println("*** Create a stack ***");
ChainOfNodesAndStack
System.out.println("--> Pushing A B C D E on the stack");
myStack.push("A");
myStack.push("B");
myStack.push("C");
myStack.push("D");
myStack.push("E");
System.out.println("Done adding 5 elements. ");
myStack.displayStack();
// TODO PROJECT #1 - uncomment the remaining lines when peek and pop are implemented
// System.out.println(" --> Testing peek, pop, isEmpty:");
//
// while (!myStack.isEmpty())
// {
// String top = myStack.peek();
// System.out.println(top + " is at the top of the stack.");
// 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());
// }
// myStack.displayStack();
//
// System.out.println(" --> Testing clear:");
//
// System.out.println("--> Pushing A B C D E F G on the stack");
// myStack.push("A");
// myStack.push("B");
// myStack.push("C");
// myStack.push("D");
// myStack.push("E");
// myStack.push("F");
// myStack.push("G");
// System.out.println("Done adding 7 elements. ");
// myStack.displayStack();
// System.out.println("--> Calling clear()");
// myStack.clear();
// myStack.displayStack();
//
// System.out.println(" --> Testing remove:");
// System.out.println("--> Calling remove(4) on empty stack");
// System.out.println(myStack.remove(4) + " elements have been removed.");
// myStack.displayStack();
//
// System.out.println("--> Pushing A B C D E F G H I J on the stack");
// myStack.push("A");
// myStack.push("B");
// myStack.push("C");
// myStack.push("D");
// myStack.push("E");
// myStack.push("F");
// myStack.push("G");
// myStack.push("H");
// myStack.push("I");
// myStack.push("J");
// System.out.println("Done adding 10 elements. ");
// myStack.displayStack();
//
// System.out.println("--> Calling remove(4)");
// System.out.println(myStack.remove(4) + " elements have been removed.");
// myStack.displayStack();
//
// System.out.println("--> Calling remove(10)");
// System.out.println(myStack.remove(10) + " elements have been removed.");
// myStack.displayStack();
System.out.println("*** Done ***");
} // end main
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started