How do I change a singly-linked list to a doubly-linked list?

点点圈 提交于 2020-02-07 11:27:13

问题


I'm currently taking a Java class and the professor told us that a good practice to understand links would be to make a doubly linked list. I have made a singly linked list but I am having trouble converting it to a doubly linked list. So I was wondering if anybody could give me any suggestions on making sure my last number is connected to the previous one? And if the front number and last number connected to the null. Here is part of the code, if you wish for more of it just ask and I shall post.

The code for adding elements and such. This is my attempt of trying to make the tail which is the end connect to the last number.

public void add(int element){

            Node n = new Node();
            n.setItem(element);
            n.setNext(head);
            head = n;
            >
            //The tail connected to the new number added.
            n.setItem(element);
            n.setBefore(tail);
            tail = n;

The code below is the insert function which I need to make sure the new inserted blocks connect but I'm having troubles thinking of a way to make it connect for both.

public void insert(int element, int position){

        int currentposition = 0;
        Node currentNode = head;

        //Traverse to the right position
        while(currentposition < position-1){

            currentposition++;
        } 
        Node n = new Node();
        n.setItem(element);
        n.setNext(currentNode.getNext());
        currentNode.setNext(n);

        //The previous number connecting to the new number
        currentNode = tail;

    }

回答1:


You add an extra Node field to each Node that holds its previous Node

Insertion Pseudocode:

insert(Node n, index i) {
    currentIndex = 0
    currentNode = head
    while (currentIndex < i) {
      currentNode = currentNode.next
      currentIndex++
    }
    n.prev = currentNode
    n.next = currentNode.next
    currentNode.next = n
}



回答2:


For doubly linked list, you need to add both head and tail fields in the class so that it maintains a doubly linked data structure.

private Node<T> head = null;
private Node<T> tail = head;
private int size = 0;

To add a new node to the end of the linked list, you can do

public void add(T element) {
    Node<T> node = new Node<T>(element);
    Node<T> current = head;
    if(current == null) {
        current = node;
        head = tail = current;
    } else {
        tail.next = node;
        node.prev = tail;
        tail = node;
    }
    size++;
}

Note that you also need to deal with the empty list case which is the first if clause. Also you can set next and prev references for tail when you add the node.

To insert a node to the list at a certain position, you need to consider that if the specified position is less than 0 or great than the existing list size, then error occurs because you can't insert element with out of bound index. So you can throw exception. Also you need to separate the case when the inserted position is at 0 (head) or at size (tail). Considering all these cases, the solution is:

public void insert(T a, int position) {
    if (position < 0 || position > size)
        throw new IndexOutOfBoundsException();
    Node<T> node = new Node<T>(a);
    Node<T> temp;
    if(position == 0) {
        if(head == null)
            add(a);
        else {
            temp = head;
            head = node;
            node.next = temp;
            temp.prev = node;
        }
        return;
    } else if (position == size) {
        temp = tail;
        tail = node;
        temp.next = tail;
        tail.prev = temp;
        return;
    }

    Node<T> current = head;
    int i = 0;
    while (current != null && i < position-1) {
        current = current.next;
        i++;
    }

    temp = current.next;
    current.next = node;
    node.prev = current;
    node.next = temp;
    temp.prev = node;
}


来源:https://stackoverflow.com/questions/18771808/how-do-i-change-a-singly-linked-list-to-a-doubly-linked-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!