课程设计的目的和要求目的:用链表对数据进行操作要求:读一个文件把各记录读入链表各结点中;对链表进行添加\删除操作;把链表的每个结点元素存储在该文件中.课程设计任务内容题1.读一个文件把各记录读入链表各结点中;对链表进行添加\删除操作;把链表的每个结点元素存储在该文件中.程序流程图开始读取文件记录菜单选择1.打印链表(print()函数)2.删除链表元素(deletes()函数)3.插入链表元素(insert()函数)4.结束开 始P=headP~尾节点结束输出p 指向的节点P 指向下一个节点是表尾?开 始P=headP~尾节点结束输出p 指向的节点P 指向下一个节点是表尾?P1=head P0=stud原链表为空P0所指节点 为唯一节点P0->num>p1->num &p1不是表尾节点P2->p1 p1向后移一个节点P0->num <=p1->numP1->next =p0p0->next =NULLP1->h ead P2->next=p0 p0->next=p1Head=p0 P0->next =p1软件使用说明:1)通过菜单函数选择使用的功能;2)调用对应的功能函数对链表进行操作;3)结束操作后,链表文件重新写入新的文件夹。
心得体会:链表学得不够扎实,各种自定义的函数操作还好,但建立主函数时,读取文件将文件中数据存入链表经常出错。
同时在进行将操作后的文件数据写入新的文件时,刚开始一直只能存入一组数据。
链表这章的适用性很强,用户操作方便。
源代码:#include <stdio.h>#include <stdlib.h>#define LEN sizeof(struct student)struct student{long num;float score;struct student *next;};void instraction(void );void print(struct student *head);struct student *del(struct student *head,long num);struct student *insert(struct student*head,struct student *stud);void writefile(struct student *head);main(){struct student *head,*stu;long delnum;struct student *new1=(struct student *) malloc(LEN);struct student *p1,*p2;int n=0,choice;FILE *fp;if((fp=fopen("compute112.txt","r+"))==NU LL){printf("file cannot opened!\n");}p1=p2=(struct student *) malloc(LEN);fscanf(fp,"%d%f",&(new1->num),&(new1->score));head=NULL;p1->num=new1->num;p1->score=new1->score;while(new1->num!=0){n++;if(n==1) head=p1;else p2->next=p1;p2=p1;p1=(struct student *) malloc(LEN);fscanf(fp,"%d%f",&(new1->num),&(new1->score));p1->num=new1->num;p1->score=new1->score;}p2->next=NULL;fclose(fp);start: instraction();scanf("%d",&choice);while(choice != 4){if(choice == 1){print(head);}else if(choice == 2){printf("输入要删除的学号:");scanf("%ld",&delnum);head=del(head,delnum);print(head);printf("\n\n");}else if(choice==3){printf("输入要插入的学号成绩:");stu=(struct student *)malloc(LEN);scanf("%ld%f",&stu->num,&stu->score);head=insert(head,stu);print(head);printf("\n\n");}goto start;}writefile(head);}void instraction(void){printf("***************************** ****\n");printf(" 1-打印链表;\n");printf(" 2-删去链表元素;\n");printf(" 3-插入链表元素;\n");printf(" 4-结束操作。
\n");printf("***************************** ****\n");printf("选择:");}void print(struct student *head){struct student *p;p=head;if(head != NULL)printf("\n 学号成绩\n");do{printf("%ld%10.1f\n",p->num,p->score);p=p->next;}while(p != NULL);}struct student *del(struct student *head,long num1){struct student *p1,*p2;if(head==NULL){printf("\nList is null!\n");return (head);}p1=head;while(num1 != p1->num && p1->next != NULL){p2=p1;p1=p1->next;}if(num1 == p1->num){if(p1 == head)head=p1->next;elsep2->next=p1->next;printf("delete:%ld\n",num1);}elseprintf("%ld not found!\n",num1); return (head);}struct student *insert(struct student *head,struct student *stu){struct student *p0,*p1,*p2;p1=head;p0=stu;if(head == NULL){head=p0;p0->next=NULL;}else{while((p0->num>p1->num) && (p1->next != NULL)){p2=p1;p1=p1->next;}if(p0->num<=p1->num){if(head == p1)head=p0;elsep2->next=p0;p0->next=p1;}else{p1->next=p0;p0->next=NULL;}}return(head);}void writefile(struct student *head){FILE *cfptr;struct student *p;p=(struct student *) malloc(LEN);cfptr=fopen("new compute112.dat","w+");p=head;fprintf(cfptr,"学号\t成绩\n");if(head!=NULL)while(p->next!=NULL){fprintf(cfptr,"%d %5.2f",p->num,p->score);//fputc(10,cfptr);p=p->next;}fclose(cfptr);}程序操作:。