English 中文(简体)
列入与杜布有联系的名单
原标题:Insertion into Doubly linked list

我对列入共同名单的内容有问题。

public class LinkedList {
    public Node first;
    public Node last;

    public LinkedList() {
        first = null;
        last = null;
    }

    public void addFirst(Student student) {
        Node f = first;
        Node newNode = new Node(student);
        first = newNode;
        if (f == null) last = newNode;
        else f.previous = newNode;
    }

    public void addLast(Student student) {
        Node l = last;
        Node newNode = new Node(student);
        last = newNode;
        if (l == null) first = newNode;
        else {
            l.next = newNode;
        }
    }


    public void display() {
        Node current = first;
        while (current != null) {
            //print...
            current = current.next;
        }
    }

我的问题是,在我执政的时候:

list.addLast(1);
list.addFirst(2);
list.display();

It displays just "2" display method just can t see last added element.
But if I run:

list.addFirst(2);
list.addLast(1);

It will display both. What is wrong with it? Thanks.

最佳回答

增 编 首先,你们还必须制定<条码>新编号= f,现在你只是重新更新双向关系的一个方面。 由于展览会利用下一个领域,所以它不像你所期望的那样工作。

同样,在添加“密码”时,你需要加上“新编号” = l,然而,由于以往的实地数字在显示方法中使用,在你执行时,该代码似乎没有标记。

问题回答

如果这份名单与这个名单有双重联系,那么你是否应在新的诺德中增加提及该名单之前/之后的内容?

Your display() method traverses node.next but in addFirst() you never set .next - so if you call addFirst() several times, and only that method, what will display() print?

public void addFirst(Student student) {
    Node f = first;
    Node newNode = new Node(student);
    newNode.next = f; // this was missing
    first = newNode;
    if (f == null)
        last = newNode;
    else
        f.previous = newNode;
}

public void addLast(Student student) {
    Node l = last;
    Node newNode = new Node(student);
    newNode.previous = l; // this was missing
    last = newNode;
    if (l == null)
        first = newNode;
    else
        l.next = newNode;
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签