Question: implement the following methods with the given code public class A5Stack implements Stack { private Node head; public A5Stack() { // TODO: implement this }
implement the following methods with the given code
public class A5Stackimplements Stack { private Node head; public A5Stack() { // TODO: implement this } public void push(T v) { // TODO: implement this } public T pop() { // TODO: implement this return null; // so it compiles } public T top() { // TODO: implement this return null; // so it compiles } public void popAll() { // TODO: implement this } public boolean isEmpty() { // TODO: implement this return false; // so it compiles } }
interface Stack{ /* * Purpose: Insert an item onto the top of the stack * Parameters: T - the item to insert * Returns: Nothing */ public void push(T v); /* * Purpose: Removes and returns the top item from the stack * Parameters: None * Returns: T - the data value of the element removed * Throws: EmptyStackException if stack is empty */ public T pop(); /* * Purpose: Accesses the top item on the stack * Parameters: None * Returns: T - the data value of the top element * Throws: EmptyStackException if stack is empty */ public T top(); /* * Purpose: Determines whether the stack is empty * Parameters: None * Returns: boolean - true if the stack is empty, false otherwise */ public boolean isEmpty(); /* * Purpose: Removes all elements from the stack * Parameters: None * Returns: Nothing */ public void popAll(); }
public class Node{ private T data; protected Node next; public Node (T value) { this.data = value; this.next = null; } /* Parameters: nothing * Purpose: get the data value of this Node * Returns: int - the value */ public T getData() { return data; } /* Parameters: nothing * Purpose: get the next of this Node * Returns: (Node) the next */ public Node getNext() { return next; } /* Parameters: Node next * Purpose: set the next of this Node to next * Returns: nothing */ public void setNext(Node next) { this.next = next; } }
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
