Question: 1. Implement the methods specified for the unbounded stack (LinkedStack) * Add an inspector method inspect(int n) . The method will return the nth element

1. Implement the methods specified for the unbounded stack (LinkedStack)

* Add an inspector method inspect(int n). The method will return the nth element in the stack. By traversing the linked list to find that element (NOT USING LLNode top TO SAY WHERE THE TOP IS AT). Rather create another marker, LLNode & assign it to =top;. 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 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.

** These methods will not be part of the StackInterface or the BoundedStackInterface. You dont need to define them in the interface declarations.

2. Test code in the LinkedStackTester to show the results of using the new methods

public class LLNode {

private T data; private LLNode nextNode; public LLNode(T data) { setData(data); //Same thing as using the this.data; } public T getData() { return data; } public void setData(T data) { this.data = data; } public LLNode getNextNode() { return nextNode; } public void setNextNode(LLNode nextNode) { this.nextNode = nextNode; } }

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

import Support.LLNode;

public class LinkedStack implements UnboundedStackInterface {

private LLNode top; public LinkedStack() { // no need to do anything, top will be null anyway } @Override public T pop() throws StackUnderflowException { // TODO Auto-generated method stub LLNode oldNode; if (isEmpty()) throw new StackUnderflowException("pop attempted on an empty stack"); else { oldNode = top; top = top.getNextNode(); oldNode.setNextNode(null); return oldNode.getData(); }

}

@Override public T peek() throws StackUnderflowException { // TODO Auto-generated method stub return null; }

@Override public boolean isEmpty() { // TODO Auto-generated method stub return (top == null); }

@Override public int size() { // TODO Auto-generated method stub return 0; }

@Override public void push(T data) { // TODO Auto-generated method stub LLNode newNode = new LLNode(data); if (isEmpty()) top = newNode; else { newNode.setNextNode(top); top = newNode; } } }

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

public interface StackInterface { public T pop() throws StackUnderflowException; public T peek()throws StackUnderflowException; public boolean isEmpty(); public int size(); }

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

public interface UnboundedStackInterface extends StackInterface {

void push(T element); }

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

public class StackOverflowException extends RuntimeException { public StackOverflowException() { super(); } public StackOverflowException(String message) { super(message); } }

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

public class StackUnderflowException extends RuntimeException { public StackUnderflowException() { super(); } public StackUnderflowException(String message) { super(message); } }

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

public class LinkedStackTester {

public static void main(String[] args) { LinkedStack letterStack = new LinkedStack();

} }

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!