赞
踩
示例代码:
#include<stdio.h> #include<stdlib.h> #define N 4 //输入到文件的函数 void file(struct node*head); //创建节点函数 struct node*fun(); //删除函数 void del(struct node*); //查找函数 void find(struct node*); //全部输出函数 struct node*output(struct node*); struct node { int date; int score; char name[10]; struct node *next; }; //首页函数 int sy() { int i=1; printf("\t\t\t\t\t——————————————————\n"); printf("\t\t\t\t\t*****链表各个功能实验测试 *****\n"); printf("\t\t\t\t\t*****0.退出系统 *****\n"); printf("\t\t\t\t\t*****1.输入数据 *****\n"); printf("\t\t\t\t\t*****2.输出全部数据 *****\n"); printf("\t\t\t\t\t*****3.删除数据 *****\n"); printf("\t\t\t\t\t*****4.保存到文本 *****\n"); printf("请选择\n"); scanf_s("%d",&i); while(1) { if(i<0||i>N) { printf("***没有此选项。请重新选择***\n"); system("pause"); } break; } return i; } void main() { int i,j=1; struct node*head=NULL; while(j) { i=sy(); switch(i) { case 0: j=0; printf("退出成功"); break; case 1: head=fun();system("pause");break;//创建节点函数 case 2: head=output(head);system("pause");break;//全部输出函数 case 3: del(head);system("pause");break;//删除函数 case 4:file(head); default:break; } printf("\t\t\t\t\t——————————————————\n"); } system("pause"); } //1创建节点 struct node*fun() { int i=0,j=1; int date; int score; char name[10]; struct node *head,*p,*n; struct node *last=NULL;//尾节点 head=p=(struct node*)malloc(sizeof(struct node)); //printf("输入多个个学生的编号,用空格隔开,输入0按回车结束输入\n"); //scanf("%d",&i); while(1) { if(j>0) { i++; n=(struct node*)malloc(sizeof(struct node)); printf("请输入第%d个学生的学号,姓名,分数\n",i); scanf("%d %s %d",&n->date,&n->name,&n->score); p->next=n; p=n; printf("是否继续输入下一个同学的信息?输入 1 继续,输入 0 就停止输入\n"); fflush(stdin); scanf("%d",&j); fflush(stdin); } else { p->next=last; p=last; break; } } return head; } //2输出 struct node* output(struct node*head) { if(head==NULL) printf("还没有数据请先输入数据\n"); else { struct node *p1; p1=head->next; printf("学号\t姓名\t分数\n"); while(p1!=NULL) { printf("%d\t%s\t%d",p1->date,p1->name,p1->score); p1=p1->next; printf("\n"); } printf("\n"); return head; } } //3删除 void del(struct node*head) { int key,j=0; struct node*p,*s; s=head->next; p=head; printf("请输入你要删除的数据\n"); getchar();//fflush(stdin) ;删除缓存的数据。 scanf("%d",&key); printf("%d\n",key); while(s!=NULL) { if(s->date!=key) { p=s; s=s->next; } else break; } if(s!=NULL) { p->next=s->next; free(s); printf("yes yes yes\n"); } else printf("no no no\n"); } void file(struct node*head){ int i=0; FILE *fp; struct node*p=head->next; fp=fopen("C:\\Users\\Administrator\\Desktop\\112.txt","wt"); while(p!=NULL) { fprintf(fp," %d\t%s\t%d\n",p->date,p->name,p->score); p = p->next; i++; } if(i>0) printf("\t\t\t\t\t—————保存成功—————\n"); else printf("\t\t\t\t\t—————保存失败—————\n"); fclose(fp); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。