gpt4 book ai didi

java - BufferedReader 输入到双向链表

转载 作者:行者123 更新时间:2023-12-01 11:24:08 25 4
gpt4 key购买 nike

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

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

}

}

}

节点类:

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

双向链表类:

    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

最佳答案

来吧,你犯了很多错误,

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

}

}

}

关于java - BufferedReader 输入到双向链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30961582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com