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

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 dont 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 implements UnboundedStackInterface {

protected LLNode 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 newNode = new LLNode(element);

newNode.setLink(top);

top = newNode;

}

}

LLNode

package support;

public class LLNode {

private LLNode link;

private T info;

public LLNode getLink() {

return link;

}

public void setLink(LLNode link) {

this.link = link;

}

public T getInfo() {

return info;

}

public void setInfo(T info) {

this.info = info;

}

public LLNode(T info) {

this.info = info;

this.link = null;

}

}

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!