|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct Player{
- char id[10];
- char name[20];
- float score;
- struct Player *next;
- }Play, *pPlay;
- void getInput(pPlay );
- void addPlayer(pPlay * );
- void printPlayer(pPlay );
- void Wlist(pPlay );
- void BubbleSort(pPlay );
- int main(void)
- {
- pPlay info = NULL;
- int num;
-
- printf("请输入参赛选手的数目: ");
- scanf("%d", &num);
-
- for(int i = 0; i < num; i++)
- {
- addPlayer(&info);
- putchar('\n');
- }
-
- printf("\n 录入完毕, 现在开始打印验证....\n");
- printPlayer(info);
- printf("\n");
- BubbleSort(info);
- printPlayer(info);
- //Wlist(info);
-
- }
- void getInput(pPlay player)
- {
- printf("请输入选手的编号 姓名 成绩 : ");
- scanf("%s %s %f", player->id, player->name, &player->score);
- }
- void addPlayer(pPlay *info)
- {
- pPlay player, tmp;
- static pPlay tail;
-
- player = (pPlay)malloc(sizeof(Play));
-
- if(player == NULL)
- {
- printf("memory allocation error!\n");
- exit(1);
- }
-
- getInput(player);
-
- if(*info != NULL)
- {
- tail->next = player;
- player->next = NULL;
- }
- else
- {
- *info = player;
- player -> next = NULL;
- }
-
- tail = player;
- }
- void printPlayer(pPlay info)
- {
- pPlay player;
- player = info;
- while(player != NULL)
- {
- printf("编号: %s, 姓名: %s, 分数 :%5.2f\n", player->id, player->name, player->score);
- player = player -> next;
- }
- }
- void Wlist(pPlay info)
- {
- pPlay tmp = info;
- FILE *fp;
- char filename[30];
- printf("input file name : ");
- scanf("%s", filename);
-
- if((fp = fopen(filename,"wb")) == NULL)
- {
- printf("file open error!\n");
- exit(0);
- }
-
- while(tmp)
- {
- fprintf(fp, "%s %s %5.2f\n", tmp->id, tmp->name, tmp->score);
- tmp = tmp->next;
- }
- fprintf(fp,"\n");
- fclose(fp);
- }
- void BubbleSort(pPlay head)
- {
- pPlay cur, tail, tmp;
-
- tail = NULL;
-
- while((head->next) != tail)
- {
- cur = head;
-
- while(cur->next != tail)
- {
- if((cur->score) > (cur->next->score))
- {
- cur->next = cur->next->next;
- tmp = cur->next->next;
- cur->next->next = cur;
- cur->next = tmp;
- }
- cur = cur->next;
- }
- tail = cur;
- }
- }
复制代码 用的是小甲鱼的链表插入,头指针里面有数据
请老哥们指点一下哪错了,调试的时候提示
添加我会这么改,print我会这么做
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct Player{
- char id[10];
- char name[20];
- float score;
- struct Player *next;
- }Play, *pPlay;
- void getInput(pPlay );
- void addPlayer(pPlay * );
- void printPlayer(pPlay );
- void Wlist(pPlay );
- void BubbleSort(pPlay );
- int main(void)
- {
- pPlay info = NULL;
- int num;
- printf("请输入参赛选手的数目: ");
- scanf("%d", &num);
- for(int i = 0; i < num; i++)
- {
- addPlayer(&info);
- }
- printf("\n 录入完毕, 现在开始打印验证....\n");
- printPlayer(info);
- printf("\n");
- //BubbleSort(info);
- printPlayer(info);
- //Wlist(info);
- }
- void getInput(pPlay player)
- {
- printf("请输入选手的编号 姓名 成绩 : ");
- scanf("%s %s %f", player->id, player->name, &player->score);
- player -> next = NULL;
- }
- void addPlayer(pPlay *info)
- {
- pPlay newplayer, player;
- newplayer = (pPlay)malloc(sizeof(Play));
- if(newplayer == NULL)
- {
- printf("memory allocation error!\n");
- exit(1);
- }
- getInput(newplayer);
- if(*info == NULL) // 判断头节点
- {
- *info = newplayer; //
- }
- else
- {
- player = *info; // 指向头节点
- while(player->next!=NULL)
- {
- player = player->next;
- }
- player->next = newplayer; // 把尾节点指向 新增用户
- }
- }
- void printPlayer(pPlay info)
- {
- while(info != NULL)
- {
- printf("编号: %s, 姓名: %s, 分数 :%5.2f\n", info->id, info->name, info->score);
- info = info -> next;
- }
- }
- void Wlist(pPlay info)
- {
- pPlay tmp = info;
- FILE *fp;
- char filename[30];
- printf("input file name : ");
- scanf("%s", filename);
- if((fp = fopen(filename,"wb")) == NULL)
- {
- printf("file open error!\n");
- exit(0);
- }
- while(tmp)
- {
- fprintf(fp, "%s %s %5.2f\n", tmp->id, tmp->name, tmp->score);
- tmp = tmp->next;
- }
- fprintf(fp,"\n");
- fclose(fp);
- }
- void BubbleSort(pPlay head)
- {
- pPlay cur, tail, tmp;
- tail = NULL;
- while((head->next) != tail)
- {
- cur = head;
- while(cur->next != tail)
- {
- if((cur->score) > (cur->next->score))
- {
- cur->next = cur->next->next;
- tmp = cur->next->next;
- cur->next->next = cur;
- cur->next = tmp;
- }
- cur = cur->next;
- }
- tail = cur;
- }
- }
复制代码
|
|