双向链表 - 无法删除第一个节点
Doubly Linked List - cannot in delete the first Node
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/
相关文章:
- 只有我的"链接列表"中的第一个节点正在打印
- 双向链表 - 无法删除第一个节点
- 链表中头/第一个节点的声明
- 链表.如何删除列表中的第一个节点?
- 链接列表正在抛出异常,而不是打印第一个节点
- 如何删除第一个节点并将其插入链接列表C 的末尾
- 为什么我的函数没有打印出双向链表中的第一个节点?
- 这是在双链表的第一个节点之后插入节点的正确方法吗?
- 返回在函数中创建的 std::list 的第一个节点
- 从链表的第一个节点中删除时出现段错误
- 如何将第一个节点移至链接列表C 中的列表末尾
- 创建第一个节点.js插件
- 在列表末尾插入具有第一个节点的数据的节点
- 访问stl列表的第一个节点
- RapidXML 解析卡在第一个节点C++上
- 在循环双向链表的第一个节点之前/之后插入的算法是什么?
- 如何在 c++ 中删除链表的第一个节点
- 如何删除双链接列表中的第一个节点
- 为什么将链表的第一个节点声明为指针
- 二叉搜索树无法删除第一个节点