Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Optimization And Data Science Trends And Applications 5th Airoyoung Workshop And Airo Phd School 2021 Joint Event

Authors: Adriano Masone ,Veronica Dal Sasso ,Valentina Morandi

1st Edition

3030862887, 978-3030862886

More Books

Students also viewed these Databases questions

Question

LOQ 14-13: How do psychologists use traits to describe personality?

Answered: 1 week ago

Question

1. Are my sources credible?

Answered: 1 week ago

Question

3. Are my sources accurate?

Answered: 1 week ago

Question

1. Is it a topic you are interested in and know something about?

Answered: 1 week ago