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;
}
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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.