BufferedReader 输入到双向链表

BufferedReader input to doubly linked list

我正在做一个数据结构项目。我需要使用 bufferedReader 让用户将字符串输入到双向链表中,其中 -1 终止流并显示列表。这是我到目前为止所拥有的。我输入的每个输入都会引发异常并关闭 Stream。我哪里错了?

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();

        }

    }  

}

节点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

双向链表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

你的时间应该是这样的:

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

给你,你有一堆错误,

  1. 缺少大括号。
  2. 关闭循环内的流。 (你打算如何在下一次迭代中读取输入?)
  3. `displayList` 没有接受任何参数(如果 `LinkedList` 没有任何引用,将如何打印它?
  4. 最后但并非最不重要的一点是,您甚至没有调用 displayList。

NodeLinkedList 类 没问题。我还做了一些其他的小改动。

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();

        }

    }  

}

您需要进行 2 处更改:

  1. 变化:

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

    inputString.equals("-1")    
    
  2. 要在用户输入 -1 时跳出循环,请在 displayList() 之后添加一个 break; 语句。

然后你问了

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

答案:

没有。它不会进入循环。你认为为什么会这样?它只是调用 class DoublyLinkedList 的构造函数,初始化 firstlast 等于 null