P
PRadyut
In this code i tried to add the elements in ascending order
but the output is only
0
1
2
the rest of the elements are not shown. 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 append(struct dnode **, int );
void deletel(struct dnode **, int );
void reverse(struct dnode **);
int main()
{
struct dnode *p=NULL;
append(&p, 2);
append(&p, 5);
append(&p, 7);
append(&p, 6);
append(&p, 8);
append(&p, 9);
append(&p, 3);
append(&p, 1);
append(&p, 4);
append(&p, 0);
display(p);
/*deletel(&p, 0);
display(p);
reverse(&p);
display(p);*/
getch();
return 0;
}
void display(struct dnode *p)
{
while(p)
{
printf("%d\n", p->data);
p=p->next;
}
printf("\n");
}
void deletel(struct dnode **s, int num)
{
struct dnode *p;
p=*s;
if(*s==NULL)
printf("empty list");
else
{
while(p!=NULL)
{
if(p->data==num)
{
if(p==*s)
{
*s=p->next;
(*s)->prev=NULL;
free(p);
return;
}
else
{
if(p->next==NULL)
{
p->prev->next=NULL;
}
else
{
p->next->prev=p->prev;
p->prev->next=p->next;
}
free(p);
return;
}
}
p=p->next;
}
}
}
void reverse(struct dnode **x)
{
struct dnode *p, *q, *r=NULL;
p=*x;
while(p)
{
q=r;
r=p;
p=p->next;
r->next=q;
}
*x=r;
}
void append(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->prev=NULL;
// r->next=NULL;
// *s=r;
// return;
//}
//else if(p->data>num)
//{
// r->next=NULL;
// r->prev=p;
// p=r;
// return;
// /**s=r;
// (*s)->next=p;*/
//}
if(*s==NULL || p->data>num)
{
r->next=NULL;
r->prev=NULL;
*s=r;
(*s)->next=p;
}
else
{
while(p->next!=NULL)
{
if(p->next->data>num)
{
/*p->prev->next=r;
p->next->prev=r;*/
r->prev=p->prev;
r->next=p->next;
//p=r;
return;
}
p=p->next;
}
/*r->next=NULL;
r->prev=p;
p=r;*/
}
}
--------------------------------------------------------------
anyt help
thanks
Pradyut
http://pradyut.tk
http://groups.yahoo.com/group/d_dom/
http://groups-beta.google.com/group/oop_programming
India
but the output is only
0
1
2
the rest of the elements are not shown. 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 append(struct dnode **, int );
void deletel(struct dnode **, int );
void reverse(struct dnode **);
int main()
{
struct dnode *p=NULL;
append(&p, 2);
append(&p, 5);
append(&p, 7);
append(&p, 6);
append(&p, 8);
append(&p, 9);
append(&p, 3);
append(&p, 1);
append(&p, 4);
append(&p, 0);
display(p);
/*deletel(&p, 0);
display(p);
reverse(&p);
display(p);*/
getch();
return 0;
}
void display(struct dnode *p)
{
while(p)
{
printf("%d\n", p->data);
p=p->next;
}
printf("\n");
}
void deletel(struct dnode **s, int num)
{
struct dnode *p;
p=*s;
if(*s==NULL)
printf("empty list");
else
{
while(p!=NULL)
{
if(p->data==num)
{
if(p==*s)
{
*s=p->next;
(*s)->prev=NULL;
free(p);
return;
}
else
{
if(p->next==NULL)
{
p->prev->next=NULL;
}
else
{
p->next->prev=p->prev;
p->prev->next=p->next;
}
free(p);
return;
}
}
p=p->next;
}
}
}
void reverse(struct dnode **x)
{
struct dnode *p, *q, *r=NULL;
p=*x;
while(p)
{
q=r;
r=p;
p=p->next;
r->next=q;
}
*x=r;
}
void append(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->prev=NULL;
// r->next=NULL;
// *s=r;
// return;
//}
//else if(p->data>num)
//{
// r->next=NULL;
// r->prev=p;
// p=r;
// return;
// /**s=r;
// (*s)->next=p;*/
//}
if(*s==NULL || p->data>num)
{
r->next=NULL;
r->prev=NULL;
*s=r;
(*s)->next=p;
}
else
{
while(p->next!=NULL)
{
if(p->next->data>num)
{
/*p->prev->next=r;
p->next->prev=r;*/
r->prev=p->prev;
r->next=p->next;
//p=r;
return;
}
p=p->next;
}
/*r->next=NULL;
r->prev=p;
p=r;*/
}
}
--------------------------------------------------------------
anyt help
thanks
Pradyut
http://pradyut.tk
http://groups.yahoo.com/group/d_dom/
http://groups-beta.google.com/group/oop_programming
India