使用head_return和tail_return从链表中查找和删除
Find and remove from linked lists using head_return and tail_return
我正在处理一个链表示例。我在查找和删除函数,它遍历列表以查找特定元素。find_remove函数不起作用。应用程序中的所有其他函数(head_return、tail_return等)
如果有人能指出我在find_remove中哪里出了问题,我将不胜感激。
#include <iostream>
using namespace std;
struct node_ll
{
int payload;
node_ll* next;//Pointer to the next node
};
void head_insert(node_ll** list, int pload)
{
node_ll* temp = new node_ll;//Create a new node, and let temp be the address of that node.
temp->payload = pload;//Set the payload of the struct whose address is temp to pload.
temp->next = *list;//Set the next of the struct whose address is temp to the address of the old head of the list.
*list = temp;//The address of the old head of the list is changed to the address of the struct temp.
};
void tail_insert(node_ll** list, int pload)
{
if (*list == NULL)
{
head_insert(list, pload);
}
else
{
node_ll* temp = new node_ll;
for (temp = *list; temp->next; temp = temp->next);
temp->next = new node_ll;
temp->next->payload = pload;
temp->next->next = NULL;
}
}
int head_return (node_ll** list)
{
if (*list != NULL)
{
int temp = (*list)->payload;
node_ll* trash = *list;
*list = (*list)->next;
delete trash;
return temp;
}
else
{
return 0;
}
}
int tail_return (node_ll** list)
{
if (*list != NULL)
{
if ((*list)->next == NULL)
{
return head_return(list);
}
else
{
node_ll* trash;
for (trash = *list; trash->next->next; trash = trash->next);
int temp = trash->next->payload;
delete trash->next;
trash->next = NULL;
return temp;
}
}
else
{
return 0;
}
}
void find_remove (node_ll** list, int pload)
{
if (*list != NULL)
{
node_ll* temp;//Declared before loop for use after loop.
for (temp = *list; temp->next; temp = temp->next)
{
if (temp->payload == pload)
{
int trash = head_return(&temp);
}
}
if (temp->payload == pload)
{
int trash = tail_return(list);
}
}
}
void print_ll (node_ll** list)
{
node_ll* temp = *list;//Let temp be the address of the node that is the head of the list.
while(temp)// != NULL
{
cout << temp->payload << endl;//Print out payload of the struct whose address is temp.
temp = temp->next;//Set the address of temp equal to the address stored in next of the struct whose address is temp.
}
}
int main()
{
node_ll *blist = NULL;
tail_insert(&blist, 2);
tail_insert(&blist, 4);
tail_insert(&blist, 6);
find_remove(&blist, 4);
print_ll(&blist);
cout << 'n';
system("PAUSE");
return 0;
}
编辑:
这是我重写你代码的尝试。
void find_remove (node_ll** list, int pload)
{
if (*list != NULL)
{
while (*list && (*list)->payload == pload)
{
head_return(list);
}
if (*list != NULL)
{
node_ll* temp;
for (temp = *list; temp->next->next; temp = temp->next)
{
if (temp->next->payload == pload)
{
node_ll* trash = temp->next;
temp->next = temp->next->next;
head_return(&trash);
}
}
}
}
}
编辑2:谢谢你!如果在开始处有多个匹配节点,在中间有多个匹配节点或在结束处有多个匹配节点,则此新解决方案有效。
void find_remove (node_ll** list, int pload)
{
if (*list != NULL)
{
while (*list && (*list)->payload == pload)
{
head_return(list);
}
if (*list != NULL)
{
node_ll* temp;
for (temp = *list; temp->next; temp = temp->next)
{
while (temp->next->next != NULL && temp->next->payload == pload)
{
node_ll* trash = temp->next;
temp->next = temp->next->next;
head_return(&trash);
}
}
if (temp->next == NULL && temp->payload == pload)
{
tail_return(list);
}
}
}
}
崩溃是由于你的循环逻辑不正确,你也没有保持跟踪前一个节点,这意味着你没有正确地重置链表,这是最简单的修复:
void find_remove (node_ll** list, int pload)
{
if (*list != NULL)
{
node_ll* temp, *prev=NULL;//Declared before loop for use after loop.
for (temp = *list; temp != NULL; temp = temp->next)
{
if (temp->payload == pload)
{
if( NULL != prev )
{
prev->next = temp->next ;
}
int trash = head_return(&temp);
}
prev=temp ;
}
}
}
虽然老实说,代码有很多风格问题,更像C风格的代码而不是c++风格的代码。你可以考虑查看Code Review。
更新:
如果你在修改后的代码中修改了它,它就可以工作了:
for (temp = *list; temp->next != NULL; temp = temp->next)
^^^^^^^^^^^^^^^^^^
在循环的顶部,您永远不知道temp->next
是否为NULL,因此执行temp->next->next
永远无效。
相关文章:
- 正在查找文档以获得PS4平台的C++中的设备信息
- 在C++中查找文件
- 模板元程序查找相似的连续类型名称
- 在UNIX系统中使用DIR查找文件的字节大小
- 查找最接近的大于当前数字的数字的索引
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 查找后更改类变量
- 使用正则表达式regex_search在字符串中查找字符串
- 使用gcc从静态链接的文件中查找可选符号
- 在C++中查找范围的长度
- 算法问题:查找从堆栈中弹出的所有序列
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 在Windows中查找扬声器输出的当前音量级别
- 如何在C++中使用X509证书模在令牌中查找私钥
- 使用.find函数在c++中查找字符和另一个字符之间的大小
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 在 for 循环中查找问题时遇到困难
- 如何在文件中查找字节序列
- 试图创建一个多线程程序来查找0-100000000之间的总素数
- 使用堆查找第K个最大元素的时间复杂性