Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

************ LinkedStack Methods ************ ************ CODE THAT IS TO BE USED AS REFERENCE/USED. ************ public class LLNode { private T data; private LLNode nextNode; public

************ LinkedStack Methods ************ image text in transcribed

************ CODE THAT IS TO BE USED AS REFERENCE/USED. ************

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();

System.out.println("Empty: " + letterStack.isEmpty()); letterStack.push("a"); letterStack.push("b"); letterStack.push("c"); letterStack.push("d"); letterStack.push("e"); System.out.println("Empty: " + letterStack.isEmpty());

System.out.println("Pop: " + letterStack.pop()); System.out.println("Pop: " + letterStack.pop()); System.out.println("Pop: " + letterStack.pop()); System.out.println("Pop: " + letterStack.pop()); System.out.println("Pop: " + letterStack.pop()); System.out.println("Pop: " + letterStack.pop());

} }

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. ie 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. i: 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 Stacklnterface or the BoundedStacklnterface. You don't need to define them in the interface declarations. 2. Test code in the LinkedStackTester to show the results of using the new methods 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. ie 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. i: 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 Stacklnterface or the BoundedStacklnterface. You don't need to define them in the interface declarations. 2. Test code in the LinkedStackTester to show the results of using the new methods

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

50 Tips And Tricks For MongoDB Developers Get The Most Out Of Your Database

Authors: Kristina Chodorow

1st Edition

1449304613, 978-1449304614

More Books

Students also viewed these Databases questions

Question

Describe how to measure the quality of work life.

Answered: 1 week ago

Question

What attracts you about this role?

Answered: 1 week ago

Question

Evaluate the impact of unions on nurses and physicians.

Answered: 1 week ago

Question

Describe the impact of strikes on patient care.

Answered: 1 week ago

Question

Evaluate long-term care insurance.

Answered: 1 week ago