double linked list delete problem

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
 
P

pete

PRadyut wrote:
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);
}
}

/* Use 4 spaces instead of tabs */

void deletel(struct dnode **s, int num)
{
struct dnode *n;

n = *s;
if (n == NULL) {
puts("list is empty");
} else {
if (n -> data == num) {
*s = n -> next;
free(n);
if (*s != NULL) {
(*s) -> prev = NULL;
}
} else {
n = n -> next;
while (n != NULL) {
if (n -> data == num) {
n -> prev -> next = n -> next;
if (n -> next != NULL) {
n -> next -> prev = n -> prev;
}
free(n);
n = *s;
break;
} else {
n = n -> next;
}
}
if (n == NULL) {
printf("element %d not found\n", num);
}
}
}
}
 
S

schwarzb

in this code the delete function does not delete the last node of the
double linked list

Did you mean the deletel function?
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));

You should not cast the return from malloc. It can rarely help and
frequently hurt. But you should check for success before you
dereference the pointer.
r->data=num;
if(*s==NULL)

At this point, we know *s is NULL. That means p is NULL also.
{
r->next=NULL;
r->prev=NULL;
*s=r;

At this point *s is the same as r. r->next is NULL so (*s)->next must
be also. p is still NULL.
(*s)->next=p;

Why are you setting (*s)->next to NULL when it already is?
}
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)

Here is your problem. If num is the value in the last p->data, then
the corresponding p->next must be NULL and you will not enter the
following code. You need to rework this into the form
while (p!=NULL)
{
if(p->data==num)
{
if(p==*s)

In general, it is not a robust design to assume that *s points to the
head of the list. A user might want to "delete the node with num if it
occurs after the node I give you". But it is true in the code you
posted.
{
(*s)->prev=NULL;
*s=p->next;
free(p);
return;
}
else
{
if(p->next==NULL)

Based on your incorrect while, this cannot be. After you fix the
while, this is code you need.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,246
Members
46,840
Latest member
BrendanG78

Latest Threads

Top