Write an LLStack<T> class that implementsthe Stack<T> interface given below usin
ID: 3615486 • Letter: W
Question
Write an LLStack<T> class that implementsthe Stack<T> interface given below using alinked list rather than an array (as done in class).Your LLStack<T> class should include all themethods in the Stack<T> interface(push(), pop(), peek(), isEmpty()) as well as atoString() method. Test this classthoroughly and make sure it works properly, because its essentialfor Part 2! ( Check the other question I posted onCramster)
public interfaceStack<E>
{
// push an item onto the stack
void push(E element);
// pop an item from the stack (and return theitem)
E pop();
// return the item on top of the stack (withoutpopping)
E peek();
// returns whether the stack is empty
boolean isEmpty();
// returns the stack contents as astring
String toString();
}
Explanation / Answer
Hey - i have this from previous class note lectures - pretty muchthe same just the naming is different - very simple to change hereit is and plz rate :) import java.util.EmptyStackException;/** Class to implement interface StackInt as a linked list.* * */public class LLStack implements Stack {/** A Node is the building block for a single-linked list. */private static class Node {// Data Fields/** The reference to the data. */private E data;/** The reference to the next node. */private Node next;// Constructors/** Creates a new node with a null next field.@param dataItem The data stored*/private Node(E dataItem) {data = dataItem;next = null;}/** Creates a new node that references another node.@param dataItem The data stored@param nodeRef The node referenced by new node*/private Node(E dataItem, Node nodeRef) {data = dataItem;next = nodeRef;}} //end class Node// Data Fields/** The reference to the first stack node. */private Node topOfStackRef = null;/** Insert a new item on top of the stack.post: The new item is the top item on the stack.All other items are one position lower.@param obj The item to be inserted@return The item that was inserted*/public E push(E obj) {topOfStackRef = new Node (obj, topOfStackRef);return obj;}/** Remove and return the top item on the stack.pre: The stack is not empty.post: The top item on the stack has beenremoved and the stack is one item smaller.@return The top item on the stack@throws EmptyStackException if the stack is empty*/public E pop() {if (empty()) {throw new EmptyStackException();}else {E result = topOfStackRef.data;topOfStackRef = topOfStackRef.next;return result;}}/** Return the top item on the stack.pre: The stack is not empty.post: The stack remains unchanged.@return The top item on the stack@throws EmptyStackException if the stack is empty*/public E peek() {if (empty()) {throw new EmptyStackException();}else {return topOfStackRef.data;}}/** See whether the stack is empty.@return true if the stack is empty*/public boolean empty() {return topOfStackRef == null;}}Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.