简体   繁体   English

将单链列表转换为双链列表

[英]Converting a singly linked list to a doubly linked list

First of all, I have searched and I can't seem to find anything that helps me understand implementing a doubly linked list better. 首先,我进行了搜索,但似乎找不到任何可以帮助我更好地理解实现双重链接列表的内容。 I will also say that this is for an assignment that I have as well, and I'm not expecting someone to just do it for me. 我还要说的是,这也是我的一项任务,我不希望有人为我做这件事。 With that said, I have working code for a singly linked list(linear) that adds, removes, gets the size, clears, etc...and I want to implement doubly linked list implementation in this code. 话虽如此,我有一个用于添加,删除,获取大小,清除等的单链接列表(线性)的工作代码,并且我想在此代码中实现双链接列表实现。

From what I understand, this should be fairly straight forward. 据我了解,这应该很简单。 However, my book does not go into very much detail about it, and provides very little as far as an example. 但是,我的书并没有涉及太多细节,仅提供了很少的示例。 I'm having a hard time visualizing how to implement this. 我很难想象如何实现这一点。 I realize that in a singly linked list the previous node points to the next node only, requiring that you traverse the entire list any time you want to delete or add anything. 我意识到在单链列表中,前一个节点仅指向下一个节点,这要求您在想要删除或添加任何内容时遍历整个列表。 In a doubly linked list it's supposed to traverse from the end that is closest to the node you are working with. 在双向链表中,它应该从最靠近您正在使用的节点的一端开始遍历。 Please correct me if my understanding is incorrect. 如果我的理解不正确,请纠正我。

Here's the piece of code I've modified so far. 这是我到目前为止已修改的代码。

private class Node
{
    private T data;
    private Node next;
    private Node previous;



    private Node(T dataPortion)
    {
        this(dataPortion, null, null);
    }

    private Node(T dataPortion, Node nextNode, Node prevNode)
    {
        data = dataPortion;
        next = nextNode;
        previous = prevNode;

    }

I added the "private Node previous" for the reference to the previous node in the list and set that equal to prevNode following the previous format that was in the code. 我在列表中添加了“先前的私有节点”作为对先前节点的引用,并将其设置为等于代码中遵循先前格式的prevNode。 Is there anything else in the initial setup that I need to do before moving on to the individual methods? 在继续进行各个方法之前,我在初始设置中还有其他需要做的事情吗?

In each of my methods, I'm not sure what I need to do now to use this to reference the previous Node as well as the next as it does currently. 在我的每个方法中,我不确定现在要使用它来引用上一个Node和当前下一个Node时需要做什么。 Can someone help me out with this, possibly providing some example code? 有人可以帮我解决这个问题,可能提供一些示例代码吗? Here is the current add method for reference. 这是当前的添加方法以供参考。

public boolean add(T newEntry)
{
    Node newNode = new Node(newEntry);

    if (isEmpty())
        firstNode = newNode;
    else
    {
        Node lastNode = getNodeAt(numberOfEntries);
        lastNode.setNextNode(newNode);
    }

    numberOfEntries++;

    return true;
}

Edit: Added the set and get methods for previous Node. 编辑:添加了上一个Node的set和get方法。

private Node getNextNode()
    {
        return next;
    }

    private Node getPrevNode()
    {
        return previous;
    }

    private void setNextNode(Node nextNode)
    {
        next = nextNode;
    }
    private void setPrevNode(Node prevNode)
    {
        previous = prevNode;
    }

Is this what I'm supposed to add? 这是我应该添加的吗?

public boolean add(T newEntry)
{
    Node newNode = new Node(newEntry);


    if (isEmpty())
        firstNode = newNode;
    else
    {
        Node lastNode = getNodeAt(numberOfEntries);
        Node prevNode = getNodeAt(numberOfEntries -1);
        lastNode.setNextNode(newNode);
        newNode.setPrevNode(prevNode);
    }

    numberOfEntries++;

    return true;
}

You did all correct. 一切都正确。 All you need is to add newNode.setPreviousNode(lastNode) 您只需要添加newNode.setPreviousNode(lastNode)

You can take a look at LinkedList for reference. 您可以查看LinkedList以供参考。 They are storing in the list two nodes for reference first and last . 它们将两个节点存储在列表中,以供参考firstlast With such approach it allows you not to iterate over the all list in add method with getNodeAt function. 通过这种方法,您无需使用getNodeAt函数即可遍历add方法中的所有列表。

Implementatopn of add method from LinkedList LinkedList中add方法的实现

void linkLast(E e) {
    final Node<E> l = last;
    final Node<E> newNode = new Node<>(l, e, null);
    last = newNode;
    if (l == null)
        first = newNode;
    else
        l.next = newNode;
    size++;
    modCount++;
}
public boolean add(T newEntry)
{
    Node newNode = new Node(newEntry);
    newNode.previous = this;
    newNode.next = next;
    next = newNode;
    numberOfEntries++;
    return true;
}

The main thing is that you don't need to know what the rest of the data structure is doing. 最主要的是,您不需要知道其余数据结构在做什么。 You don't even need to know there are other nodes. 您甚至不需要知道还有其他节点。 You just tell the node to add this node and update the links. 您只是告诉节点添加此节点并更新链接。 Or delete this node by removing references to it. 或者通过删除对该节点的引用来删除该节点。

public void remove() {
     if (next != null) next.previous = previous;
     if (previous != null) previous.next = next;
     numberofEntries--;
}

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

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