Question: PLEASE HELP! LinkedStack.java import java.util.EmptyStackException;//for when you want to pop from an empty /** A class of stacks whose entries are stored in a chain

PLEASE HELP!

PLEASE HELP! LinkedStack.java import java.util.EmptyStackException;//for when you want to pop from an

LinkedStack.java

import java.util.EmptyStackException;//for when you want to pop from an empty /** A class of stacks whose entries are stored in a chain of nodes. Each node in the chain is an instance of the private class Node that is defined within the class LinkedStack */ public final class LinkedStack implements StackInterface { private Node topNode; // References the first node in the chain //StackInterface myStack = new LinkedStack(); public LinkedStack() { topNode = null;//The default constructor sets head reference to null } // end default constructor public void push(T newEntry) { Node newNode = new Node(newEntry, topNode);//creating a newNode that point to the first node in chain topNode = newNode;//making sure that topNode that references the first node is pointing to our new node // topNode = new Node(newEntry, topNode); // Alternate code } // end push //peek method retrieves data by calling getData on topNode public T peek() { if (isEmpty())//if the stack is empty, nothing is there to be peeked throw new EmptyStackException(); else return topNode.getData();//if we have items in the stack, lets return the firts object's data } // end peek //retrieves and removes the object public T pop() { T top = peek(); // Might throw EmptyStackException // Assertion: topNode != null topNode = topNode.getNextNode();//makes sure that topNode is pointing to the second node in chain return top;//return the value in th etop } // end pop public boolean isEmpty() { return topNode == null; } // end isEmpty public void clear() { topNode = null; // Causes deallocation of nodes in the chain } // end clear private class Node { private T data; // Entry in stack private Node next; // Link to next node //Node newNode = new Node("Jack"); private Node(T dataPortion) { this(dataPortion, null); } // end constructor // Node newNode1 = new Node("Hellen" , newNode); private Node(T dataPortion, Node linkPortion) { data = dataPortion; next = linkPortion; } // end constructor private T getData() { return data; } // end getData private void setData(T newData) { data = newData; } // end setData private Node getNextNode() { return next; } // end getNextNode private void setNextNode(Node nextNode) { next = nextNode; } // end setNextNode } // end Node } // end LinkedStack

StackInterface.java

empty /** A class of stacks whose entries are stored in a

Driver.java

/** A driver that demonstrates the class LinkedStack. @author Frank M. Carrano @author Timothy M. Henry @version 5.0 */ public class Driver { public static void main(String[] args) { System.out.println("Create a stack: "); //myStack is a reference variable with type StackInterface, this means that we can only call the methods in the //StackInterface on myStack variable StackInterface myStack = new LinkedStack();//creates an object of type Linkedstack System.out.println("isEmpty() returns " + myStack.isEmpty()); //if isEmpty works => T System.out.println(" Add to stack to get Joe Jane Jill Jess Jim"); myStack.push("Jim");//Jim myStack.push("Jess");// Jess Jim myStack.push("Jill");// Jill Jess Jim myStack.push("Jane");//Jane Jill Jess Jim myStack.push("Joe");//Joe Jane Jill Jess Jim System.out.println(" isEmpty() returns " + myStack.isEmpty());//F System.out.println(" Testing peek and pop:"); while (!myStack.isEmpty()) { String top = myStack.peek();//call peek to return the data in the topNode to top variable System.out.println(" " + top + " is at the top of the stack.");//print the value of top top = myStack.pop();//delete the first node System.out.println(top + " is removed from the stack."); //see that the node that has been removed is //the same as was peeked } // end while System.out.print(" The stack should be empty: "); System.out.println("isEmpty() returns " + myStack.isEmpty());//T System.out.println(" Add to stack to get " + "Jim Jess Joe "); myStack.push("Joe"); myStack.push("Jess"); myStack.push("Jim");//Jim Jess Joe System.out.println(" Testing clear:"); myStack.clear();//makes the topNode = null System.out.println("The stack should be empty: "); System.out.println(" isEmpty() returns " + myStack.isEmpty());//T System.out.println(" myStack.peek() returns "); System.out.println(myStack.peek()); //Exception because stack is empty System.out.println(" myStack.pop() returns "); //Exception because stack is empty System.out.println(myStack.pop()); } // end testStackOperations } // end Driver /* Create a stack: isEmpty() returns true Add to stack to get Joe Jane Jill Jess Jim isEmpty() returns false Testing peek and pop: Joe is at the top of the stack. Joe is removed from the stack. Jane is at the top of the stack. Jane is removed from the stack. Jill is at the top of the stack. Jill is removed from the stack. Jess is at the top of the stack. Jess is removed from the stack. Jim is at the top of the stack. Jim is removed from the stack. The stack should be empty: isEmpty() returns true Add to stack to get Jim Jess Joe Testing clear: The stack should be empty: isEmpty() returns true myStack.peek() returns Exception in thread "main" java.util.EmptyStackException at LinkedStack.peek(LinkedStack.java:28) at Driver.testStackOperations(Driver.java:58) at Driver.main(Driver.java:12) */

CSC 2720: Data Structures Project 02 Points: 100 points Problem Description: Suppose that the ADT stack included the void method display, which displays the entries in a stack. Implement this method for each of the following classes: a) LinkedStack, as outlined in Listing 6-1. Please make sure that the method uses T generic type and print "Stack is empty" if the stack doesn't have anything. Submission files: Driver.java LinkedStack.java StackInterface.java public interface StackInterface /** Adds a new entry to the top of this stack. @param newEntry An object to be added to the stack. */ public void push(T newEntry); /** Removes and returns this stack's top entry. @return The object at the top of the stack. @throws EmptyStackException if the stack is empty before the operation. */ public T pop(); /** Retrieves this stack's top entry. @return The object at the top of the stack. @throws EmptyStackException if the stack is empty. */ public T peek(); /** Detects whether this stack is empty. @return True if the stack is empty, / public boolean isEmpty(); /** Removes all entries from this stack. */ public void clear()

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!