【问题标题】:Trying to delete a node in Singly Circular Linked List尝试删除单循环链表中的节点
【发布时间】:2016-08-06 01:42:11
【问题描述】:

我正在尝试删除具有给定键的节点,并且还想显示更新后的 Tail 和 Head 节点值。我可以删除第一个节点(头)并且无法删除尾节点请检查下面的代码

public void delete(int key){
        Node current = head;            
        while(current.next.data != key){
            current = current.next;
        }
        if(current.next.data == key ){              //deleting current node
            current.next = current.next.next;
            if(current.next == head)
                tail = current;
            else if(current == head)
                head = current.next;    
        }
    }

我的主要方法:

public class Caller {
    public static void main(String args[]){

            Linklist theList = new Linklist();  
            theList.insertFirst(22);
            theList.insertFirst(44);
            theList.insertFirst(55);
            theList.insertFirst(66);
            theList.delete(22);
            System.out.println("deleting 22");
            theList.display();
            theList.delete(66);
            System.out.println("Deleting 66");
            theList.insertLast(99);
            theList.insertLast(11);
            theList.display();
    }
}

我的 insertLast 方法:

public void insertLast(int data){
        Node newNode = new Node(data);
        Node current = head;

        while(current.next != head){
            current = current.next;
        }

        current.next = newNode;
        newNode.next = head;
        tail = newNode;
    }

我的输出是:

deleting 22
Displaying list first ----> last
{ 66 }
{ 55 }
{ 44 }
Head : 66 Tail: 44
Deleting 66

这段代码之后什么都没有发生

【问题讨论】:

  • 您能分享一下您的insertLast() 方法吗?这就是您的代码似乎卡住的地方——不在delete() 内。
  • 更新了@MickMnemonic
  • 注意:“什么都没有发生”是非常模糊的......程序只是安静地退出,还是只是旋转直到您决定终止它?这是一个重要的区别。
  • @Lars 它不在那里旋转.. 只是正常退出,无论如何我找到了解决方案。谢谢

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


【解决方案1】:

这是最好通过用笔和纸逐步运行算法来解决的问题之一。我认为问题不在于删除尾节点(您自己的日志输出显示为有效),而在于删除头节点(在本例中为“66”)。是的,'66' 是最后插入的,但它是在列表中已经存在的任何其他内容之前插入的,因此使其成为头节点。

问题是您在更新头/尾指针之前更改了循环列表的结构。在去掉头节点的情况下,当代码到达current.next = current.next.next;这一行时,current指向尾节点,current.next是头节点,current.next.next是头+1节点。通过执行赋值,current.next 指向 head+1 节点,这意味着if(current.next == head)else if (current == head) 都不会触发。头节点现在位于循环链表之外,但head 指针仍指向该节点;更糟糕的是,head.next 仍然指向循环列表。

另外两个问题:

  • 严重:delete()方法不处理列表最后一个元素的删除
  • 次要:if(current.next.data == key ) 是不必要的,因为它实际上是前面 while 循环的停止条件。

【讨论】:

    【解决方案2】:

    我跟踪了以前的和当前的节点并且它工作了!

    public void delete(int key){
        Node current = head;
        Node prev = current;
    
        while(current.data != key){
            prev = current;
            current = current.next;
        }
        if(current.data == key ){              //deleting current node
            if(current == head){
                prev = tail;
                head = current.next;
            }else if(current == tail){
                tail = prev;
                head = current.next;
            }   
        prev.next = current.next;   
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-30
      相关资源
      最近更新 更多