主题:建一个双向链表
#include<stdio.h>
#include<stdlib.h>
struct list{
int data;
struct list *next;
struct list *prior;
};
int main()
{
struct list *p,*q;
p=(struct list *)malloc(sizeof(struct list));
p->prior=NULL;
p->next=NULL;
q=(struct list *)malloc(sizeof(struct list));
q=NULL;
int i,m;
printf("enter:\n");
for(i=0;i<=5;i++)
{
p->next=(struct list *)malloc(sizeof(struct list));
p->prior=(struct list *)malloc(sizeof(struct list));
scanf("%d",&m);
p->data=m;
if(i=5)
{
p->next=NULL;
p->prior=q;
p->prior->next=NULL;
}
else
{
q=p;
p->next->prior=q;
p=p->next;
p->prior=q;
}
}
while(p)
{
printf("%d->",p->data);
p=p->prior;
}
return 0;
}
做双向链表有点头晕,不知道这程序哪里出错了,敬请各位老大指教
#include<stdlib.h>
struct list{
int data;
struct list *next;
struct list *prior;
};
int main()
{
struct list *p,*q;
p=(struct list *)malloc(sizeof(struct list));
p->prior=NULL;
p->next=NULL;
q=(struct list *)malloc(sizeof(struct list));
q=NULL;
int i,m;
printf("enter:\n");
for(i=0;i<=5;i++)
{
p->next=(struct list *)malloc(sizeof(struct list));
p->prior=(struct list *)malloc(sizeof(struct list));
scanf("%d",&m);
p->data=m;
if(i=5)
{
p->next=NULL;
p->prior=q;
p->prior->next=NULL;
}
else
{
q=p;
p->next->prior=q;
p=p->next;
p->prior=q;
}
}
while(p)
{
printf("%d->",p->data);
p=p->prior;
}
return 0;
}
做双向链表有点头晕,不知道这程序哪里出错了,敬请各位老大指教