利用以下的实例来引入单链表的冒泡排序(有注释)
设计一个菜单,具有功能:1.输入任意数量的学生信息(存储大小允许下);
2.修改指定学生的成绩;
3.对学生的成绩冒泡排序(两种做法)
以下为代码部分:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//定义信息
typedef struct menu
{
/* data */
char id[80];
char name[80];
int score1;
int score2;
int score3;
int score4;
float average;
int sum;
}info;
//定义节点
typedef struct node{
info item;
struct node * next;
}Node;
Node *addList(Node **head,Node *tail);
void print1(Node *head);
void revise(Node **head);
void cal(Node **head);
void print2(Node *head);
void print3(Node *head);
void BubbleSort(Node *head);//按照平均成绩进行冒泡排序
int main()
{
int choice;
Node *head=NULL,*tail;
while(1){
scanf("%d",&choice);
switch (choice)
{
case 0:
system("pause");
return 0;
case 1:
tail=addList(&head,tail);
break;
case 2:
cal(&head);
BubbleSort(head);
print1(head);
break;
case 3:
revise(&head);
break;
case 4:
cal(&head);
BubbleSort(head);
print3(head);
break;
case 5:
cal(&head);
BubbleSort(head);
print2(head);
break;
}
}
}
Node *addList(Node **head,Node *tail)
{
int number,i;
Node *p=NULL;
scanf("%d",&number);
for(i=0;i<number;i++)
{
p = (Node *)malloc(sizeof(Node));
scanf("%s",&p->item.id);
scanf("%s",&p->item.name);
scanf("%d",&p->item.score1);
scanf("%d",&p->item.score2);
scanf("%d",&p->item.score3);
scanf("%d",&p->item.score4);
p->item.sum=p->item.score1+p->item.score2+p->item.score3+p->item.score4;
if(*head==NULL){
*head=p;
tail=*head;
}
else{
tail->next=p;
tail=tail->next;
}
}
if(tail->next!=NULL) tail->next=NULL;//尾指针置空
return tail;//返回尾指针,方便下次添加
}
void print1(Node *head)
{
Node *p=head;
while(p!=NULL)
{
printf("%s ",p->item.id);
printf("%s ",p->item.name);
printf("%d ",p->item.score1);
printf("%d ",p->item.score2);
printf("%d ",p->item.score3);
printf("%d\n",p->item.score4);
p=p->next;
}
}
void revise(Node **head)
{
Node *p=*head;
char id1[80];
int number;
scanf("%s",&id1);
while(strcmp(id1,p->item.id)!=0)
p=p->next;
scanf("%d",&number);
switch (number)
{
case 1 :
scanf("%d",&(p->item.score1));
break;
case 2 :
scanf("%d",&(p->item.score2));
break;
case 3 :
scanf("%d",&(p->item.score3));
break;
case 4 :
scanf("%d",&(p->item.score4));
break;
}
p->item.sum=p->item.score1+p->item.score2+p->item.score3+p->item.score4;
}
void cal(Node **head)
{
Node *p=*head;
while(p)
{
p->item.average=1.0*p->item.sum/4;
p=p->next;
}
}
void print2(Node *head)
{
Node *p=head;
while(p!=NULL)
{
printf("%s ",p->item.id);
printf("%s ",p->item.name);
printf("%d ",p->item.sum);
printf("%.2f\n",p->item.average);
p=p->next;
}
}
void print3(Node *head)
{
Node *p=head;
while(p!=NULL)
{
printf("%s ",p->item.id);
printf("%s ",p->item.name);
printf("%.2f\n",p->item.average);
p=p->next;
}
}
//交换数据域
// void BubbleSort(Node *head)
// {
// int i=0,num=0,count=0;//count记录node的个数
// Node *p,*q;
// info temp;
// p=head;
// while (p!=NULL)//统计节点个数
// {
// count++;
// p=p->next;
// }
// for(i=0;i<count-1;i++)
// {
// num=count-i-1;//记录内存循环需要的次数
// p=head;
// q=p->next;
// while(num--)
// {
// if(p->item.average>q->item.average)
// {
// temp=p->item;
// p->item=q->item;
// q->item=temp;
// }
// p=p->next;
// q=p->next;
// }
// }
// }
//交换指针域
void BubbleSort(Node *head)
{
int i=0,num=0,count=0;//count记录node的个数
Node *p,*q,*last;
last = (Node *)malloc(sizeof(Node));
p=head;
while (p!=NULL)//统计节点个数
{
count++;
p=p->next;
}
for(i=0;i<count-1;i++)
{
num=count-i-1;//记录内存循环需要的次数
last->next=head;//在head的前方虚设一个方便交换
p=head;
q=p->next;
while(num--)
{
if(p->item.average>q->item.average)
{
p->next=q->next;
q->next=p;
last->next=q;
p=last->next;
}
last=last->next;
p=p->next;
q=p->next;
}
}
}