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

java LinkedStack For this question, you must implement the following two methods

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

roll() and unroll using the technique presented in class to implement recursive

Files:

public interface Stack { /** Returns true if this stack is empty, and * false otherwise. * * @return true if this stack is empty, and * false otherwise. */ 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 LinkedStack implements 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 true if this stack is empty, and * false otherwise. * * @return true if this stack is empty, and * false otherwise. */ 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!