单链表的冒泡排序(交换数据域和指针域两种做法)

利用以下的实例来引入单链表的冒泡排序(有注释)

设计一个菜单,具有功能: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;

        }

    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北城南笙(在校学习)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值