[英]Java - Manual linked list, removing current node
So I've implemented a linked list from scratch and am trying to remove the current node (cursor). 因此,我从头开始实现了一个链表,并试图删除当前节点(光标)。 When I run the program and try to remove the current node, I don't receive any errors, but then I'll try to print the current node (which should now be the next or previous) and it prints the one that should've been removed.
当我运行该程序并尝试删除当前节点时,我没有收到任何错误,但是我将尝试打印当前节点(现在应该是下一个或上一个),并且会打印应该的那个节点。已被删除。
First of all, this line makes no sense: 首先,这行没有意义:
// ...
}else{
cursor = cursor.getPrev().getNext(); // THIS LINE - all you say here is 'cursor = cursor'
cursor = cursor.getNext();
}
// ...
You probably wanted to disconnect the previous node from pointing to cursor and make it point to node after cursor: 您可能想断开上一个节点的指向光标的连接,使其指向光标之后的节点:
// get previous node and set its next to node after cursor
cursor.getPrev().setNext(cursor.getNext());
In this part: 在这一部分:
if(cursor.getNext() == null){ //it's the tail
tail = cursor.getPrev();
}
You never disconnect tail.next
by saying tail.next = null
so your tail.next
will point to cursor
after you update it. 您永远不会通过说
tail.next = null
断开tail.next
的连接,因此您的tail.next
将在更新后指向cursor
。
And then this line: 然后这行:
else{
cursor = cursor.getNext().getPrev(); // again no effect
cursor = cursor.getPrev();
}
Should look like: 应该看起来像:
// get next node and set its prev to node before cursor
cursor.getNext().setPrev(cursor.getPrev());
Overall, your logic seems much more complicate then it should be. 总体而言,您的逻辑似乎比应该的复杂得多。 Here is one way of simplifying your code but not changing your logic (still use cursor node)
这是一种简化代码但不更改逻辑的方法(仍然使用游标节点)
You can just reorder your if
statements a little bit to make things clearer. 您可以稍微调整一下
if
语句的顺序,使事情变得更清楚。 You should check the edge cases (head and tail) first, then the rest: 您应该先检查边缘情况(头和尾),然后再检查其余情况:
if (cursor != null){
if(cursor.getPrev() == null){ //it's the head
head = cursor.getNext();
head.setPrev(null); // disconnect the head from current node
} else if (cursor.getNext() == null) { // it's the tail
tail = cursor.getPrev();
tail.setNext(null); // disconnect the tail from current node
} else { // regular node
Node prev = cursor.getPrev();
prev.setNext(next); // connect previous node to next node
Node next = cursor.getNext();
next.setPrev(prev); // connect next node to previous node
}
// this part isn't necessary because we are skipping the cursor node
// so nothing in the list references to it anymore
// however it is a good safety measure and it helps the GC a bit
cursor.setPrev(null); // disconnect cursor from previous node
cursor.setNext(null; // disconnect cursor from next node
}
I left out the updating of cursor because there is an ambigous situation when cursor is on a middle node and you remove it. 我省略了游标的更新,因为当游标位于中间节点上并且将其删除时,存在很多情况。 The problem is how do you decide to update the cursor to
prev
or to next
? 问题是你如何决定更新光标到
prev
或next
?
You don't really need the cursor but I've already congested this answer a lot so i will give you this link
and this link
to check it out for some good ideas. 您实际上并不需要光标,但是我已经把这个答案塞满了,所以我会给您
this link
和this link
以查看一些好的想法。
As far as formatting your long-prints: 至于格式化长印:
If you are using Eclipse, you can use Ctrl-Shift-F
on Windows or Cmd-Shift-F
on Mac to automatically format your code :) 如果您使用的是Eclipse,则可以在Windows上使用
Ctrl-Shift-F
或在Mac上使用Cmd-Shift-F
来自动设置代码格式:)
I'm suspicious that your call to 我怀疑您打来的电话
cursor = cursor.getPrev().getNext();
(assuming cursor is the element in your list you want to delete) isn't doing anything, as cursor
should already == cursor.getPrev().getNext()
(假设光标是您要删除的列表中的元素)没有做任何事情,因为
cursor
应该已经 == cursor.getPrev().getNext()
What I suspect you want to do is 我怀疑你想做的是
cursor.getPrev().setNext(cursor.getNext()); // note SET instead of GET
cursor.getNext().setPrev(cursor.getPrev());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.