Question
Add the following methods to the LinkedStack class Add the following methods to the LinkedStack class, and create a test driver for each to show
Add the following methods to the LinkedStack class
Add the following methods to the LinkedStack class, and create a test driver for each to show that they work correctly. In order to practice your array related coding skills, code each of these methods by accessing the internal variables of the LinkedStack, not by calling the previously defined public
int size()returns a count of how many items are currently on the stack. Do not add any instance variables to the ArrayBoundedStack class in order to implement this method.
Code:
//---------------------------------------------------------------------- // LinkedStack.java by Dale/Joyce/Weems Chapter 2 // // Implements StackInterface using a linked list to hold the elements. //-----------------------------------------------------------------------
package ch02.stacks;
import support.LLNode;
public class LinkedStack
public LinkedStack() { top = null;
}
public void push(T element) // Places element at the top of this stack. { LLNode
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; }
public int size() {
} }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started