Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > double linked list delete problem

Reply
Thread Tools

double linked list delete problem

 
 
PRadyut
Guest
Posts: n/a
 
      06-08-2005
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, ;
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

 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      06-08-2005
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);
}
}
}
}

--
pete
 
Reply With Quote
 
 
 
 
schwarzb@oz.net
Guest
Posts: n/a
 
      06-09-2005
On 8 Jun 2005 06:59:44 -0700, "PRadyut" <(E-Mail Removed)> wrote:

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

> {
> 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);
> }
>}


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Linked list within a linked list joshd C++ 12 10-02-2006 08:57 AM
Linked list, New try (was:Linked list, no out put,help) fool C Programming 14 07-03-2006 12:29 AM
cannot convert parameter from 'double (double)' to 'double (__cdecl *)(double)' error Sydex C++ 12 02-17-2005 06:30 PM
Generating a char* from a linked list of linked lists Chris Ritchey C++ 7 07-10-2003 10:12 PM
Generating a char* from a linked list of linked lists Chris Ritchey C Programming 7 07-10-2003 10:12 PM



Advertisments