Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Add the following methods to the LinkedStack class, and create a test driver for each to show that they work correctly. In order to practice

Add the following methods to the LinkedStack class, and create a test driver for each to show that they work correctly. In order to practice your array related coding skills, code each of the 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 each

class and for testing and debugging applications that use the class. Assume each stack element already provided its own reasonable toString method.

import support.LLNode;

public class LinkedStack implements StackInterface { protected LLNode top; // reference to the top of this stack

public LinkedStack() { top = null; }

public void push(T element) // Places element at the top of this stack. { LLNode newNode = new LLNode(element); newNode.setLink(top); top = newNode; }

public void pop() // Throws StackUnderflowException if this stack is empty, // otherwise removes top element from this stack. { if (isEmpty()) throw new StackUnderflowException("Pop attempted on an empty stack."); else top = top.getLink(); }

public T top() // Throws StackUnderflowException if this stack is empty, // otherwise returns top element of this stack. { if (isEmpty()) throw new StackUnderflowException("Top attempted on an empty stack."); else return top.getInfo(); }

public boolean isEmpty() // Returns true if this stack is empty, otherwise returns false. { return (top == null); }

public boolean isFull() // Returns false - a linked stack is never full { return false; }

}

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_2

Step: 3

blur-text-image_3

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 Modeling And Design

Authors: Toby J. Teorey, Sam S. Lightstone, Tom Nadeau, H.V. Jagadish

5th Edition

0123820200, 978-0123820204

More Books

Students also viewed these Databases questions

Question

Describe the new structures for the HRM function. page 676

Answered: 1 week ago