Question: Implement the push() and pop() methods in the following class. public class LinkedStack { private class Node { Node next; T data; Node(T e, Node
Implement the push() and pop() methods in the following class.
public class LinkedStack
Node(T e, Node nxt) { data = e; next = nxt; } }
private Node head = null;
public LinkedStack() { }
/* * Pushes an item onto the top of this stack. * Parameters: * item - the item to be pushed onto this stack. * Returns: * the item argument. */ public T push(T item) { /* * YOUR CODE HERE */ return item; }
/* * Removes the object at the top of this stack and returns that object as * the value of this function. null is returned if stack is empty * Returns: * The object at the top of this stack or null if stack is empty */ public T pop() { /* * YOUR CODE HERE */ return null; // This line will change }
/* * Tests if this stack is empty. * Parameters: * o - element whose presence in this set is to be tested * Returns: * true if and only if this stack contains no items; false otherwise. */ public boolean empty() { return head == null; } } Please add comments to help me learn, thanks.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
