|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
刚学链表写了一个关于录入学生成绩的程序,求指教哪里可以修改
- #include<stdio.h>
- #include<malloc.h>
- #include<stdlib.h>
- #define LN sizeof(struct student) //大小
- struct student *creat(); //输入与插入
- struct student *del(struct student *head);//删除
- void print (struct student *head); //打印
- struct student *head=NULL;
- int n=0;
- struct student
- {
- long num;
- float score;
- struct student *next;
- };
- main()
- {
- struct student *stu;
- int i=0;
- printf("*************************");
- printf(" 欢迎使用学生成绩录入系统");
- printf("************************\n");
- printf("建立与插入请按 1 \n删除请按 2\n打印请按 3\n结束请按 0\n");
- scanf("%d",&i);
- while(i)
- {
- switch(i)
- {
- case 1:
- stu=creat();
- break;
- case 2:
- stu=del(stu);
- break;
- case 3:
- print(stu);
- break;
- }
- printf("建立与插入请按1\n删除请按2\n打印请按3\n结束请按0\n");
- scanf("%d",&i);
- }
- }
- struct student *creat() //建立与插入
- {
- struct student *p1,*p2,*p3;
- p3=(struct student *)malloc(LN);
- n++;
- printf("请输入第%d学生的学号和成绩\n",n);
- scanf("%ld%f",&p3->num,&p3->score);
- while(p3->score>100||p3->score<0)
- {
- printf("输入成绩错误请输入第%d学生的学号和成绩\n",n);
- scanf("%ld%f",&p3->num,&p3->score);
- }
- while(p3->num)
- {
- if(head==NULL)
- {
- head=p3;
- n++;
- head->next=NULL;
- }
- p1=head;
- while(p3->num>p1->num&&p1->next!=NULL)
- {
- p2=p1;
- p1=p1->next;
- }
- if(p3->num<p1->num)
- {
- if(p1==head)
- {
- p3->next=head;
- head=p3;
- n++;
- }
- else
- {
- p2->next=p3;
- p3->next=p1;
- n++;
- }
- }
- else if(head!=NULL&&p3->num>p1->num)
- {
-
- p1->next=p3;
- p3->next=NULL;
- n++;
- }
- p3=(struct student*)malloc(LN);
- printf("请输入第%d学生的学号和成绩:\n",n);
- scanf("%ld%f",&p3->num,&p3->score);
- while(p3->score>100||p3->score<0)
- {
- printf("输入成绩错误请输入第%d学生的学号和成绩:\n",n);
- scanf("%ld%f",&p3->num,&p3->score);
- }
- }
-
- return head;
- }
- struct student *del(struct student *head) //删除
- {
- struct student *p1,*p2;
- long num;
- if(head==NULL)
- {
- printf("这是一个空链表\n");
- return head;
- }
- print(head);
- printf("请输入上表要删除的学生学号:\n");
- scanf("%ld",&num);
- while(num)
- {
- p1=head;
- while(p1->num!=num&&p1->next!=NULL)
- {
- p2=p1;
- p1=p1->next;
- }
- if(p1->num==num)
- {
- if(p1==head)
- {
- head=p1->next;
- n--;
- }
- else
- {
- p2->next=p1->next;
- n--;
- }
- }
- else
- {
- printf("没有符合删除条件的学生\n");
- }
- printf("请输入要删除的学生学号:\n");
- scanf("%ld",&num);
- }
- printf("删除后列表如下:\n");
- p1=head;
- while(p1)
- {
- printf("%5d%6.1lf\t\n",p1->num,p1->score);
- p1=p1->next;
- }
- return head;
-
- }
- void print(struct student *head) //打印
- {
- struct student *p;
- p=head;
- if(head==NULL)
- {
- printf("没有可以查看的内容\n");
- }
- else
- {
- printf("整理后列表如下:\n");
- while(p)
- {
- printf("%5ld%6.1f\t\n",p->num,p->score);
- p=p->next;
- }
- }
- }
复制代码
|
|