Question: java LinkedStack For this question, you must implement the following two methods roll() and unroll using the technique presented in class to implement recursive methods:
java LinkedStack

For this question, you must implement the following two methods roll() and unroll using the technique presented in class to implement recursive methods:

Files:
public interface Stack{ /** Returns trueif this stack is empty, and *falseotherwise. * * @returntrueif this stack is empty, and *falseotherwise. */ boolean isEmpty(); /** Returns the top element, without removing it. * * @return the top element */ E peek(); /** Removes and returns the top element. * * @return the top element */ E pop(); /** Inserts an element onto the stack. * * @param element the element to be inserted */ void push( E element); }
public class LinkedStackimplements Stack { // Objects of the class Elem are used to store the elements of the // stack. private static class Elem { private T value; private Elem next; private Elem(T value, Elem next) { this.value = value; this.next = next; } } // Reference to the top element private Elem top; /** Returns trueif this stack is empty, and *falseotherwise. * * @returntrueif this stack is empty, and *falseotherwise. */ public boolean isEmpty() { return top == null; } /** Inserts an element onto the stack. * * @param value the element to be inserted */ public void push(E value) { if (value == null) { throw new NullPointerException(); } top = new Elem(value, top); } /** Returns the top element, without removing it. * * @return the top element */ public E peek() { // pre-condition: the stack is not empty return top.value; } /** Removes and returns the top element. * * @return the top element */ public E pop() { // pre-condition: the stack is not empty E saved = top.value; top = top.next; return saved; } /** Removes the top element of the stack. The element inserted at * the bottom of the stack. */ public void roll() { throw new UnsupportedOperationException("IMPLEMENT THIS METHOD"); } /** Removes the botttom element. The element is inserted on the * top of the stack. */ public void unroll() { throw new UnsupportedOperationException("IMPLEMENT THIS METHOD"); } /** Returns a string representation of the stack. * * @return a string representation */ @Override public String toString() { StringBuffer stackStr = new StringBuffer("{"); Elem current = top; while (current != null) { stackStr.append(current.value); if (current.next != null) { stackStr.append(","); } current = current.next; } stackStr.append("}"); return stackStr.toString(); } }
The class LinkedStack implements the interface Stack using singly linked elements public interface Stack
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
