JAVA /** A class of stacks whose entries are stored in a chain of nodes. Implement all methods in MyLinkedStack class using the inner StackNode
JAVA
/** A class of stacks whose entries are stored in a chain of nodes. Implement all methods in MyLinkedStack class using the inner StackNode class.
Main Reference : text book or class notes Do not change or add data fields Do not add new methods You may access nodes' fields directly, i.e. data and next */
package PJ2;
public class MyLinkedStack
public T peek() { // add stataments return null; } // end peek
public T pop() { // add stataments return null; } // end pop
public int size() { // add stataments return 0; } // end size
public boolean empty() { // add stataments return false; } // end empty public void clear() { // add stataments } // end clear
public String toString() { // add stataments // note: data class in stack must implement toString() method // return a list of data in Stack, separate them with ',' return null; }
/**************************************************** private inner node class Do not modify the class!! you may access data and next directly ***************************************************/
private class StackNode { private T data; // entry in list private StackNode next; // link to next node private StackNode (T dataPortion) { data = dataPortion; next = null; // set next to NULL } // end constructor
private StackNode (T dataPortion, StackNode nextStackNode) { data = dataPortion; next = nextStackNode; // set next to refer to nextStackNode } // end constructor } // end StackNode
/**************************************************** Do not modify: Stack test ****************************************************/ public static void main (String args[]) {
System.out.println(" "+ "******************************************************* "+ "Sample Expected output: "+ " "+ "OK: stack is empty "+ "Push 3 data: 10, 30, 50 "+ "Print stack [50,30,10,] "+ "OK: stack size is 3 "+ "OK: peek stack top is 50 "+ "OK: stack is not empty "+ "Pop 2 data: 50, 30 "+ "Print stack [30,10,] "+ "Print stack [10,] "+ "OK: stack pop data is 30 "+ "Clear stack "+ "Print stack [] "+ " "+ "*******************************************************");
System.out.println(" Your Test output: "); MyStackInterface
s.push(10); s.push(30); s.push(50); System.out.println("Push 3 data: 10, 30, 50"); System.out.println("Print stack " + s);
if (s.size() == 3) System.out.println("OK: stack size is 3"); else System.out.println("Error: stack size is " + s.size());
if (s.peek() == 50) System.out.println("OK: peek stack top is 50"); else System.out.println("Error: peek stack top is " + s.size());
if (!s.empty()) System.out.println("OK: stack is not empty"); else System.out.println("Error: stack is empty");
System.out.println("Pop 2 data: 50, 30"); s.pop(); System.out.println("Print stack " + s); int data=s.pop(); System.out.println("Print stack " + s); if (data == 30) System.out.println("OK: stack pop data is 30"); else System.out.println("Error: stack pop data is " + data);
System.out.println("Clear stack"); s.clear(); System.out.println("Print stack " + s); }
} // end Stack
Step by Step Solution
There are 3 Steps involved in it
Step: 1
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