如何解决如何删除双链表中的尾部?
我的老师在这个活动中指导我们如何删除双链表的尾部。他为我们创建了一个循序渐进的过程或算法。我跟着它,但它不起作用。或者,也许我跟随它是错误的。这是算法
检查列表是否为空
- 如果不是空的
这是我的代码
public void delTail(){
DoubleNode temp;
if(isEmpty()){
return;
}
else if(!isEmpty()){
if(head == tail){
head = tail = null;
}
else{
temp = tail.next;
tail.prev = null;
temp.next = null;
temp = tail;
}
}
}
这是我看到的错误 error in my terminal
我认为我是否正确地遵循了它?非常感谢您的帮助:)
这是我的构造函数*
public class DoubleNode{
public DoubleNode prev;
public int data;
public DoubleNode next;
public DoubleNode(int d){
this(null,d,null);
}
public DoubleNode(DoubleNode p,int d,DoubleNode n){
prev = p;
data = d;
next = n;
}
}
这是完整的操作员代码*
public class operator{
DoubleNode head;
DoubleNode tail;
DoubleNode laman;
String output = "";
public operator(){
head = tail = null;
}
public boolean isEmpty(){
return head == null;
}
public void addHead(int i){
if(isEmpty()){
head = tail =new DoubleNode(i);
}
else{
head = new DoubleNode(null,i,head);
head.prev = head;
}
}
public void addTail(int i){
DoubleNode last = new DoubleNode(i);
if(isEmpty()){
head = tail = new DoubleNode(i);
}
else{
tail.next = last;
tail = last;
}
}
public void delHead(){
DoubleNode temp = head.next;
if(head==tail){ //this if condition is testing if the head and tail is one only,head = tail =null; //if there is only one this will set the tail and head to null
}
else{
head = head.next;
head = temp;
}
}
public void delTail(){
DoubleNode temp;
if(isEmpty()) {
return;
}
else {
if(head != tail) {
tail = tail.prev;
temp = tail;
}
else {
head = tail = null;
}
}
}
public void display(){
DoubleNode tmp = head;
output = "<html>";
for(tmp = head; tmp != null; tmp = tmp.next){
output = output + "<br>" + tmp.data + "<b>" + "<br>";
}
output = output + "</html>";
}
}
到目前为止,这是我的全部代码,我有一个带有 jframe 的主类,但我认为它很好,因为我也将它用于单个链接列表。但是我在双链接列表中确实有关于删除最后一个节点的问题
解决方法
您的问题是您只是分配给 temp
而实际上并未使用它。此外,您没有正确设置回前一个元素的链接。
假设 tail.next
再次指向 head
,您可能会执行以下操作:
tail.prev.next = tail.next; //you might need to check for `tail.prev` being null
tail.next.prev = tail.prev; //you might need to check for `tail.next` being null
//delete tail
举例说明:
A -next-> Tail -next-> Head
^---prev--+ ^---prev--+
第 1 步:
+----next--------------V
A Tail -next-> Head
^---prev--+ ^---prev--+
第 2 步:
+----next--------------V
A Tail -next-> Head
^---prev--+ |
^--------------prev----+
实际上,如果 tail.next == head
删除尾部与删除任何其他节点没有什么不同。
您的 else
块中有两个错误:
-
您永远不会更改
tail
引用。最后一条语句应该是对tail
的赋值。 -
您似乎假定
tail
具有非空的next
引用,但这是矛盾的。尾部应该是最后一个节点,因此它的next
引用将始终为空(除非您应该创建一个循环列表)。因此,temp
将为空,并且语句temp.next = tail
将触发空指针异常。tail
更有趣的属性是它的prev
属性,它指的是删除当前尾节点后将成为tail
的节点。此tail.prev
在您的作业中明确提及。
所以:
else {
temp = tail.prev; // <--- should point to the new tail
tail.prev = null;
temp.next = tail;
tail = temp; // <--- reverse the assignment
}
其他几个问题...
在 addHead
中,您没有正确设置 prev
属性。你让它成为一个自我参考。意识到 head
已经在引用新节点。更改:
head.prev = head;
到:
head.next.prev = head;
在 addTail
中缺少对 prev
的赋值。更改:
tail.next = last;
tail = last;
到:
tail.next = last;
last.prev = tail; // needed!
tail = last;
在 delHead
中,您必须确保新头部的 prev
为空。所以改变:
head = temp;
到:
head = head.next;
head.prev = null;
注意:该方法中不需要 DoubleNode temp = head.next;
。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。