|
本题添加时间:2023/4/3 12:59:00 |
|
圆梦客服:王老师 19139051760(微信同号) 19139051760(微信同号) |
|
3.建立一个链表,每个结点包括:学号、姓名、年龄。数据从键盘上输入, 输入的学号为0时结束输入。链表建立后,将该链表中数据输出到屏幕上并存放到文件student.dat中。
|
答案是:#include "stdio.h" #define LEN sizeof(struct student) struct student { long num; char name[8]; int age; struct student *next; }; struct student *creat(void) { struct student *p1,*p2,*head; int n=0; p1=p2=(struct student *)malloc(LEN); printf("请输入学号:"); scanf("%ld",&p1->num); head=NULL; while(p1->num!=0) { printf("请输入姓名:"); scanf("%s",p1->name); printf("请输入年龄:"); scanf("%d",&p1->age); n++; if(n==1) head=p1; else p2->next=p1; p2=p1; p1=(struct student *)malloc(LEN); printf("请输入学号:"); scanf("%ld",&p1->num); } free(p1); if(head) p2->next=NULL; return head; } void print(struct student *head) { struct student *p=head; while(p) { printf("\n姓名:%s,学号:%ld,年龄:%d",p->name,p->num,p->age); p=p->next; } } void save(struct student *head) { struct student *p=head; FILE *fp; fp=fopen("stud","wb"); for(;p;) { if(fwrite(p,sizeof(struct student),1,fp)!=1) printf("File write error\n"); p=p->next; } fclose(fp); } struct student *access(void) { struct student *p1,*p2,*head=NULL; FILE *fp; fp=fopen("stud","rb"); int n=0; p1=p2=(struct student *)malloc(LEN); while(1) { if(fread(p1,sizeof(struct student),1,fp)!=1) printf("File read error\n"); n++; if(n==1) head=p1; else p2->next=p1; p2=p1; if(p1->next==NULL) break; p1=(struct student *)malloc(LEN); } if(head) p2->next=NULL; return head; } void main(void) { struct student *head; printf("请输入各结点数据:\n"); head=creat(); if(head) { print(head); save(head); free(head); head=access(); print(head); } }
出自
中北大学-C语言程序设计 学起plus弘成系统
中北大学
|
更多试题>>>>
1、2.用选择法或冒泡法对输入的十个字符(按ASCII码由小到大)进行排序。
2、1.用递归方法将一个整数N转换成字符串。例如,输入483,应输出字符串“483”。N的位数不确定,可以是任意位数的整数。
3、下列程序用来创建一个学生数据的单向链表。创建中如输入的学号为零,则创建结束。#define LEN sizeof (struct student)
struct student
{
long num;
float
4、下列程序将两个升序数组a、b合并到数组c中后,c仍为升序数组。
main()
{
int a[10]={1,2,5,8,9,10}, int b[10]={1,3,4,8,12,18};
int j,k,l,c[2
5、判断m是不是素数。算法:如m能被2~m-1中的任何一个数整除,则不是素数。
main( )
{
int m,i;
scanf("%d",&m);
if(m<=2)
{printf(“\n%d是素数
|
|