Question: Instructions: JAVA 1. Please change the methods accordingly to make it a doubly linked list. 2. Reading a file using a file reader class. Please

Instructions: JAVA

1. Please change the methods accordingly to make it a doubly linked list.

2. Reading a file using a file reader class. Please read the heroes file and put the input from the file into the stack. You dont have to create a long list of heroes, just two will be enough. The goal of this lab to read a file, put the input into the stack and change the stack into a doubly linked list. Then, in your main method, test your file reader and see how it works. A basic test outline and Stack code is provided for you

//comments in the code are hinted as to what should be done.

######MAIN########

public class Main { public static void main(String[] args) { Main mainClass = new Main(); Stack list = new Stack(); String hero1 = new String(); String hero2 = new String(); /** * TO-DO: * Lab 09: Use scanner class to read the hero names, * and assign those names to two Strings above. */

list.push(hero1); list.push(hero2); System.out.println("Length : "+list.size());

list.iterate(); } }

#########NODE#########

public class Node { private String data; private Node next; /** * Lab 09: Stack adjustment for doubly-linked list */ private Node previous;

/** * TO-DO * Lab 09: Made adjustments to make the stack as a doubly-linked list * Adjustments required on: constructor, and accessors of 'previous' node */ public Node(String data) { this.data = data; this.next = null; }

public Node(String data, Node next){ this.data = data; this.next = next; }

/** * @return data */ public String getData() { return data; }

/** * @param data :the data to set */ public void setData(String data) { this.data = data; }

/** * @return String of the data */ public String toString() { return "Data: "+ data.toString(); }

/** * @return the next */ public Node getNext() { return next; }

/** * @param next :the next to set */ public void setNext(Node next) { this.next = next; } }

#########STACK########

public class Stack {

private int size = 0; private Node root = null; //Lab 09: Stack double-linked list adjustment private Node tail = null;

/* * It has to return the size of the NodeList * * @return size */ public int size() { return size; }

/** * TO-DO: * Lab 09: Made adjustments to make the stack as a doubly-linked list * @param item */ // add an element to the stack public void push(String item) { Node oldfirst = root; root = new Node(item, oldfirst); size++; }

/** * TO-DO: * Lab 09: Made adjustments to make the stack as a doubly-linked list * @return item */ // delete and return the most recently added element public Object pop() { if (isEmpty()) throw new RuntimeException("Stack underflow"); String item = root.getData(); // save item to return root = root.getNext(); // delete first node size--; return item; // return the saved item }

/** * TO-DO: * Lab 09: Made adjustments to make the stack as a doubly-linked list * @return boolean */ // is the stack empty? public boolean isEmpty() { return root == null; } /** * TO-DO: * Lab 09: Made adjustments to make the stack as a doubly-linked list * @return */ /** * Start with the head and traverse till you reach null. */ public void iterate(){ System.out.println("Iterate forward..."); Node current = root; while(current!=null){ System.out.println(current.toString()); current = current.getNext(); } } }

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!