#include <stdio.h>
#include <stdlib.h>
typedef struct student
{
int id;
char name[32];
struct student* next;
}ST;
ST* create(ST* h) //头插创建链表;
{
ST* head = h;
ST* p =(ST*)malloc(sizeof(ST));
printf("请输入学号、姓名\n");
scanf("%d %s",&p->id,p->name);
if(head = NULL)
{
head = p;
p->next = NULL;
}
else
{
p->next = h;
head = p;
}
return head;
}
void write(ST* h)
{
ST* p= h;
FILE* fp=NULL;
fp = fopen("1.txt","w");
if(fp!=NULL)
{
printf("打开成功\n");
}
for(;p!=NULL;p=p->next)
{
fprintf(fp,"%d %s\n",p->id,p->name);
}
fclose(fp);
}
void print(ST* h)
{
ST* p = h;
printf("学号\t 姓名\t\n");
while(p!=NULL)
{
printf("%d\t %s\t\n",p->id,p->name);
p = p->next;
}
}
int main()
{
ST* head = NULL;
// head=read(head);
int n = 0;
printf("请输入要创建学生信息的个数\n");
scanf("%d",&n);
for(int i = 0;i<n ;i++)
{
printf("请输入第%d个学生的信息\n",i+1);
head = create(head);
}
print(head);
write(head);
}能否告知下,多谢啦!
添加回答
举报
0/150
提交
取消