[英]Trying to delete a node in Singly Circular Linked List
I am trying to delete nodes with a given key and also want to display the updated Tail and Head node values. 我正在尝试删除具有给定键的节点,并且还想显示更新的Tail和Head节点值。 I am able to delete first node (Head) and cannot delete Tail node Please check my code below
我可以删除第一个节点(头),但不能删除尾部节点,请检查下面的代码
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;
}
}
My Main method: 我的主要方法:
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();
}
}
my insertLast method: 我的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;
}
and my output is : 我的输出是:
deleting 22
Displaying list first ----> last
{ 66 }
{ 55 }
{ 44 }
Head : 66 Tail: 44
Deleting 66
Nothing happens after this code 此代码后无任何反应
This is one of the problems which are best solved by running through the algorithm step-by-step with pen and paper. 这是通过用笔和纸逐步执行算法可以最好地解决的问题之一。 I think the problem is not in removing the tail node, which your own log output shows as working, but in deleting the head node ('66' in this case).
我认为问题不在于删除尾部节点(您自己的日志输出显示该节点正在工作),而是在于删除头节点(在这种情况下为“ 66”)。 Yes, '66' was inserted last, but it was inserted upfront of anything else already in the list, thus making it the head node.
是的,最后插入的是“ 66”,但它已插入列表中其他所有对象的前面,因此成为头节点。
The problem is that you change the cyclic list's structure before you update the head/tail pointers. 问题是在更新头/尾指针之前,您要更改循环列表的结构。 In the case of removing the head node, when the code gets to the
current.next = current.next.next;
在删除头节点的情况下,当代码到达
current.next = current.next.next;
line, current
points to the tail node, current.next
is the head node, and current.next.next
is the head+1 node. 线,
current
指向尾节点, current.next
是头节点, current.next.next
是头+1节点。 By executing the assignment, current.next
is made to point to the head+1 node, meaning that neither if(current.next == head)
nor else if (current == head)
will trigger. 通过执行分配,
current.next
指向head + 1节点,这意味着if(current.next == head)
和else if (current == head)
都不会触发。 The head node is now outside of the cyclic list, but the head
pointer still points to that node; 头节点现在在循环列表之外,但是
head
指针仍然指向该节点。 and worse, head.next
still points into the cyclic list. 更糟糕的是,
head.next
仍然指向循环列表。
Two more issues: 还有两个问题:
if(current.next.data == key )
is unnecessary because it is literally the stopping condition for the preceding while-loop. if(current.next.data == key )
是不必要的,因为它实际上是前一个while循环的停止条件。 I kept track of previous and current node and it worked! 我一直跟踪先前和当前的节点,并且它起作用了!
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.