Menu

(Solved) : Implement Push Pop Methods Following Class Public Class Linkedstack Private Class Node Nod Q44002927 . . .

Implement the push() and pop()methods in the following class.

public class LinkedStack<T> {
private class Node {
Node next;
T data;

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 thatobject as
* the value of this function. null is returned if stack isempty
* Returns:
* The object at the top of this stack or null if stack isempty
*/
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; falseotherwise.
*/
public boolean empty() {
return head == null;
}
}
Please add comments to help me learn, thanks.

Expert Answer


Answer to Implement the push() and pop() methods in the following class. public class LinkedStack { private class Node { Node next…

OR