Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

From text book: p. 158 #47 Add the following methods to the LinkedStack class. In order to practice your linked list related coding skills, code

From text book: p. 158 #47

Add the following methods to the LinkedStack class. In order to practice your linked list related coding skills, code each of these methods by accessing the internal variables of the LinkedStack, not by calling the

previously defined public methods of the class.

a. String toString() - creates and returns a string that correctly represents the current stack. Such a method could prove useful for testing and debugging the class and for testing and debugging applications that use the class. Assume each stacked element already provided its own reasonable toString method.

b. int size() - returns a count of how many items are currently on the stack. Do not add any instance variables to the LinkedStack class in order to implement this method.

c. void popSome(int count) - removes the top count elements from the stack; throws StackUnderflowException if there are less than count elements on the stack.

d. boolean swapStart() - if there are less than two elements on the stack returns false; otherwise it reverses the order of the top two elements on the stack and returns true.

e. T poptop() - the "classic" top operation, if the stack is empty it throws StackUndrflowException; otherwise it both removes and returns the top element of the stack.

Set up:

Create a new java project called Lab5 and place the .java files in the src folder.

Submission Instructions:

Run the Lab5Tester.java to confirm your solution is correct. Submit the LinkedStack.java file only.

public class LinkedStack implements StackInterface{

LLNode top ;

private Object setLink;

public LinkedStack() {

top =null;

}

@Override

public void push(T element) throws StackOverflowException {

LLNode newNode =new LLNode<>(element);

newNode.setLink(top);

top = newNode;

}

@Override

public void pop() throws StackUnderflowException {

if(isEmpty()) {

throw new StackUnderflowException();

}

top = top.getLink();

}

@Override

public T top() throws StackUnderflowException {

if(isEmpty()) {

throw new StackUnderflowException();

}

return top.getInfo();

}

@Override

public boolean isEmpty() {

return (top== null);

}

@Override

public boolean isFull() {

return false;

}

public String toString() {

String s = setLink.toString();

for(int i=1; i

return s;

}

public String size() {

// TODO Auto-generated method stub

return null;

}

public void popSome(int i) {

}

public void swapStart() {

LLNode oldTop = top;

LLNode newTop = top.getLink();

}

public String poptop() {

return null;

}

}

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

Database Internals A Deep Dive Into How Distributed Data Systems Work

Authors: Alex Petrov

1st Edition

1492040347, 978-1492040347

More Books

Students also viewed these Databases questions