Answered step by step
Verified Expert Solution
Question
1 Approved Answer
public class Stack { //linked list version of stack, String data // uses the Node class private Node headNode; // refers to dummy head node
public class Stack { //linked list version of stack, String data // uses the Node class private Node headNode; // refers to dummy head node of the linked list-"bookend" private Node endNode; // refers to dummy end node of the linked list-"bookend" //Constructor establishes an empty stack public Stack() { // sets up two linked dummy "bookend" nodes endNode=new Node("a",null); headNode=new Node("a",endNode); } //Methods - Push, Pop and Peek public void Push(String item) { if(headNode.getLink()==endNode) { // if the list is empty Node n = new Node(); n.setDatum(item); n.setLink(endNode); headNode.setLink(n); } else { Node n = new Node(); n.setDatum(item); n.setLink(headNode.getLink()); headNode.setLink(n); } } public void Pop() { headNode.setLink(headNode.getLink().getLink()); } public String Peek() { return headNode.getLink().getDatum(); } // Method Display public void Display(){ System.out.println("The data in the linked list are:"); Node l = headNode.getLink(); while (l != endNode) { System.out.println( l.getDatum()); l = l.getLink(); } } }
MAIN:
public class StackMain { //uses Stack.java, a linked list implementation public static void main (String[] args) { Stack S1 = new Stack(); // constructs the stack S1 S1.Push("tree"); S1.Push("leaf"); S1.Push("trunk"); S1.Push("branch"); S1.Pop(); S1.Push("twig"); S1.Push("bark"); S1.Display(); }// end main }
The necessary information is all attached, please also provide whatever main program you come up with along with whatever programs are required. Thank you.
Data File: There will be one data file. You must test your program with this file. The file is: bashemin.i n (you should create this file and use it in your program). Its text is: aVAX785 aPDPII aIBM360 aMTHCSC dPDP11 aMAC512 dVAX785 aP1314 aCSB-1 dCSB-1 dIBM360 aPDP11 a257ADT dMACS12 Your program might start out like this - import java.io. class BasheminParkinglot public static void main(Stringl) args) throws Exception Stack parkinglot- new Stack): Stack alley-new Stack(: File f= new File(.bashemin.in"); FilelnputStream finstream new FileinputStream(fl: InputStreamReader finreader new InputstreamReaderffinstream) BufferedReader finput- new BufferedReader(finreader) String line = finput-readLine(); String plate System.out.printin System.out.printin("The Bashemin Status": System.out.printin while(linel-null) fline.charAt(o) 'a') plate-line.substring(1); System.out.printin/"car"+plate+ "arrived and parked": parkinglot.Push(plateStep 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