【发布时间】:2017-02-19 13:52:21
【问题描述】:
我想解决一些链表问题,但我无法从控制台获取输入,我不知道我在哪里做错了。
我的代码做错了什么:
import java.util.*;
class ScannerInputLinkedList{
static class Node{
int data;
Node next;
}
void insertNode(Node head, int data){
Node curr = head;
Node temp = new Node();
temp.data = data;
temp.next = null;
while(curr.next!=null){
curr = curr.next;
}
curr.next = temp;
System.out.print(curr.data+"->");
}
System.out.println();
public static void main(String[] args) {
ScannerInputLinkedList obj = new ScannerInputLinkedList();
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
int x;
Node head = new Node();
while(t-- > 0){
x = sc.nextInt();
obj.insertNode(head, x);
}
}
}
【问题讨论】:
-
我有点困惑,你是否想从用户输入构建一个链表并测试这样的列表?
-
我想你从来没有分配过
obj.head -
@cricket_007 : 没明白,你能帮忙吗
-
@Crabime : 从用户输入构造一个链表
-
哦,没关系。大多数链表实现在 List 类中维护一个头节点引用,而不是维护一个带有链接的 Node 元素
标签: java linked-list java.util.scanner singly-linked-list doubly-linked-list