c语言怎么把一个双向链表写入文件

用c语言这一个双向链表,然后用fwrite写入文件,用fread读取,这个过程和单链表是一样的吗?

把数据存入到文件中是一样的,只是读取的时候需要双向关联

写文件:
#include
#include
typedef struct D{
char name[20];
char ID[10];
char card[10];
char MI[7];
float money;
struct D *next;
struct D *prior;
}*U,u;
void creat(U *head){
U p,h;
h=*head;
int n;
printf("1.继续 2.退出\n");
scanf("%d",&n);
while(n==1){
p=(U)malloc(sizeof(u));
scanf("%s",&p->name);
scanf("%s",&p->card);
scanf("%s",&p->ID);
scanf("%s",&p->MI);
p->money=0;
if(h->next==NULL){
h->next=p;
h->prior=p;
p->next=h;
p->prior=h;
}
else{
h->next->prior=p;
p->next=h->next;
p->prior=h;
h->next=p;
}
printf("1.继续 2.退出\n");
scanf("%d",&n);
}
}
int main(){
U head,p;
head=(U)malloc(sizeof(u));
head->next=head->prior=NULL;
creat(&head);
p=head->next;
FILE *s;
s=fopen("链表.txt","w+");
while(p!=head){
fwrite(p,sizeof(U),1,s);
p=p->next;
}
fclose(s);
return 0;
}
读取文件:
#include
#include
typedef struct D{
char name[20];
char ID[10];
char card[10];
char MI[7];
float money;
struct D *next;
struct D *prior;
}*U,u;
void DUQU(U *head){
U p,h;
h=*head;
FILE *s;
s=fopen("链表.txt","r+");
while(!feof(s)){
p=(U)malloc(sizeof(u));
fread(p,sizeof(U),1,s);
if(h->next==NULL){
h->next=p;
h->prior=p;
p->next=h;
p->prior=h;
}
else{
h->next->prior=p;
p->next=h->next;
p->prior=h;
h->next=p;
}
}
fclose(s);
}
int main(){
U head,p;
head=(U)malloc(sizeof(u));
head->next=head->prior=NULL;
DUQU(&head);
p=head->next;
while(p!=head){
printf("%s\n",p->name);
printf("%s\n",p->card);
printf("%s\n",p->ID);
printf("%s\n",p->MI);
p=p->next;
}
}