#include?<stdio.h>
#include?<stdlib.h>
typedef?struct?student?
{
int?id;
char?name[32];
struct?student*?next;
}ST;
ST*?create(ST*?h) //頭插創(chuàng)建鏈表;
{
ST*?head?=?h;
ST*?p?=(ST*)malloc(sizeof(ST));
printf("請(qǐng)輸入學(xué)號(hào)、姓名\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("學(xué)號(hào)\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("請(qǐng)輸入要?jiǎng)?chuàng)建學(xué)生信息的個(gè)數(shù)\n");
scanf("%d",&n);
for(int?i?=?0;i<n?;i++)
{
printf("請(qǐng)輸入第%d個(gè)學(xué)生的信息\n",i+1);
head?=?create(head);
}
print(head);
write(head);
}能否告知下,多謝啦!
- 1 回答
- 0 關(guān)注
- 1970 瀏覽
添加回答
舉報(bào)
0/150
提交
取消