链接列表仅显示头节点,不确定原因

时间:2018-03-24 17:46:12

标签: java linked-list

我正在学校为我的班级做一个链表项目。基本上我们应该从头开始创建一个链表,并添加,删除和查找命令。无论我多么努力,我似乎​​无法让列表显示除头节点以外的任何内容。这是我从节点

开始的类

public class main {

public static void main(String args[]) {
    for (int i = 0; i < 3; i++) {
        LinkedList list = new LinkedList();
        Node focus = new Node();
        String start;
        start = JOptionPane.showInputDialog("Enter 'A' to add an item"
                + "\n" + "Enter 'D' to delete an item\nEnter 'F' to find an item.");
        if (start.equals("a") || start.equals("A")) {
            focus.data = JOptionPane.showInputDialog("enter an item to ADD");
            list.Add(focus);
            while (focus != null) {
                focus = list.head;
                focus = focus.next;
                JOptionPane.showMessageDialog(null, "your list is\n" + focus.getData());
            }
        }
    }
}

} 公共类Node {

String data;
Node next;
Node prev;

public Node(String data, Node next) {
    this.data = data;
    this.next = next;
}

Node() {

}

public void setData(String data) {
    this.data = data;
}

public String getData() {
    return this.data;
}

public void setNext(Node next) {//setnext
    this.next = next;

}

public Node getNext() {
    return next;
}

}

public class LinkedList extends Node {

Node head;
int listcount = 0;

public LinkedList() {
    this.prev = null;
    this.next = null;
    this.listcount = 0;
}

LinkedList(Node Set) {

}

public void Add(Node n) {
    Node current = this.prev;
    if (current != null) {
        current = this.prev;
        this.prev = new Node();
    } else {
        head = this.prev = new Node();
        current = head;
    }
    listcount++;

}

}

I think my biggest problem is the "your list is" part. I can't seem to get it to display anything other than the head node. I would really appreciate the help, as this has been giving me a huge headache. :)

1 个答案:

答案 0 :(得分:0)

首先,为什么您的LinkedList扩展了Node类?它是链接列表而不是节点。链表之前和之后都没有任何内容。因此,链接列表没有上一步下一步。所有元素都添加到列表中,元素插入 head 节点之后。节点的具有上一步下一步。在添加方法中,如果列表的 head 为空(即列表为空),则新元素将成为 head 列表。否则,将在 head 之后插入新节点。

public class LinkedList {

    Node head;
    int listcount = 0;

    public LinkedList() {
        this.head = null;
        this.listcount = 0;
    }

    public void Add(Node n) {
        Node current = this.head;
        if (current == null) {
            head = n;
        } else {            
            Node prev = null;
            while (current != null) {
                prev = current;
                current = current.next;
            }
            prev.next = n;
        }
        listcount++;
    }

    public String toString() {
        StringBuilder builder = new StringBuilder();

        Node current = this.head;
        while (current != null) {
            builder.append(current.data).append(", ");
            current = current.next;
        }

        return builder.toString();
    }
}

我添加了一个 toString 方法,该方法遍历列表并使用每个节点的内容构建一个字符串。

在主要方法中存在一些问题。链接列表仅在您每次选择时都不会初始化一次。如果每次选择某些内容时初始化链接列表,则链接列表将始终重新初始化,并且在添加新元素后,将包含的唯一节点将是 head 节点。

public class main {

    public static void main(String args[]) {
        String start;
        boolean finished=false;
        LinkedList list = new LinkedList();

        while(!finished) {

            start = JOptionPane.showInputDialog("Enter 'A' to add an item"
                    + "\n" + "Enter 'D' to delete an item\nEnter 'F' to find an item.");

            if (start.equals("a") || start.equals("A")) {
                Node focus = new Node();
                focus.data = JOptionPane.showInputDialog("enter an item to ADD");
                list.Add(focus);

                JOptionPane.showMessageDialog(null, "your list is\n" + list.toString());
            }
            else {
                finished = true;
            }
        }
    }
}

尝试查看代码并了解正在发生的事情以及原因。还可以使用铅笔和纸来理解逻辑。