P
PRadyut
in this code the delete function does not delete the last node of the
double linked list
the code
----------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct dnode
{
struct dnode *prev;
int data;
struct dnode *next;
};
void display(struct dnode *);
void add(struct dnode **, int );
void deletel(struct dnode **, int );
int main()
{
struct dnode *p=NULL;
add(&p, 2);
add(&p, 3);
add(&p, 4);
add(&p, 5);
add(&p, 7);
add(&p, 8);
add(&p, 9);
add(&p, 1);
add(&p, 10);
add(&p, 9);
display(p);
deletel(&p, 9);
deletel(&p, 9);
display(p);
getch();
return 0;
}
void add(struct dnode **s, int num)
{
struct dnode *p, *r;
p=*s;
r=(struct dnode*)malloc(sizeof(struct dnode));
r->data=num;
if(*s==NULL)
{
r->next=NULL;
r->prev=NULL;
*s=r;
(*s)->next=p;
}
else
{
while(p->next!=NULL)
p=p->next;
r->prev=p;
r->next=NULL;
p->next=r;
}
}
void display(struct dnode *s)
{
while(s)
{
printf("%d\n", s->data);
s=s->next;
}
printf("\n");
}
void deletel(struct dnode **s, int num)
{
struct dnode *p;
p=*s;
if(*s==NULL)
printf("list is empty");
else
{
while(p->next!=NULL)
{
if(p->data==num)
{
if(p==*s)
{
(*s)->prev=NULL;
*s=p->next;
free(p);
return;
}
else
{
if(p->next==NULL)
{
p->prev->next=NULL;
}
else
{
p->prev->next=p->next;
p->next->prev=p->prev;
}
free(p);
}
return;
}
p=p->next;
}
printf("element %d not found", num);
}
}
-----------------------------------------------------------
any help
thanks
Pradyut
http://pradyut.tk
http://groups.yahoo.com/group/d_dom/
http://groups-beta.google.com/group/oop_programming
India
double linked list
the code
----------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct dnode
{
struct dnode *prev;
int data;
struct dnode *next;
};
void display(struct dnode *);
void add(struct dnode **, int );
void deletel(struct dnode **, int );
int main()
{
struct dnode *p=NULL;
add(&p, 2);
add(&p, 3);
add(&p, 4);
add(&p, 5);
add(&p, 7);
add(&p, 8);
add(&p, 9);
add(&p, 1);
add(&p, 10);
add(&p, 9);
display(p);
deletel(&p, 9);
deletel(&p, 9);
display(p);
getch();
return 0;
}
void add(struct dnode **s, int num)
{
struct dnode *p, *r;
p=*s;
r=(struct dnode*)malloc(sizeof(struct dnode));
r->data=num;
if(*s==NULL)
{
r->next=NULL;
r->prev=NULL;
*s=r;
(*s)->next=p;
}
else
{
while(p->next!=NULL)
p=p->next;
r->prev=p;
r->next=NULL;
p->next=r;
}
}
void display(struct dnode *s)
{
while(s)
{
printf("%d\n", s->data);
s=s->next;
}
printf("\n");
}
void deletel(struct dnode **s, int num)
{
struct dnode *p;
p=*s;
if(*s==NULL)
printf("list is empty");
else
{
while(p->next!=NULL)
{
if(p->data==num)
{
if(p==*s)
{
(*s)->prev=NULL;
*s=p->next;
free(p);
return;
}
else
{
if(p->next==NULL)
{
p->prev->next=NULL;
}
else
{
p->prev->next=p->next;
p->next->prev=p->prev;
}
free(p);
}
return;
}
p=p->next;
}
printf("element %d not found", num);
}
}
-----------------------------------------------------------
any help
thanks
Pradyut
http://pradyut.tk
http://groups.yahoo.com/group/d_dom/
http://groups-beta.google.com/group/oop_programming
India