简体   繁体   中英

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. 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. 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.

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)

You can take a look at LinkedList for reference. They are storing in the list two nodes for reference first and last . With such approach it allows you not to iterate over the all list in add method with getNodeAt function.

Implementatopn of add method from LinkedList

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--;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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