写了一个链表的冒泡排序,用尾插法插入的链表,运行失败了
#include <stdio.h>#include <stdlib.h>
typedef struct Player{
char id;
char name;
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;
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;
char name;
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;
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;
}
}
页:
[1]