/* * To change this license header, choose License Headers in Project Properties
ID: 3687944 • Letter: #
Question
/*
* 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 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:
* 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();
}
}
===============================================================
/*
* 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;
}
/**
* @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;
}
}
===============================================================
/*
* 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);
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");
String item = root.getData(); // save item to return
root = root.getNext(); // delete first node
size--;
return item; // 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();
}
}
}
Explanation / Answer
Hello there ,
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.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.