Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please Answer the bolded section with the # of lines required, Ty import java.util.NoSuchElementException ; /** Testing the StackBasedOnNode class that we wrote in class
Please Answer the bolded section with the # of lines required, Ty
import java.util.NoSuchElementException ; /** Testing the StackBasedOnNode class that we wrote in class and which you partially write below. */ public class StackBasedOnNodeTester { public static void main(String[] args) { StackBasedOnNode stack = new StackBasedOnNode() ; stack.push(new Integer(3)) ; stack.push(new Integer(4)) ; stack.push(new Integer(5)) ; stack.push(new Integer(6)) ; stack.push(new Integer(7)) ; if (! stack.isEmpty()) System.out.println("Top of the stack: " + stack.pop()) ; if (! stack.isEmpty()) System.out.println("Top of the stack: " + stack.pop()) ; if (! stack.isEmpty()) System.out.println("Top of the stack: " + stack.pop()) ; if (! stack.isEmpty()) System.out.println("Top of the stack: " + stack.pop()) ; if (! stack.isEmpty()) System.out.println("Top of the stack: " + stack.pop()) ; if (! stack.isEmpty()) System.out.println("Top of the stack: " + stack.pop()) ; else System.out.println("The stack is empty!") ; System.out.println("--------------Expected:") ; System.out.println("Top of the stack: 7") ; System.out.println("Top of the stack: 6") ; System.out.println("Top of the stack: 5") ; System.out.println("Top of the stack: 4") ; System.out.println("Top of the stack: 3") ; System.out.println("The stack is empty!") ; System.out.println("--------------") ; } } /** Implement the Stack interface using the inner class Node. Your job is to fill in some of the methods below. */ class StackBasedOnNode { /** Instance variables */ Node top ; /** inner class */ class Node { public Node next ; public Object data ; public Node(Object data, Node next) { this.data = data ; this.next = next ; } } /** Constructor to initialize the stack */ public StackBasedOnNode() { top = null ; } /** Method to remove top element of the stack @return the data of the top element */ public Object pop() { //-----------Start below here. To do: approximate lines of code = 4 // 1. fill in the method (don't forget NSEE) //-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions. } /** Method to show the top element without removing it @return the data of the top element */ public Object peek() { //stub return null ; } /** Method to put something on top of the stack @param data the data to put on the stack */ public void push(Object data) { //-----------Start below here. To do: approximate lines of code = 1 // 1. fill in the method //-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions. } /** Method to check if the stack is empty @return true if empty */ public boolean isEmpty() { //-----------Start below here. To do: approximate lines of code = 1 // 1. fill in the method //-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions. } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started