HELP IN JAVA: Create a file of (unsorted) integers. Input them into a doubly lin
ID: 3749234 • Letter: H
Question
HELP IN JAVA:
Create a file of (unsorted) integers. Input them into a doubly linked list so the list is sorted. There are 13 integers in your input file, but make your program work for any input file – meaning don’t use a for loop.
Print the contents of your list forwards and backwards.
Note: Use these integers
56 4 9 11 40 68 3 -3 55 40 30 100 90
You MUST use nodes, not a built in data structure.
Rubric:
* methid to build the list
* method to print fowards
* method to print backwards
* list correctly build sorted/ good code used throughout
Explanation / Answer
// Java program to input data into a doubly linked list so the list is sorted.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class SortDoublyLinkedList {
// class representing Node in the linkedlist
private class Node{
private int data;
private Node next;
private Node previous;
public Node(int data)
{
this.data = data;
previous = null;
next = null;
}
public void setData(int data)
{
this.data = data;
}
public void setNext(Node next)
{
this.next = next;
}
public void setPrevious(Node previous)
{
this.previous = previous;
}
public int getData()
{
return data;
}
public Node getNext()
{
return next;
}
public Node getPrevious()
{
return previous;
}
}
private Node head; // points to the beginning of linkedlist
//constructor
public SortDoublyLinkedList()
{
head = null;
}
// method to insert the node in the linked list, so that the list is sorted in increasing order
public void insert(int data)
{
Node node = new Node(data);
// insert the nod eat the beginning
if(head == null || head.getData()>data)
{
node.setNext(head) ;
if(head != null)
head.setPrevious(node);
head = node;
}else {
Node curr = head;
// loop to find the current position to insert the data in the list
while(curr.getNext() != null)
{
if(curr.getData() > data)
{
node.setNext(curr);
node.setPrevious(curr.getPrevious());
curr.getPrevious().setNext(node);
curr.setPrevious(node);
return;
}
curr = curr.getNext();
}
if(curr.getData() > data)
{
node.setNext(curr);
node.setPrevious(curr.getPrevious());
curr.getPrevious().setNext(node);
curr.setPrevious(node);
}else {
curr.setNext(node);
node.setPrevious(curr);
}
}
}
//method to print the list forward
public void printForward()
{
Node curr = head;
if(head == null)
{
System.out.print(" Empty list");
return;
}
System.out.print(" List contents in forward direction : ");
while(curr != null)
{
System.out.print(curr.getData()+" ");
curr = curr.getNext();
}
}
// method to print the list backward
public void printBackward()
{
Node curr = head;
if(head == null)
{
System.out.print(" Empty list");
return;
}
System.out.print(" List contents in backward direction : ");
while(curr.getNext() != null)
curr = curr.getNext();
while(curr != null)
{
System.out.print(curr.getData()+" ");
curr = curr.getPrevious();
}
}
public static void main(String[] args) throws FileNotFoundException {
// open the file
Scanner scan = new Scanner(new File("sortNumbers.txt"));
SortDoublyLinkedList list = new SortDoublyLinkedList();
int num;
// read till the end of the file
while(scan.hasNext())
{
num = scan.nextInt();
list.insert(num); // insert the node in the list
}
scan.close();
// output
list.printForward();
list.printBackward();
}
}
//end of program
Output:
List contents in forward Direction:
-3 3 4 9 11 30 40 40 55 56 68 90 100
List contents in backward Direction:
100 90 68 56 55 40 40 30 11 9 4 3 -3
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.