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