Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

LinkedStack class Add the following methods to the LinkedStack class package ch0

ID: 3791534 • Letter: L

Question

LinkedStack class

Add the following methods to the LinkedStack class

package ch02.stacks;

import support.LLNode;

public class LinkedStack implements StackInterface
{
protected LLNode top;   // reference to the top of this stack

public LinkedStack()
{
    top = null;
}

public void push(T element)
// Places element at the top of this stack.
{
    LLNode newNode = new LLNode(element);
    newNode.setLink(top);
    top = newNode;
}   

public void pop()
// Throws StackUnderflowException if this stack is empty,
// otherwise removes top element from this stack.
{                
    if (isEmpty())
      throw new StackUnderflowException("Pop attempted on an empty stack.");
    else
      top = top.getLink();
}

public T top()
// Throws StackUnderflowException if this stack is empty,
// otherwise returns top element of this stack.
{               
    if (isEmpty())
      throw new StackUnderflowException("Top attempted on an empty stack.");
    else
      return top.getInfo();
}

public boolean isEmpty()
// Returns true if this stack is empty, otherwise returns false.
{            
    return (top == null);
}

public boolean isFull()
// Returns false - a linked stack is never full
{            
      return false;
}

}

---------------------------------------------------------------------------------

I need help completing the methods and drivers. so far this is what I have in my code.

-------------------------------------------------------------------------------------

// represents the current stack.
      StringBuilder output = new StringBuilder();
      LLObjectNode traverse = top;
      while(traverse != null){
          output.append(traverse.getInfo());
          output.append(" ");
          traverse = traverse.getLink();
      }
      return output.toString();
}

// returns a count of how many items are currently on the stack.
    public int size()
{
    return count;
}

// removes the top count elements from the stack; throws StackUnderflowException if there are less than count elements on the stack
    public T pop() throws EmptyStackException
{
    if (isEmpty())
      throw new EmptyStackException();

    T result = top.getElement();
    top = top.getNext();
    count--;

    return result;
}

Add the following methods t the Linkedstack class and create a test driver for each to show that the work correctly code each of these methods by accessing the internal variables of the Linkedstack, not by calling the previously defined public methods of the class. String tostring creates and returns a string that correctly represents the current stack. Such a method could prove useful for testing and debugging the class and for testing and debugging applications that use the class. Assume each stacked element already provided its own reasonable tostring method int size returns a count of how many items are currently on the stack. Do not add any instance variables to the Linkedstack class in order to implement this method void popsome (int count) removes the top count elements from the stack throws StackUnderflowException if there are less than count elements on the stack. boolean swapstart()-if there are less than two elements on the stack returns false; otherwise it reverses the order of the top two elements on the stack and returns true. T poptop )-the classic pop operation if the stack is empty it throws Stack Underflow Exception

Explanation / Answer

mport support.LLNode;

public class LinkedStack implements StackInterface
{
protected LLNode top;   // reference to the top of this stack

public LinkedStack()
{
    top = null;
}

public void push(T element)
// Places element at the top of this stack.
{
    LLNode newNode = new LLNode(element);
    newNode.setLink(top);
    top = newNode;
}   

public void pop()
// Throws StackUnderflowException if this stack is empty,
// otherwise removes top element from this stack.
{                
    if (isEmpty())
      throw new StackUnderflowException("Pop attempted on an empty stack.");
    else
      top = top.getLink();
}

public T top()
// Throws StackUnderflowException if this stack is empty,
// otherwise returns top element of this stack.
{               
    if (isEmpty())
      throw new StackUnderflowException("Top attempted on an empty stack.");
    else
      return top.getInfo();
}

public boolean isEmpty()
// Returns true if this stack is empty, otherwise returns false.
{            
    return (top == null);
}

public boolean isFull()
// Returns false - a linked stack is never full
{            
      return false;
}

}

---------------------------------------------------------------------------------

I need help completing the methods and drivers. so far this is what I have in my code.

-------------------------------------------------------------------------------------

// represents the current stack.
      StringBuilder output = new StringBuilder();
      LLObjectNode traverse = top;
      while(traverse != null){
          output.append(traverse.getInfo());
          output.append(" ");
          traverse = traverse.getLink();
      }
      return output.toString();
}

// returns a count of how many items are currently on the stack.
    public int size()
{
    return count;
}

// removes the top count elements from the stack; throws StackUnderflowException if there are less than count elements on the stack
    public T pop() throws EmptyStackException
{
    if (isEmpty())
      throw new EmptyStackException();

    T result = top.getElement();
    top = top.getNext();
    count--;

    return result;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote