简体   繁体   English

添加到双向链接列表的末尾

[英]Add to end of a doubly linked list

I am really struggling in adding a node at the end of a double linked list, adding to the head was easy, now I really can't find a way to add to the end. 我真的很努力地在双链表的末尾添加一个节点,添加到头部很容易,现在我真的找不到添加到末尾的方法。 It seems stupid but I'm losing a lot of time, so if anyone can help that would be appreciated. 看起来很愚蠢,但是我正在浪费很多时间,因此,如果有人可以帮助您,我们将不胜感激。 Here's my partial implementation in Java. 这是我在Java中的部分实现。

public class DoublyLinkedList implements Iterable<Node>, Iterator<Node> {

private Node head = new Node();
private Node tail = head;
private Node current;
private Node previous;

@Override
public Iterator<Node> iterator() {
    current = head;
    previous = null;
    return this;
}

public void addToHead(Node node) {
    Node next = head.getNext(null);
    Node previous = head.getNext(next);
    head.setNext(previous, node);
    node.setNext(null, head);
    head = node;
}

@Override
public boolean hasNext() {
    return current != tail;
}

@Override
public Node next() {
    Node tmp = previous;
    previous = current;
    current = current.getNext(tmp);
    return previous;
}

And here's Node Class 这是节点类

public class Node {
Node next = null;
Node previous = null;


Node getNext(Node node){
    if(node == next){
        return previous;
    }

    if(node == previous){
        return next;
    }

    throw new IllegalStateException("something went wrong");
}

void setNext(Node node, Node next){
    if(node == previous){
        previous = next;
    }
    else if(node == this.next){
        this.next = next;
    }
    else{
        throw new IllegalStateException("something went wrong");
    }
}


}

I hope you will understand the code above, basically I will need the following : 希望您能理解上面的代码,基本上我需要以下代码:

public void addToEnd(Node node) {
    // implementation
}

What interface are you aiming to implement on this double-linked list? 您打算在此双向链接列表上实现什么接口? The way you've implemented getNext and setNext is not a clear way of doing it. 您实现getNextsetNext方式尚不清楚。 If you only have one node, and you attempt to setNext with a null value for node , there is no way of telling unless you look at the code where your new node is going to end up. 如果你只有一个节点,并尝试setNext一个null的值node ,也没有告诉,除非你看看您的新节点将要结束的代码的方式。 Along with that, I'm not sure if there is a nice way for you to set the previous node of a new tail node with the way you've implemented it. 随之而来的是,我不确定是否有一种很好的方法可以用实现方式设置新尾节点的上一个节点。 It's very ambiguous. 非常模棱两可。

Can I recommend that you instead implement getNext() , setNext(Node) , getPrevious() and setPrevious(Node) methods in your Node class? 我是否可以建议您在Node类中实现getNext()setNext(Node)getPrevious()setPrevious(Node)方法? This would greatly simplify and clear up your code. 这将大大简化和清除您的代码。

You would then be able to implement your addToEnd(Node) method very simply. 然后,您将可以非常简单地实现addToEnd(Node)方法。

public void addToEnd(Node node) {
    node.setPrevious(tail);
    tail.setNext(node);
    tail = node;
}

If you want to be able to iterate through the list in both directions using 'getNext()' and 'hasNext()', you could provide a forward or reverse Iterator implementation. 如果您希望能够使用“ getNext()”和“ hasNext()”在两个方向上遍历列表,则可以提供正向或反向Iterator实现。 There are examples of how to create Iterators floating around. 有一些示例如何创建浮动的Iterators The first answer on this question has an example of a reverse iterator. 关于这个问题的第一个答案有一个反向迭代器的例子。

The basic idea is just the same as adding at the beginning of the list. 基本思路与在列表的开头添加相同。

We can split it up into two parts: 我们可以将其分为两部分:

  1. adding the item to the list 将项目添加到列表
  2. updating the head/tail-node of the list 更新列表的头/尾节点

In pseudocode: 用伪代码:

node to_insert

//step 1
tail.setNext(to_insert)
to_insert.setPrevious(tail)

//step 2
tail = to_insert

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

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