1. Implement the methods specified for the unbounded stack (LinkedStack) • Add a
ID: 3753821 • Letter: 1
Question
1. Implement the methods specified for the unbounded stack (LinkedStack)
• Add an inspector method inspect(int loc). The method will return the element found at the location loc. Return null if the location is invalid.
• Implement the method popSome(int count). The method will remove the top count items from the stack. The method should throw a StackUnderFlow Exception as needed. Note that you may have some room for decisions in this method, but possible less choices than with the Array implementation. Document your approach in the comments.
• Without adding any new instance variables, implement a size() method. The method will return an integer value indicating the number of elements in the stack. You will need to traverse your stack nodes using a while loop similar to the example in class.
** These methods will not be part of the StackInterface or the BoundedStackInterface. You don’t need to define them in the interface declarations.
2. Test your code in the LinkedStackTester to show the results of using the new methods
LinkedStack
package ch03;
import support.LLNode;
public class LinkedStack<T> implements UnboundedStackInterface<T> {
protected LLNode<T> top;
public LinkedStack() {
this.top = null;
}
@Override
public void pop() throws StackUnderflowException {
// TODO Auto-generated method stub
if (isEmpty())
throw new StackUnderflowException("Stack is empty");
else {
top = top.getLink();
}
}
@Override
public T top() throws StackUnderflowException {
// TODO Auto-generated method stub
if (isEmpty())
throw new StackUnderflowException("Stack is empty");
else {
return top.getInfo();
}
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return (top == null);
}
@Override
public void push(T element) {
// TODO Auto-generated method stub
LLNode<T> newNode = new LLNode(element);
newNode.setLink(top);
top = newNode;
}
}
Explanation / Answer
The LinkedStack class uses LLNode class. You have not provided the LLNode class and also your Tester class.
Based on the exising code, I have coded the three new methods - size(), inspect() and popSome().
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
package ch03;
import support.LLNode;
public class LinkedStack<T> implements UnboundedStackInterface<T> {
protected LLNode<T> top;
public LinkedStack() {
this.top = null;
}
@Override
public void pop() throws StackUnderflowException {
// TODO Auto-generated method stub
if (isEmpty())
throw new StackUnderflowException("Stack is empty");
else {
top = top.getLink();
}
}
@Override
public T top() throws StackUnderflowException {
// TODO Auto-generated method stub
if (isEmpty())
throw new StackUnderflowException("Stack is empty");
else {
return top.getInfo();
}
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return (top == null);
}
@Override
public void push(T element) {
// TODO Auto-generated method stub
LLNode<T> newNode = new LLNode(element);
newNode.setLink(top);
top = newNode;
}
public int size(){
LLNode<T> curr = top;
int count = 0;
while(curr != null){
count++;
curr = curr.getLink();
}
return count;
}
//loc is the index from the top. 0 refers to top, 1 refers to element below top and so on
//return null if invalid loc
public T inspect(int loc){
LLNode<T> curr = top;
for(int i = 0; curr != null && i < loc; i++ )
curr = curr.getLink();
if(curr == null)
return null;
else
return curr.getInfo();
}
//will pop upto count elements if available. If count elements are not available, will not pop any,
//but throw exception
public void popSome(int count) throws StackUnderflowException{
if(count > size())
throw new StackUnderflowException("Not enough elements to pop " + count + " elements");
for(int i = 1; i <= count; i++)
pop();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.