C语言高手进!关于结构体链表的文件写入与读取问题

C语言中怎样将一个结构体链表写入指定文件!又怎样将他读出来?最好给个列子..谢谢

第1个回答  2013-09-18
链表的建立与插入一个数,你看下吧。#include<stdio.h>
#include<malloc.h>
#define N '\0'
struct number
{
int num;
struct number *next;
};
typedef struct number ST;
ST *A()
{
int x;
ST *h,*s,*r;
h=(ST *)malloc(sizeof(ST));
r=h;
scanf("%d",&x);
while(x!=-1)
{
s=(ST *)malloc(sizeof(ST));
s->num=x;
r->next=s;
r=s;
scanf("%d",&x);
}
r->next=N;
return h;
}
void B(ST *h)
{
ST *p;
p=h->next;
if(p==N)
printf("NOT!\n");
else
{
do
{
printf("%d ",p->num);
p=p->next;
}while(p!=N);
printf("\n");
}
}
void C(ST *h,int x,int y)
{
ST *s,*p,*q;
s=(ST *)malloc(sizeof(ST));
s->num=x;
p=h;
q=h->next;
while(q!=N&&q->num!=y)
{
p=q;
q=q->next;
}
p->next=s;
s->next=q;
}
void D(ST *h)
{
ST *p;
p=h->next;
while(p!=N)
{
printf("%d ",p->num);
p=p->next;
}
}
void main()
{
ST *head;
head=A();
B(head);
int x,y;
scanf("%d%d",&x,&y);
C(head,x,y);
D(head);}
第2个回答  2013-09-18
这个简单啊。
相似回答