简体   繁体   English

如何在Java中的双向链接列表中附加节点?

[英]How to append a node in a doubly linked list in Java?

I am beginning to study Java. 我开始学习Java。 As an assigment I have to implement a doubly linked list with given interfaces which i included in the code. 作为一项工作,我必须用代码中包含的给定接口实现一个双向链表。 My method insertAtTheEnd() seems not to work properly, since I have the value null on elements after inserting several ones. 我的方法insertAtTheEnd()似乎无法正常工作,因为在插入多个元素后,元素的值为null I checked similar questions on that topic and tried to apply the answers to my problem, but could not get any further. 我在该主题上检查了类似的问题,并尝试将答案应用于我的问题,但此后再也找不到答案。 So any help is appreciated how i can setup this method to make it work. 因此,我将如何设置此方法以使其起作用,我们将不胜感激。 Thanks! 谢谢!

public interface IValueElement
{
    public String getName();
    public void setName(String paramName);
    public int getValue();
    public void setValue(int paramValue);
}

public interface IListElement
{
    public IValueElement getValueElement();
    public void setValueElement(IValueElement value);
    public IListElement getPredecessor();
    public void setPredecessor(IListElement predecessor);
    public IListElement getSuccessor();
    public void setSuccessor(IListElement successor);
}

public interface IList
{
    public IListElement getHead();
    public void insertAtTheEnd(IValueElement value);
//...
}

public class List implements IList
{
    public List()
    {
        if (head == null)
        {
            head = new ListElement(null);
        }
        else
        {
            return;
        }   
    }

    private IListElement head;

    public IListElement getHead()
    {
        return head;
    }

    public void insertAtTheEnd(IValueElement value)
    {
        if (head.getSuccessor() != null)
        {
            IListElement l = head;
            while (l.getSuccessor() != null)
                l = l.getSuccessor();
            IListElement q = new ListElement(value);
            l.setPredecessor(q);
        }
        else
        {
            IListElement q = new ListElement(value);
            q.setPredecessor(head);
            q.setSuccessor(null);
            head.setSuccessor(q);
            head.setPredecessor(q);
        }
    }
}

Additionally here are my implementations of ValueElement and ListElement: 另外,这是我对ValueElement和ListElement的实现:

//ListElement.java

public class ListElement implements IListElement
{
    public ListElement(IValueElement value)
    {
        this.valueElement = checkValueElementAttribute(value);
    }

    private IValueElement checkValueElementAttribute(IValueElement value)
    {
        return (value == null) ? new ValueElement(null, 0) : value;
    }

    private IValueElement valueElement;

    public IValueElement getValueElement()
    {
        return this.valueElement;
    }

    public void setValueElement(IValueElement value)
    {
        if (value != null)
        {
            this.valueElement = value;
        }
    }

    private IListElement predecessor;

    public IListElement getPredecessor()
    {
        return this.predecessor;
    }

    public void setPredecessor(IListElement predecessor)
    {
        this.predecessor = predecessor;
    }

    private IListElement successor;

    public IListElement getSuccessor()
    {
        return this.successor;
    }

    public void setSuccessor(IListElement successor)
    {
        this.successor = successor;
    }
}


// ValueElement.java
public class ValueElement implements IValueElement
{
    private String name;

    public String getName()
    {
        return this.name;
    }

    public void setName(String paramName)
    {
        if (paramName != null)
        {
            this.name = paramName;
        }
    }

    public ValueElement(String name, int value)
    {
        if (name == null || name.equals(""))
        {
            name = "default";
        }
        else
        {
            this.name = name;
        }
        this.value = value;
    }

    private int value;

    public int getValue()
    {
        return this.value;
    }

    public void setValue(int paramValue)
    {
        if (paramValue != 0)
        {
            this.value = paramValue;
        }
    }

    public String toString()
    {
        return "Name: " + this.name + " - Value: " + this.value;
    }

}

Have implementations of IValueElement and IListElement been provided for you, or are they something that you must also implement? 是否已为您提供IValueElement和IListElement的实现,或者您还必须实现它们? From the code you have provided I cannot see implementations for them. 从您提供的代码中,我看不到它们的实现。

Regarding your method insertAtTheEnd, it looks like you have mostly the right idea, but maybe you could improve on it. 关于方法insertAtTheEnd,看来您的想法大致正确,但也许可以改进。

At the end of the if block, you set l's predecessor to q. 在if块的末尾,将l的前任设置为q。 I think that might be the wrong way round. 我认为这可能是错误的方法。 l at that point is the last element in the list, having walked along the chain of successors until there is no more, it seems to me that the new element (q) should now be added as a successor to l NOT a predecessor. l此时,它是列表中的最后一个元素,沿着继任者链走了直到没有更多,在我看来,现在应该将新元素(q)作为后继者添加到l不是前任中。 In addition, I think q would need to have it's predecessor set to l. 另外,我认为q需要将其前身设置为l。

In your else block you correctly set q's predecessor and successor, and correctly set head's successor, however, why do you set head's predecessor to q? 在您的else块中,您正确设置了q的前任和后继者,并正确设置了head的后继者,但是,为什么将head的前任设置为q? That would make q both a successor and predecessor to head. 这将使q既是继任者又是前任。 I think you should just remove that last setPredecessor call. 我认为您应该删除最后一个setPredecessor调用。

please provide the complete code so that we can run that and help you as of now we can only think of these problems: 请提供完整的代码,以便我们可以运行该代码并为您提供帮助,到目前为止,我们只能考虑以下问题:

1) you might not have called setValueElement on those nodes 2)your else block contains setting of both predecessor and successor to the same element so its a definite wrong line of code 1)您可能尚未在这些节点上调用setValueElement 2)您的else块包含同一元素的前继和后继设置,因此它肯定是错误的代码行

shouldn't this be your code 这不应该是您的代码吗

public void insertAtTheEnd(IValueElement value)
    {
        if (head.getSuccessor() != null)
        {
            IListElement l = head;
            while (l.getSuccessor() != null)
                l = l.getSuccessor();
            IListElement q = new ListElement(value);
            l.setSuccessor(q);
        }       

    else
        {
            IListElement q = new ListElement(value);
            q.setPredecessor(head);
            q.setSuccessor(null);
            head.setSuccessor(q);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM