I am trying this without a head/start pointer which would generally hold the address of first node. I have 3 nodes here from which I am trying to delete last node, but its not happening. I might be wrong in my logic and this is my first linked list program, so please help me!
output::::
add of p1::0x9605008 add of p2::0x9605018 add of p3::0x9605028 add of p1->prev::(nil) add of p1->next::0x960501 8 add of p2->prev::0x960500 8 add of p2->next::0x960502 8 add of p3->prev::0x960501 8 add of p3->next::(nil)
no of nodes 3
enter the addresss of node to delete it
0x9605028
after deletion attempted
add of p1::0x9605028 add of p2::0x9605018 add of p3::0x9605028 add of p1->prev::0x960501 8 add of p1->next::(nil) add of p2->prev::0x960500 8 add of p2->next::0x960502 8 add of p3->prev::0x960501 8 add of p3->next::(nil)
no of nodes 3
In this example i am trying to delete the node 3 which is p3, by deleting its address 0x9605028. But after deletion the node count is still 3 and addresses are like unexpected!
Code:
#include <stdio.h>
#include <stdlib.h>
struct dll{
struct dll *prev;
int data;
struct dll *next;
};
int main()
{
struct dll *p1,*p2,*p3, *temp;
p1=malloc(sizeof(struct dll));
p2=malloc(sizeof(struct dll));
p3=malloc(sizeof(struct dll));
temp=malloc(sizeof(struct dll));
p1->prev=NULL;
p1->data=1;
p1->next=p2;
p2->prev=p1;
p2->data=2;
p2->next=p3;
p3->prev=p2;
p3->data=3;
p3->next=NULL;
struct dll *add=NULL;
int count=0;
printf("add of p1::%p add of p2::%p add of p3::%p add of p1->prev::%p add of p1->next::%p add of p2->prev::%p add of p2->next::%p add of p3->prev::%p add of p3->next::%p\n",p1,p2,p3,p1->prev,p1->next,p2->prev,p2->next,p3->prev,p3->next);
while(p1->next!=NULL)
{
count++;
p1=p1->next;
}
printf("no of nodes %d\n",count+1);
puts("enter the addresss of node to delete it");
scanf("%p",&add);
while(p1->next!=NULL)
{
if(p1->next==add)
{
temp = p1->next;
p1->next=NULL;
free(temp);
temp=NULL;
}
else
p1=p1->next;
}
puts("after deletion attempted");
printf("add of p1::%p add of p2::%p add of p3::%p add of p1->prev::%p add of p1->next::%p add of p2->prev::%p add of p2->next::%p add of p3->prev::%p add of p3->next::%p\n",p1,p2,p3,p1->prev,p1->next,p2->prev,p2->next,p3->prev,p3->next);
while(p1->next!=NULL)
{
count++;
p1=p1->next;
}
printf("no of nodes %d\n",count+1);
free(p1);
p1=NULL;
free(p2);
p2=NULL;
free(p3);
p3=NULL;
return 0;
}
add of p1::0x9605008 add of p2::0x9605018 add of p3::0x9605028 add of p1->prev::(nil) add of p1->next::0x960501 8 add of p2->prev::0x960500 8 add of p2->next::0x960502 8 add of p3->prev::0x960501 8 add of p3->next::(nil)
no of nodes 3
enter the addresss of node to delete it
0x9605028
after deletion attempted
add of p1::0x9605028 add of p2::0x9605018 add of p3::0x9605028 add of p1->prev::0x960501 8 add of p1->next::(nil) add of p2->prev::0x960500 8 add of p2->next::0x960502 8 add of p3->prev::0x960501 8 add of p3->next::(nil)
no of nodes 3
In this example i am trying to delete the node 3 which is p3, by deleting its address 0x9605028. But after deletion the node count is still 3 and addresses are like unexpected!
Comment