如何在链表中检查节点是否被删除 [英] How to check if the node is deleted or not in linked list

查看:70
本文介绍了如何在链表中检查节点是否被删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



Hey please someone clear my doubt, if I delete a node and later I try to print the data of that node what should be the output? How to check if the node is deleted or not?

In this program I am trying to delete duplicate nodes and how to know if I deleted them correctly! I thought after deleting a node if I try to access that deleted node's data I will get zero if everything went correctly! But it's some garbage here, so I tried counting nodes again and it's good. Is counting the only way to check?

#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 = 1;
p2->next = p3;

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

p4->data = 1;
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;

    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;
        }
        else
        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;
}





输出:



Output:

p1::0x9daa008
p2::0x9daa018
p3::0x9daa028
p4::0x9daa038
p5::0x9daa048
head::0x9daa008

no of nodes 5

no of nodes 1

p1::0x9daa008
p2::0x9daa018
p3::0x9daa028
p4::0x9daa038
p5::0x9daa048

p1->data::1
p2->data::0
p3->data::165322768
p4->data::165322784
p5->data::165322800

推荐答案

引用:

但这里有些垃圾,所以我再次尝试计算节点,这很好。是否计算唯一的检查方法?

But it's some garbage here, so I tried counting nodes again and it's good. Is counting the only way to check?



计数很好(你不应该对相关内存做任何假设)。但是,删除重复项的代码部分对我来说是错误的。这是(以我的拙见)正确的一个:


Counting is fine (you shouldn't make any assumption on relesed memory). However, the part of your code that removes duplicates looks wrong to me. This is (in my humble opinion) the correct one:

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;
  }


否:从链接列表中删除节点时,将其从列表中完全删除:就像撕掉一个页面一样一个三环活页夹 - 下一个看起来赢了的人; t注意它不存在,除非他试图按照页面到页面的文字。



一旦你删除它,你不能通过遍历列表再次找到它。
No: when you delete a node from a linked list, you remove it from the list completely: it's like tearing a page out of a three-ring binder - the next person to look won;t notice it isn't there unless he tries to follow the text from page to page.

Once you have deleted it, you can't "find" it again by traversing the list.


这篇关于如何在链表中检查节点是否被删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆