【问题标题】:How do I change a singly-linked list to a doubly-linked list?如何将单链表更改为双链表?
【发布时间】:2013-09-17 06:50:29
【问题描述】:

我目前正在学习 Java 课程,教授告诉我们,理解链接的一个好习惯是制作一个双向链表。我制作了一个单链表,但我无法将其转换为双链表。所以我想知道是否有人可以给我任何建议以确保我的最后一个号码与前一个号码相关联?并且如果前面的数字和最后的数字连接为null。这是代码的一部分,如果您希望获得更多代码,请询问,我会发布。

添加元素等的代码。这是我试图使尾部连接到最后一个数字的尝试。

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;

下面的代码是插入函数,我需要确保新插入的块连接但我在想办法让它同时连接时遇到了麻烦。

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;

    }

【问题讨论】:

  • 列表中的每个节点现在都应该有一个previous 引用。
  • 另外,您应该参考firstlast
  • 所以我应该添加对前一个节点的引用,换句话说,我应该添加对我的 CurrentNode 的引用?
  • 您的第一个(头)节点应该有prev-&gt;nullnext-&gt;[the next node]。下一个节点应该有prev-&gt;headnext-&gt;[the next node or null]
  • @nachokk:所以我应该将 currentNode 引用到第一个节点和最后一个节点?比如使用n.setNext()连接上一个和下一个Node?​​span>

标签: java singly-linked-list doubly-linked-list


【解决方案1】:

您为每个包含其前一个节点的节点添加一个额外的节点字段

插入伪代码:

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】:

    对于双向链表,需要在类中同时添加头域和尾域,使其保持双链数据结构。

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

    要将新节点添加到链表的末尾,您可以这样做

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

    请注意,您还需要处理第一个 if 子句的空列表情况。您还可以在添加节点时为 tail 设置 next 和 prev 引用。

    要将节点插入到列表的某个位置,需要考虑如果指定位置小于0或大于现有列表大小,则会发生错误,因为无法插入越界元素指数。所以你可以抛出异常。当插入位置在0 (head) 或size (tail) 时,您还需要分隔大小写。考虑到所有这些情况,解决方案是:

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

    【讨论】:

      猜你喜欢
      • 2015-05-06
      • 2012-03-21
      • 2013-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多