gpt4 book ai didi

c - 如何检查链表中节点是否被删除

转载 作者:太空宇宙 更新时间:2023-11-04 08:37:21 27 4
gpt4 key购买 nike

嘿,请有人消除我的疑问,如果我删除了一个节点,稍后我尝试打印该节点的数据,输出应该是什么?如何查看节点是否被删除?

在这个程序中,我试图删除重复的节点以及如何知道我是否正确删除了它们!我想在删除一个节点后,如果我尝试访问该已删除节点的数据,如果一切正常,我将得到零!但是这里是这样,所以我又尝试了一次节点数,结果很好。计数是检查的唯一方法吗?

#include <stdio.h>
#include <stdlib.h>

typedef struct dll {
int data;
struct dll* next;
} dll;

int main() {
dll* p1, *p2, *p3, *p4, *p5, *temp, *head, *todel, *cur, *fwd, *dup;
int count = 0, i = 0, j = 0;

p1 = (dll*)malloc(sizeof(dll));
p2 = (dll*)malloc(sizeof(dll));
p3 = (dll*)malloc(sizeof(dll));
p4 = (dll*)malloc(sizeof(dll));
p5 = (dll*)malloc(sizeof(dll));

p1->data = 1;
p1->next = p2;

p2->data = 2;
p2->next = p3;

p3->data = 3;
p3->next = p4;

p4->data = 2;
p4->next = p5;

p5->data = 1;
p5->next = NULL;

head = p1;

printf("p1::%p\n", p1);
printf("p2::%p\n", p2);
printf("p3::%p\n", p3);
printf("p4::%p\n", p4);
printf("p5::%p\n", p5);
printf("head::%p\n", head);

for (temp = head; temp != NULL; temp = temp->next) {
count++;
}
printf("no of nodes %d\n", count);

temp = head;

cur = temp;

while (cur) {
for (fwd = cur->next; fwd != NULL; fwd = fwd->next) {
if (cur->data == fwd->data) {
cur->next = fwd->next;
// fwd->next=fwd->next->next;
todel = fwd;
free(todel);
fwd = cur;
}
}
cur = cur->next;
}

printf("p1::%p\n", p1);
printf("p2::%p\n", p2);
printf("p3::%p\n", p3);
printf("p4::%p\n", p4);
printf("p5::%p\n", p5);

printf("p1->data::%d\n", p1->data);
printf("p2 data::%d\n", p2->data);
printf("p3->data::%d\n", p3->data);
printf("p4->data::%d\n", p4->data);
printf("p5->data::%d\n", p5->data);

return 0;
}

输出:

p1::0x8728008
p2::0x8728018
p3::0x8728028
p4::0x8728038
p5::0x8728048
head::0x8728008
no of nodes 5
temp::0x8728008
cur::0x8728008
no of nodes 1
p1::0x8728008
p2::0x8728018
p3::0x8728028
p4::0x8728038
p5::0x8728048
p1->data::1
p2 data::2
p3->data::3
p4->data::2
p5->data::0

通过查看输出(节点数:1)我知道节点已被删除但不是很清楚,我什至不知道哪个节点被删除了,仍然是 p2p4 正在提供相同的旧数据值,我怎么知道?

最佳答案

您正在删除不正确的节点,在 for 循环内部,而 fwd 变量包含 repeated nodecur 变量包含原始节点(即重复)。您正在删除原始节点重复节点 之间的节点。为了仅删除重复的节点,您需要维护在此之前的节点并设置last_fwd->next = fwd->next;,这将仅删除重复的节点(例如: p4 和 p5,而不是 p1 或 p2 如果您也需要删除它,则需要更多工作)。

另一个问题是您使用指针打印节点,而不是创建的链表。在那种情况下,当您删除 p4p5 并尝试打印它们的值时,这是未定义的行为(任何事情都可能发生,工作,打印垃圾,重启机器 :D,等等。 .).

使用更新代码,打印是使用列表的链接进行的。在这种情况下,删除的节点不会被触摸。

代码:

#include <stdio.h>
#include <stdlib.h>

typedef struct dll {
int data;
struct dll* next;
} dll;

int main() {
dll* p1, *p2, *p3, *p4, *p5, *temp, *head, *todel, *cur, *fwd, *dup, *last_fwd;
int count = 0, i = 0, j = 0;

p1 = (dll*)malloc(sizeof(dll));
p2 = (dll*)malloc(sizeof(dll));
p3 = (dll*)malloc(sizeof(dll));
p4 = (dll*)malloc(sizeof(dll));
p5 = (dll*)malloc(sizeof(dll));

p1->data = 1;
p1->next = p2;

p2->data = 2;
p2->next = p3;

p3->data = 3;
p3->next = p4;

p4->data = 2;
p4->next = p5;

p5->data = 1;
p5->next = NULL;

head = p1;

printf("p1::%p\n", p1);
printf("p2::%p\n", p2);
printf("p3::%p\n", p3);
printf("p4::%p\n", p4);
printf("p5::%p\n", p5);
printf("head::%p\n", head);

for (temp = head; temp != NULL; temp = temp->next) {
count++;
}
printf("no of nodes %d\n", count);

temp = head;

cur = temp;

while (cur) {
last_fwd = cur;
for (fwd = cur->next; fwd != NULL; fwd = fwd->next) {
if (cur->data == fwd->data) {
last_fwd->next = fwd->next;
todel = fwd;
free(todel);
}
last_fwd = fwd;
}
cur = cur->next;
}

for (count = 0, temp = head; temp != NULL; temp = temp->next, count++) {
printf("p%d::%p\n", count, temp);
printf("p%d->data::%d\n", count, temp->data);
}

return 0;
}

关于c - 如何检查链表中节点是否被删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25671502/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com