您的位置:首页 > 其它

编写程序STUDENT *Create(STUDENT studs[],int n)。 STUDENT是一个结构类型,包含姓名、成绩和指针域。

2017-08-04 19:43 337 查看
编写程序STUDENT *Create(STUDENT studs[],int n)。

  STUDENT是一个结构类型,包含姓名、成绩和指针域。

  studs数组中存储了n个STUDENT记录。

  create函数的功能是根据studs数组建立一个链表,

  链表中结点按成绩降序排列,函数返回链表头指针。

#include <stdio.h>

#include <stdlib.h>

struct student  //创建学生结构体

{
char name[20];
int score;
struct student *next;

};

typedef struct student STUDENT;

STUDENT *Create(STUDENT *studs, int n)

{
int i = 0;
int j = 0;
STUDENT t;
STUDENT *head;

for (i = 0; i < n - 1; i++) //冒泡排序
{
for (j = 0; j < n - 1 - i; j++)
{
if (studs[j].score < studs[j + 1].score)
{
t = studs[j];
studs[j] = studs[j + 1];
studs[j + 1] = t;
}
}
}

head = &studs[0];

for (i = 0; i < n - 1; i++) //链接成链表
{
studs[i].next = &studs[i + 1];
}
studs[i].next = NULL;

return head;

}

void Display(STUDENT *head)     //打印链表节点数据

{
STUDENT *p = head;

while (p != NULL)
{
printf ("%s  %d", p -> name, p -> score);
printf ("\n");
p = p -> next;
}

}

int main()

{
STUDENT studs[40];
int n = 0;
int i = 0;
STUDENT *head;

printf ("How many students : ");
scanf ("%d", &n);
printf ("Please input the name and scores : \n");

for (i = 0; i < n; i++)
{
getchar();
scanf ("%s%d", studs[i].name, &studs[i].score); //接收学生的数据
}

head = Create(studs, n);

printf ("Large to small:\n"); //打印结果
Display(head);

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐