双向链表 - 无法删除第一个节点

Doubly Linked List - cannot in delete the first Node

本文关键字:第一个 节点 删除 双向链表      更新时间:2023-10-16
struct Node
{
int data;
Node *next;
Node *prev;
};
class DoublyLinkedList
{
ofstream cout3;
Node *head; 
public:
DoublyLinkedList()
{
head = NULL;
cout3.open("task3.out");
}
void insert(int num)
{
Node *temp = new Node;
//To insert if there are no elements
if(head == NULL)
{
temp->prev = NULL;
temp->data = num;
temp->next = NULL;
head = temp;
}
//To insert if there are elements
else
{
temp->prev = NULL;
temp->data = num;
temp->next = head;
head->prev = temp;
head = temp;
}       
cout3<<"inserted "<<num<<endl;      
}
void dele(int num)
{
Node *temp = head;
int found_num = 0;
while(temp != NULL)
{
if(temp->data == num)
{
found_num = 1;
break;
}   
else
temp = temp->next;
}
if(found_num == 0)
cout3<<"cannot delete "<<num<<endl;
//To delete first element
else if (temp->prev == NULL)
{
head = temp->next;
(temp->next)->prev == NULL;
delete temp;            
cout3<<"deleted "<<num<<endl;
}
//To delete last element
else if (temp->next == NULL)
{
(temp->prev)->next = NULL;
cout3<<"deleted "<<num<<endl;
delete temp;
}
//To delete any other element
else
{
(temp->prev)->next = temp->next;
(temp->next)->prev = temp->prev;
cout3<<"deleted "<<num<<endl;
delete temp;
}
}
void search(int num)
{
Node *temp = head;
int found_num = 0;
while(temp != NULL)
{
if(temp->data == num)
{
found_num = 1;
break;
}   
else
temp = temp->next;
}
if(found_num == 0)
cout3<<"not found "<<num<<endl;
else
cout3<<"found "<<num<<endl;
}
void display()
{
Node *temp = head;
while(temp != NULL)
{
cout3<<temp->data<<" ";
temp = temp->next;
}
cout3<<endl;
}
};

我对双向链表的实现。 我只在开头插入并删除数字的第一次出现。 但是,如果我想删除第一个元素,那么它会打印"已删除的数字",但是当我显示数字时,该数字仍然存在。 问题似乎出在我的删除功能中,但我找不到它是什么

请参阅此行:(temp->next(->prev == NULL; 你写的是==而不是=,这似乎是问题所在。 您没有显示如何打印值,但我猜测您在开始之前向后移动直到空值。.

只需花费代码来测试它,它就会发出警告。修复它,然后程序将按预期运行。

$ g++ test.cpp 
test.cpp:66:30: warning: equality comparison result unused
[-Wunused-comparison]
(temp->next)->prev == NULL;
~~~~~~~~~~~~~~~~~~~^~~~~~~
test.cpp:66:30: note: use '=' to turn this equality comparison into an
assignment
(temp->next)->prev == NULL;
^~
=
1 warning generated.

测试.cpp

#include <iostream>
#include <fstream>
struct Node
{
int data;
Node *next;
Node *prev;
};
class DoublyLinkedList
{
std::ofstream cout3;
Node *head; 
public:
DoublyLinkedList()
{
head = NULL;
cout3.open("task3.out");
}
void insert(int num)
{
Node *temp = new Node;
//To insert if there are no elements
if(head == NULL)
{
temp->prev = NULL;
temp->data = num;
temp->next = NULL;
head = temp;
}
//To insert if there are elements
else
{
temp->prev = NULL;
temp->data = num;
temp->next = head;
head->prev = temp;
head = temp;
}       
cout3<<"inserted "<<num<<std::endl;      
}
void dele(int num)
{
Node *temp = head;
int found_num = 0;
while(temp != NULL)
{
if(temp->data == num)
{
found_num = 1;
break;
}   
else
temp = temp->next;
}
if(found_num == 0)
cout3<<"cannot delete "<<num<<std::endl;
//To delete first element
else if (temp->prev == NULL)
{
head = temp->next;
(temp->next)->prev == NULL;
delete temp;            
cout3<<"deleted "<<num<<std::endl;
}
//To delete last element
else if (temp->next == NULL)
{
(temp->prev)->next = NULL;
cout3<<"deleted "<<num<<std::endl;
delete temp;
}
//To delete any other element
else
{
(temp->prev)->next = temp->next;
(temp->next)->prev = temp->prev;
cout3<<"deleted "<<num<<std::endl;
delete temp;
}
}
void search(int num)
{
Node *temp = head;
int found_num = 0;
while(temp != NULL)
{
if(temp->data == num)
{
found_num = 1;
break;
}   
else
temp = temp->next;
}
if(found_num == 0)
cout3<<"not found "<<num<<std::endl;
else
cout3<<"found "<<num<<std::endl;
}
void display()
{
Node *temp = head;
while(temp != NULL)
{
cout3<<temp->data<<" ";
temp = temp->next;
}
cout3<<std::endl;
}
};
int main()
{
DoublyLinkedList list;
list.insert(3);
list.insert(4);
list.insert(5);
list.display();
list.dele(3);
list.display();  
}

在下面的链接中,有一个释放内存的指南。Delete功能不起作用,大多数情况下,您可以使用free方法。

https://www.geeksforgeeks.org/write-a-function-to-delete-a-linked-list/