Click here to Skip to main content
15,891,811 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?

Objective-C
#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:
CSS
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
Posted

Quote:
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:
C
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;
  }
 
Share this answer
 
Comments
Jeevan83 4-Sep-14 13:06pm    
Mmmm hey thanks for the correction, but what is the best way to know if they are deleted, please I need one badly!
CPallini 4-Sep-14 13:16pm    
Once the 'deleting code' works, you can trust the count of remaining nodes. In any case, you may log the delete operation.
Jeevan83 4-Sep-14 15:01pm    
Thank you!:)
CPallini 4-Sep-14 15:41pm    
You are welcome.
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.
 
Share this answer
 
Comments
Jeevan83 4-Sep-14 13:07pm    
So what is the best way to know if they are deleted, please I need one badly! I mean there would be some way right! If you were me how would you check if the work is done?
OriginalGriff 4-Sep-14 14:07pm    
Counting should work - once you are confident of the delete operation.
I'd test the delete by sorting the list and looking for consecutive matching values.
Jeevan83 4-Sep-14 15:00pm    
Thanks! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900