编写程序STUDENT *Create(STUDENT studs[],int n)。
2016-11-06 18:49
190 查看
STUDENT是一个结构类型,包含姓名、成绩和指针域。studs数组中存储了n个STUDENT记录。create函数的功能是根据studs数组建立一个链表,链表中结点按成绩降序排列,函数返回链表头指针。
#include<stdio.h> #include<stdlib.h> #include<string.h> struct student//定义结点结构体 { char name[10]; int score; struct student *next; }; typedef struct student STUDENT; typedef struct student* Link; void create_link(Link *head) { *head = NULL; } void is_malloc_ok(Link new_node) { if(NULL == new_node) { printf("malloc error!\n"); exit(-1); } } void create_new_node(Link *new_node) { *new_node = (Link)malloc(sizeof(STUDENT)); is_malloc_ok(*new_node); } void insert_new_node(Link *head, Link new_node) {//由成绩从小到大排序,插入新结点 Link tmp = NULL; Link q = NULL; tmp = *head; q = *head; if(NULL == *head)//空链表,头插新结点 { new_node->next = NULL; *head = new_node; } else { while ((new_node->score > tmp->score) && (tmp->next != NULL)) { q = tmp; tmp = tmp->next; } if (new_node->score < tmp->score) { if (*head == tmp) { new_node->next = tmp; *head = new_node; } else { new_node->next = tmp; q->next = new_node; } } if ((new_node->score > tmp->score) && (tmp->next==NULL)) { tmp->next = new_node; new_node->next = NULL; } } } STUDENT *create(STUDENT studs[], int n) { int i; Link head = NULL; Link new_node = NULL; create_link(&head); for (i = 0; i < n; i++) { create_new_node(&new_node); strcpy(new_node->name,studs[i].name); new_node->score = studs[i].score; new_node->next = studs[i].next; insert_new_node(&head, new_node); } return head; } void display_link(Link head) { Link tmp = NULL; tmp = head; while (tmp->next != NULL) { printf("The score %s is %d\n",tmp->name,tmp->score); tmp = tmp->next; if (tmp->next == NULL) { printf("The score %s is %d\n",tmp->name,tmp->score); break; } } } int main() { int n; int i; Link head = NULL; STUDENT studs[10]; printf("Enter number of students!(<=10)\n"); scanf("%d",&n); printf("Enter name and score!\n"); for (i = 0; i < n; i++) { scanf("%s",studs[i].name); scanf("%d",&(studs[i].score)); studs[i].next = NULL; } head = create(studs,n); display_link(head); return 0; }
相关文章推荐
- 编写程序STUDENT *Create(STUDENT studs[],int n)。 STUDENT是一个结构类型,包含姓名、成绩和指针域。
- 编写程序STUDENT *Create(STUDENT studs[],int n)
- 编写程序STUDENT *Create(STUDENT studs[],int n)。STUDENT是一个结构类型,包含姓名、成绩和指针域。studs数组中存储了n个STUDENT记录。create函
- 自己动手编写int 0处理程序
- 编写高质量代码改善C#程序的157个建议——建议5: 使用int?来确保值类型也可以为null
- 请编写一个c程序确定signed,unsigned的char,short,int和long变量取值范围
- 编写程序方法boolean isLeap(int date),要求传入的的日期,能够输出该日期是否是闰年
- 多态部分作业 1.按要求编写Java程序:(1)编写一个接口:InterfaceA,只含有一个方法int method(int n);
- 编写一个程序以确定分别由signed及unsigned限定的char、short、int及long类型变量的取值范围。采用打印标准头文件中的相应值以及直接计算两种方式实现
- 练习2-1 编写一个程序以确定分别由signed及unsigend限定的char short int 与long类型变量的取值范围
- 用C++编写递归函数GetPower(int x,int y)计算x的y次幂,在同一程序里中针对整型和实型实现两个重载的函数;在主程序中实现输入输出
- 编写一个程序,分别以signed和unsigned限定的char、short、int与long类型的变量取值范围
- 练习2-1 编写一个程序一确定分别由signed及unsigned限定的char,short,int及long类型变量的取值范围。采用打印标准头文件中的相应值以及直接计算两种方式实现。通过直接计算来确定浮点类型的取值范围是一项难度很大的任务。
- How To Create A Client Server Application In Minutes[如何在几分钟的时间里编写一个客户机/服务器控制台程序]
- 编写一个Java应用程序,该程序包括3个类:定义一个学生类,Student有姓名,学号,选学的课程列表 ,定义一个课程类:课程名称,课程分数
- [基础题] 6.(*)按如下要求编写Java程序: (1)编写一个接口:OneToN,只含有一个方法int dispose(int n)
- 编写一个JAVA程序,创建指定长度的 int 型数组,并生成 100 以内随机数为数组中的每个元素赋值,然后输出数组
- ★实验10-1 1. 编写程序测试函数setitimer(int)和getitimer()。 2. 安装信号, 使信号SIGALRM、SIGVTALRM、SIGPROF可以被捕获。 #include
- 编写一个函数,返回一个int数组中存储的最大数值,并在一个简单的程序中测试这个函数。(有缺陷)
- 练习 2-1 编写一个程序以确定分别由 signed 及 unsigned 限定的 char、short、 int 与 long 类型变量的取值范围。