BufferedReader input to doubly linked list

1.3k Views Asked by At

I am doing a project for Data Structures. I need to have the user input Strings into a doubly linked list using bufferedReader, where -1 terminates the stream and displays the list. Here is what I have so far. Every input I enter is throwing an exception and closing the Stream. Where am I going wrong?

public class Main {

/**
 * @param args the command line arguments
 * @throws java.io.IOException
 */
public static void main(String[] args) throws IOException {
    int count;
    String inputString;
    InputStreamReader reader = new InputStreamReader(System.in);
    BufferedReader in = new BufferedReader(reader);
    DoublyLinkedList list = new DoublyLinkedList();//does this go in the loop?

    while ((inputString = in.readLine()) != null) 
    {
        System.out.println("Enter a String, then press enter.");

        if (inputString.equals(-1) {
          displayList();
        } else {
          list.insertFirst(inputString);
        } 

    count++
    in.close();
}
   /**
 *
 */
public static void displayList() 
    {
    Node first = null;
    Node node = first;

        while(node != null){

            node.displayNode();

            System.out.println("Next Link: " + node.next);

            node = node.next;

            System.out.println();

        }

    }  

}

Node Class:

public class Node {

public String dData; // data item
public Node next; // next node in list
public Node previous; // previous node in list


/**
 *
 * @param d
 */
public Node(String d) // constructor
    { 
dData = d; 
    }

public void displayNode() // display this link
    {
System.out.print(dData + " ");
    } 
}// end class Node

Doubly-linked-list Class:

    public class DoublyLinkedList {

    public Node first; // ref to first item

    /**
     *
     */
    public Node last; // ref to last item


    public DoublyLinkedList() // constructor
    {
        first = null; // no items on list yet
        last = null;
    }


    public boolean isEmpty() // true if no links
    {
        return first == null;
    }


    public void insertFirst(String dd) // insert at front of list
    {
        Node newNode = new Node(dd); // make new link

        if (isEmpty()) // if empty list,
        {
            last = newNode; // newLink <-- last
        } else {
            first.previous = newNode; // newLink <-- old first
        }
        newNode.next = first; // newLink --> old first
        first = newNode; // first --> newLink
    }


    public void insertLast(String dd) // insert at end of list
    {
        Node newNode = new Node(dd); // make new link
        if (isEmpty()) // if empty list,
        {
            first = newNode; // first --> newLink
        } else {
            last.next = newNode; // old last --> newLink
            newNode.previous = last; // old last <-- newLink
        }
        last = newNode; // newLink <-- last
    }


    public Node deleteFirst() // delete first link
    { // (assumes non-empty list)
        Node temp = first;
        if (first.next == null) // if only one item
        {
            last = null; // null <-- last
        } else {
            first.next.previous = null; // null <-- old next
        }
        first = first.next; // first --> old next
        return temp;
    }

    public Node deleteLast() // delete last link
    { // (assumes non-empty list)
        Node temp = last;
        if (first.next == null) // if only one item
        {
            first = null; // first --> null
        } else {
            last.previous.next = null; // old previous --> null
        }
        last = last.previous; // old previous <-- last
        return temp;
    }

// insert dd just after key

    public boolean insertAfter(String key, String dd) { // (assumes non-empty list)
        Node current = first; // start at beginning
        while (!current.dData.equals(key)) // until match is found,
        {
            current = current.next; // move to next link
            if (current == null) {
                return false; // didn’t find it
            }
        }
        Node newNode = new Node(dd); // make new link

        if (current == last) // if last link,
        {
            newNode.next = null; // newLink --> null
            last = newNode; // newLink <-- last
        } else // not last link,
        {
            newNode.next = current.next; // newLink --> old next
// newLink <-- old next
            current.next.previous = newNode;
        }
        newNode.previous = current; // old current <-- newLink
        current.next = newNode; // old current --> newLink
        return true; // found it, did insertion
    }

    /**
     *
     * @param key
     * @return
     */
    public Node deleteKey(String key) // delete item w/ given key
    { // (assumes non-empty list)
        Node current = first; // start at beginning

        while (!current.dData.equals(key)) // until match is found,
        {
            current = current.next; // move to next link
            if (current == null) {
                return null; // didn’t find it
            }
        }
        if (current == first) // found it; first item?
        {
            first = current.next; // first --> old next
        } else // not first
        // old previous --> old next
        {
            current.previous.next = current.next;
        }

        if (current == last) // last item?
        {
            last = current.previous; // old previous <-- last
        } else // not last
        // old previous <-- old next
        {
            current.next.previous = current.previous;
        }
        return current; // return value
    }


    public void displayForward() {
        System.out.print("List(first-- > last): ");
        Node current = first; // start at beginning
        while (current != null) // until end of list,
        {
            current.displayNode(); // display data
            current = current.next; // move to next link
        }
        System.out.println("");
}

public void displayBackward() {
        System.out.print("List(last-- > first): ");
        Node current = last; // start at end
        while (current != null) // until start of list,
        {
            current.displayNode(); // display data
            current = current.previous; // move to previous link


}
        System.out.println("");
}

} // end class DoublyLinkedList
3

There are 3 best solutions below

2
On BEST ANSWER

Here you go, you had a bunch of mistakes,

  1. Missing out a brace.
  2. Closing the stream inside the loop. (How are you going to read input in the next iteration?)
  3. `displayList` did not take any arguments (How will print the `LinkedList` if it doesn’t have any references to it?
  4. Last but not the least, you didn’t even call displayList.

The Node and LinkedList classes are fine. I also made some other minor changes.

import java.io.*;

class Main {

/**
 * @param args the command line arguments
 * @throws java.io.IOException
 */

public static void main(String[] args) throws IOException 
{
    int count = 0;
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    DoublyLinkedList list = new DoublyLinkedList();//does this go in the loop?
    System.out.println("Enter a String, then press enter.");
    String inputString = br.readLine();
    while ((inputString != null) )
    {
        if (inputString.equals("-1")) 
        {
          break;
        } 
        else 
        {
          list.insertFirst(inputString);
        } 
       inputString =br.readLine();
       count++;
  }

  displayList(list);
  br.close();
}

public static void displayList(DoublyLinkedList d) 
    {

    Node node = d.first;

        while(node != null)
        {

            node.displayNode();

            //System.out.println("Next Link: " + node.next);

            node = node.next;

            System.out.println();

        }

    }  

}
0
On

Your while should look like this:

System.out.println("Enter a String, then press enter.");
while (!(inputString = in.readLine()).equals("-1")) {
    if (in.readLine().equals("")) { // filter empty lines, user probably hit enter twice
        continue;
    }
    // add inputString to the list
    System.out.println("Enter a String, then press enter.");
}
// print list
0
On

You need to make 2 changes:

  1. Change:

    inputString.equals(-1)  // A String cannot be equal to an integer.
    

    to

    inputString.equals("-1")    
    
  2. To break out of the loop if user enters -1, add a break; statement after displayList().

Then you asked

 DoublyLinkedList list = new DoublyLinkedList();//does this go in the loop?  

Answer:

No. It doesn't enter a loop. Why do you think it does? It just calls the constructor of class DoublyLinkedList whici initializes first and last equal to null