【问题标题】:Linked List Wrong Output Java链表错误输出Java
【发布时间】:2015-02-28 22:48:23
【问题描述】:

大家好,我的单链表有点麻烦。我决定创建一个简单的,因为我们在我的数据结构课程中没有得到足够的练习,并且似乎无法找到为什么我没有得到正确的输出。

代码是:

package linked_list;

public class LinkedList {

    private Node head;
    private Node tail;   // After figuring out head, come back to this FIXME
    private int listSize;

    public LinkedList() {
        head = new Node(null);
        tail = new Node(null);
    }

    public void addLast(String s) {
        Node newNode = new Node(s);
        if (head == null) {
            addFirst(s);
        } else {
            while (head.next != null) {
                head = head.next;
            }
            head.next = newNode;
            tail = newNode;
        }
        listSize++;
    }

    public void addFirst(String s) {
        Node newNode = new Node(s);
        if (head == null) {
            head = newNode;
            tail = newNode;
        }
        else {
            newNode.next = head;
            head = newNode;
        }
        listSize++;
    }

    public Object getFirst() {
        return head.data;
    }

    public Object getLast() {
        return tail.data;
    }

    public void clear() {
        head = null;
        tail = null;
        listSize = 0;
    }

    public Object peek() {
        try {
            if (head == null) {
                throw new Exception ("The value is null");
            }
            else {
                return head;
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }
    }

    public int size() {
        return listSize;
    }

    // This class has the ability to create the nodes that are used
    // in the Linked List.

    private class Node {
        Node next;
        Object data;

        public Node(String value) {
            next = null;
            data = value;
        }

        public Node(Object value, Node nextValue) {
            next = nextValue;
            data = value;
        }

        public Object getData() {
            return data;
        }

        public void setData(Object dataValue) {
            data = dataValue;
        }

        public Node getNext() {
            return next;
        }

        public void setNext(Node nextValue) {
            next = nextValue;
        }
    }
}

现在这是我为运行一个简单的小操作而创建的驱动程序:

package linked_list;

public class LinkedListDriver {

    public static void main(String[] args) {

        LinkedList list1 = new LinkedList();

        list1.clear();

        list1.addLast("This goes last");
        list1.addFirst("This goes first");
        list1.addLast("Now this one goes last");

        System.out.println(list1.getFirst());
        System.out.println(list1.getLast());
    }

}

我的输出是这样的:

This goes last
Now this one goes last

我想我的问题是为什么我没有从我的 getFirst() 方法中得到答案 This goes first。该方法的顺序或结构似乎有问题,但我无法确定。

【问题讨论】:

  • 在写问题之前调试你的代码

标签: java linked-list singly-linked-list


【解决方案1】:

当您在 addLast 中的 else 中时,您正在更改对 head 的引用。在添加 else 时,应该使用另一个引用指针来遍历列表。

此外,您的列表大小仅应在 addLast 中的 else 中增加,因为否则您将增加两次(一次在 addFirst 中,然后在 addLast 中的 if-else 之后再次增加)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-03
    相关资源
    最近更新 更多