Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Instructions: JAVA 1. Please change the methods accordingly to make it a doubly

ID: 3689738 • Letter: I

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 read the ’heroes’ file and put the input from the file into the stack. You don’t 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();
}
}
  
}

Explanation / Answer

Please find below code and input file (heroes.txt) and its O/P .

===

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.list;
public class Node {
private String data;
private Node next;
/**
* Stack adjustment for doubly-linked list
*/
private Node previous;
/**
* TO-DO
* 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;
}
public Node(String data, Node next,Node previous){
this.data = data;
this.next = next;
this.previous = previous;
}
/**
* @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;
}
   public Node getPrevious() {
       return previous;
   }
   public void setPrevious(Node previous) {
       this.previous = previous;
   }
}

====

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.list;

public class Stack {
   private int size = 0;
   private Node root = null;
   // 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: 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);
       root.setPrevious(null);
       if (oldfirst != null) {
           oldfirst.setPrevious(root);
       }
       size++;
   }

   /**
   * TO-DO: 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");
       else if (!isEmpty()) {
           String item = root.getData(); // save item to return
           root = root.getNext(); // delete first node
           return item;
       }
       return root;// return the saved item

   }

   /**
   * TO-DO: 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: 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();
       }
   }

}

=====

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.list;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Main {
   public static void main(String[] args) {
       BufferedReader br = null;
       Stack list = new Stack();
       try {

           String sCurrentLine;

           br = new BufferedReader(new FileReader("heroes.txt"));

           while ((sCurrentLine = br.readLine()) != null) {
               list.push(sCurrentLine);
           }

       } catch (IOException e) {
           e.printStackTrace();
       } finally {
           try {
               if (br != null)
                   br.close();
           } catch (IOException ex) {
               ex.printStackTrace();
           }
       }

       System.out.println("Length : " + list.size());
       list.iterate();
   }

}

===I/P== heroes.txt

Johnny Depp
Kevin Spacey
Russell Crowe
Brad Pitt
Leonardo DiCaprio
Tom Cruise

Let me know if you have any doubts.