鱼C论坛

 找回密码
 立即注册
查看: 2322|回复: 13

[已解决]求教c语言的 动态链表排序

[复制链接]
发表于 2018-4-14 16:29:10 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
struct Postgraduate {                                                        //研究生
        int num;                                                                        //学号
        char name[10];                                                                //姓名
        char sex1[4];                                                            //性别
        int score;
        struct  Postgraduate *next;
}*head;


例如head有 3个数据,|01  小明  男   66 | 02  小甲鱼  99 | 03 大甲鱼 88|NULL

然后怎么才能按照分数由大到小排好???
最佳答案
2018-4-14 22:30:19
ssg 发表于 2018-4-14 19:17
没有人吗?????

^_^

今天先就写到这里,我明天有事,要早点睡觉
Swap函数已经调试到完美,希望没有什么问题了,^_^
就剩下 void SortList(struct Postgraduate *head) 这个函数了
你先试着写一写,看看能不能写出来,如果明天有时间,我再来写,不过真的建议你先写一写
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Postgraduate                        // 研究生
{
        int num;                                // 学号
        char name[10];                                // 姓名
        char sex1[4];                                // 性别
        int score;
        struct  Postgraduate *prior;
        struct  Postgraduate *next;
} Node;

struct Postgraduate *CreateListDebug(struct Postgraduate arr[], int count)
{
        // 头结点不存储数据
        struct Postgraduate *head = malloc(sizeof(Node));;
        head->num = 0;
        head->name[0] = '\0';
        head->sex1[0] = '\0';
        head->score = 0;
        head->prior = NULL;
        head->next = NULL;

        Node *p = head;
        for(int i = 0; i < count; ++i)
        {
                Node *n = malloc(sizeof(Node));

                n->num = arr[i].num;
                strcpy(n->name, arr[i].name);
                strcpy(n->sex1, arr[i].sex1);
                n->score = arr[i].score;
                n->prior = NULL;
                n->next = NULL;

                p->next = n;
                n->prior = p;
                p = p->next;
        }

        return head;
}

void Swap(Node *a, Node *b)
{
        // a和b紧挨着的时候需要另一种方法
        if((a->next == b) || (b->next == a))
        {
                // a在b的前面和b在a的前面不能使用同一种方法
                if(b->next == a)        // 让a始终在b的前面
                {
                        struct  Postgraduate *temp = a;
                        a = b;
                        b = temp;
                }

                struct  Postgraduate *a_prior = a->prior;
                struct  Postgraduate *b_next = b->next;

                if(b_next == NULL)        // b是尾结点
                {
                        a_prior->next = b;
                        b->prior = a_prior;
                        a->next = b_next;
                        a->prior = b;
                        b->next = a;
                }
                else
                {
                        a_prior->next = b;
                        b->prior = a_prior;
                        a->next = b_next;
                        a->prior = b;
                        b->next = a;
                        b_next->prior = a;
                }
        }
        else
        {
                struct  Postgraduate *a_prior = a->prior;
                struct  Postgraduate *a_next = a->next;
                struct  Postgraduate *b_prior = b->prior;
                struct  Postgraduate *b_next = b->next;


                if((a_next == NULL) || (b_next == NULL))        // 这两个结点中有一个是尾结点
                {
                        if(a_next == NULL)        // 让b成为尾结点
                        {
                                struct  Postgraduate *temp = a;
                                a = b;
                                b = temp;

                                // update
                                a_prior = a->prior;
                                a_next = a->next;
                                b_prior = b->prior;
                                b_next = b->next;
                        }

                        a_prior->next = b;
                        b->prior = a_prior;
                        a->next = b_next;
                        a->prior = b_prior;
                        a_next->prior = b;
                        b_prior->next = a;
                        b->next = a_next;
                }
                else
                {
                        a_prior->next = b;
                        b->prior = a_prior;
                        a->next = b_next;
                        a->prior = b_prior;
                        a_next->prior = b;
                        b_prior->next = a;
                        b->next = a_next;
                        b_next->prior = a;
                }
        }
}

void SortList(struct Postgraduate *head)
{

}

int main(void)
{
        struct Postgraduate arr[] =
        {
                {0, "小红", "女", 100},
                {1, "小明", "男", 10},
                {2, "小玲", "女", 50},
                {3, "小丽", "女", 40},
                {4, "小林", "男", 90},
                {5, "小张", "男", 5}
        };
        int arr_count = sizeof(arr) / sizeof(arr[0]);

        struct Postgraduate *head = CreateListDebug(arr, arr_count);

        Swap(head->next, head->next->next->next);
        Swap(head->next->next->next, head->next);
        
        Swap(head->next, head->next->next);
        Swap(head->next->next, head->next);
        
        Swap(head->next, head->next->next->next->next->next->next);
        Swap(head->next->next->next->next->next->next, head->next);
        
        Swap(head->next->next->next->next->next, head->next->next->next->next->next->next);
        Swap(head->next->next->next->next->next->next, head->next->next->next->next->next);
        return 0;
}


下面是前一个版本,未完成的版本,正因为有下面这个版本,才会有双向链表的这个版本,如果有兴趣,你可以参考
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Postgraduate                        // 研究生
{
        int num;                                // 学号
        char name[10];                                // 姓名
        char sex1[4];                                // 性别
        int score;
        struct  Postgraduate *next;
} Node;

void GetString(char *buff, int max_count)
{
        fgets(buff, max_count, stdin);
        int len = strlen(buff);
        buff[len - 1] = '\0';
}

struct Postgraduate *CreateListDebug(struct Postgraduate arr[], int count)
{
        struct Postgraduate *head = NULL;
        Node *p;

        for(int i = 0; i < count; ++i)
        {
                Node *n = malloc(sizeof(Node));

                n->num = arr[i].num;
                strcpy(n->name, arr[i].name);
                strcpy(n->sex1, arr[i].sex1);
                n->score = arr[i].score;
                n->next = NULL;

                if(head == NULL)
                {
                        head = n;
                        p = head;
                        continue;
                }

                p->next = n;
                p = p->next;
        }

        return head;
}

struct Postgraduate *CreateList(void)
{
        struct Postgraduate *head = NULL;
        Node *p;
        int count = 5;
        
        do
        {
                Node *n = malloc(sizeof(Node));

                printf("学号:");
                scanf("%d", &n->num);
                getchar();
                printf("姓名:");
                GetString(n->name, 10);
                printf("性别:");
                GetString(n->sex1, 4);
                printf("成绩:");
                scanf("%d", &n->score);
                getchar();
                n->next = NULL;

                if(head == NULL)
                {
                        head = n;
                        p = head;
                        continue;
                }

                p->next = n;
                p = p->next;
        }
        while(--count);

        return head;
}

void Swap(Node *a, Node *b)
{
        struct  Postgraduate *temp_a;
        struct  Postgraduate *temp_b;

        temp_a = a->next;
        a->next = b->next;
        temp_b = b->next->next;
        b->next->next = temp_a;
        b->next->next->next = temp_b;
}

void SortList(struct Postgraduate *head)
{

}

int main(void)
{
        struct Postgraduate arr[] =
        {
                {0, "小红", "女", 100},
                {1, "小明", "男", 10},
                {2, "小玲", "女", 50},
                {3, "小丽", "女", 40},
                {4, "小林", "男", 90},
                {5, "小张", "男", 5}
        };
        int arr_count = sizeof(arr) / sizeof(arr[0]);

        //struct Postgraduate *head = CreateList();
        struct Postgraduate *head = CreateListDebug(arr, arr_count);        // 为debug方便,使用数组录入数据,每次都要手动输入太要命了,^_^

        //Swap(head, head->next);
        //Swap(head->next, head->next->next);
        Swap(head, head->next->next);
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-4-14 22:30:19 | 显示全部楼层    本楼为最佳答案   
ssg 发表于 2018-4-14 19:17
没有人吗?????

^_^

今天先就写到这里,我明天有事,要早点睡觉
Swap函数已经调试到完美,希望没有什么问题了,^_^
就剩下 void SortList(struct Postgraduate *head) 这个函数了
你先试着写一写,看看能不能写出来,如果明天有时间,我再来写,不过真的建议你先写一写
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Postgraduate                        // 研究生
{
        int num;                                // 学号
        char name[10];                                // 姓名
        char sex1[4];                                // 性别
        int score;
        struct  Postgraduate *prior;
        struct  Postgraduate *next;
} Node;

struct Postgraduate *CreateListDebug(struct Postgraduate arr[], int count)
{
        // 头结点不存储数据
        struct Postgraduate *head = malloc(sizeof(Node));;
        head->num = 0;
        head->name[0] = '\0';
        head->sex1[0] = '\0';
        head->score = 0;
        head->prior = NULL;
        head->next = NULL;

        Node *p = head;
        for(int i = 0; i < count; ++i)
        {
                Node *n = malloc(sizeof(Node));

                n->num = arr[i].num;
                strcpy(n->name, arr[i].name);
                strcpy(n->sex1, arr[i].sex1);
                n->score = arr[i].score;
                n->prior = NULL;
                n->next = NULL;

                p->next = n;
                n->prior = p;
                p = p->next;
        }

        return head;
}

void Swap(Node *a, Node *b)
{
        // a和b紧挨着的时候需要另一种方法
        if((a->next == b) || (b->next == a))
        {
                // a在b的前面和b在a的前面不能使用同一种方法
                if(b->next == a)        // 让a始终在b的前面
                {
                        struct  Postgraduate *temp = a;
                        a = b;
                        b = temp;
                }

                struct  Postgraduate *a_prior = a->prior;
                struct  Postgraduate *b_next = b->next;

                if(b_next == NULL)        // b是尾结点
                {
                        a_prior->next = b;
                        b->prior = a_prior;
                        a->next = b_next;
                        a->prior = b;
                        b->next = a;
                }
                else
                {
                        a_prior->next = b;
                        b->prior = a_prior;
                        a->next = b_next;
                        a->prior = b;
                        b->next = a;
                        b_next->prior = a;
                }
        }
        else
        {
                struct  Postgraduate *a_prior = a->prior;
                struct  Postgraduate *a_next = a->next;
                struct  Postgraduate *b_prior = b->prior;
                struct  Postgraduate *b_next = b->next;


                if((a_next == NULL) || (b_next == NULL))        // 这两个结点中有一个是尾结点
                {
                        if(a_next == NULL)        // 让b成为尾结点
                        {
                                struct  Postgraduate *temp = a;
                                a = b;
                                b = temp;

                                // update
                                a_prior = a->prior;
                                a_next = a->next;
                                b_prior = b->prior;
                                b_next = b->next;
                        }

                        a_prior->next = b;
                        b->prior = a_prior;
                        a->next = b_next;
                        a->prior = b_prior;
                        a_next->prior = b;
                        b_prior->next = a;
                        b->next = a_next;
                }
                else
                {
                        a_prior->next = b;
                        b->prior = a_prior;
                        a->next = b_next;
                        a->prior = b_prior;
                        a_next->prior = b;
                        b_prior->next = a;
                        b->next = a_next;
                        b_next->prior = a;
                }
        }
}

void SortList(struct Postgraduate *head)
{

}

int main(void)
{
        struct Postgraduate arr[] =
        {
                {0, "小红", "女", 100},
                {1, "小明", "男", 10},
                {2, "小玲", "女", 50},
                {3, "小丽", "女", 40},
                {4, "小林", "男", 90},
                {5, "小张", "男", 5}
        };
        int arr_count = sizeof(arr) / sizeof(arr[0]);

        struct Postgraduate *head = CreateListDebug(arr, arr_count);

        Swap(head->next, head->next->next->next);
        Swap(head->next->next->next, head->next);
        
        Swap(head->next, head->next->next);
        Swap(head->next->next, head->next);
        
        Swap(head->next, head->next->next->next->next->next->next);
        Swap(head->next->next->next->next->next->next, head->next);
        
        Swap(head->next->next->next->next->next, head->next->next->next->next->next->next);
        Swap(head->next->next->next->next->next->next, head->next->next->next->next->next);
        return 0;
}


下面是前一个版本,未完成的版本,正因为有下面这个版本,才会有双向链表的这个版本,如果有兴趣,你可以参考
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Postgraduate                        // 研究生
{
        int num;                                // 学号
        char name[10];                                // 姓名
        char sex1[4];                                // 性别
        int score;
        struct  Postgraduate *next;
} Node;

void GetString(char *buff, int max_count)
{
        fgets(buff, max_count, stdin);
        int len = strlen(buff);
        buff[len - 1] = '\0';
}

struct Postgraduate *CreateListDebug(struct Postgraduate arr[], int count)
{
        struct Postgraduate *head = NULL;
        Node *p;

        for(int i = 0; i < count; ++i)
        {
                Node *n = malloc(sizeof(Node));

                n->num = arr[i].num;
                strcpy(n->name, arr[i].name);
                strcpy(n->sex1, arr[i].sex1);
                n->score = arr[i].score;
                n->next = NULL;

                if(head == NULL)
                {
                        head = n;
                        p = head;
                        continue;
                }

                p->next = n;
                p = p->next;
        }

        return head;
}

struct Postgraduate *CreateList(void)
{
        struct Postgraduate *head = NULL;
        Node *p;
        int count = 5;
        
        do
        {
                Node *n = malloc(sizeof(Node));

                printf("学号:");
                scanf("%d", &n->num);
                getchar();
                printf("姓名:");
                GetString(n->name, 10);
                printf("性别:");
                GetString(n->sex1, 4);
                printf("成绩:");
                scanf("%d", &n->score);
                getchar();
                n->next = NULL;

                if(head == NULL)
                {
                        head = n;
                        p = head;
                        continue;
                }

                p->next = n;
                p = p->next;
        }
        while(--count);

        return head;
}

void Swap(Node *a, Node *b)
{
        struct  Postgraduate *temp_a;
        struct  Postgraduate *temp_b;

        temp_a = a->next;
        a->next = b->next;
        temp_b = b->next->next;
        b->next->next = temp_a;
        b->next->next->next = temp_b;
}

void SortList(struct Postgraduate *head)
{

}

int main(void)
{
        struct Postgraduate arr[] =
        {
                {0, "小红", "女", 100},
                {1, "小明", "男", 10},
                {2, "小玲", "女", 50},
                {3, "小丽", "女", 40},
                {4, "小林", "男", 90},
                {5, "小张", "男", 5}
        };
        int arr_count = sizeof(arr) / sizeof(arr[0]);

        //struct Postgraduate *head = CreateList();
        struct Postgraduate *head = CreateListDebug(arr, arr_count);        // 为debug方便,使用数组录入数据,每次都要手动输入太要命了,^_^

        //Swap(head, head->next);
        //Swap(head->next, head->next->next);
        Swap(head, head->next->next);
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2018-4-14 16:58:36 From FishC Mobile | 显示全部楼层
比较每一个节点的score值大小即可
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2018-4-14 16:59:32 | 显示全部楼层
BngThea 发表于 2018-4-14 16:58
比较每一个节点的score值大小即可

emmm  我想实现把排序好的顺序 放到 head2 去,怎么写??
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-14 17:01:51 From FishC Mobile | 显示全部楼层
将head2的next指针指向头节点即可
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-4-14 17:03:11 | 显示全部楼层
BngThea 发表于 2018-4-14 17:01
将head2的next指针指向头节点即可

那循环的条件语句怎么写,我c语言很菜
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-4-14 17:03:46 | 显示全部楼层
BngThea 发表于 2018-4-14 17:01
将head2的next指针指向头节点即可

我写好几个星期了,还没写好这程序
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-4-14 19:17:42 | 显示全部楼层
没有人吗?????
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-4-15 08:40:05 | 显示全部楼层
人造人 发表于 2018-4-14 22:30
^_^

今天先就写到这里,我明天有事,要早点睡觉

谢谢层主,膜拜,,,我 昨天 用冒泡写了一下,暂时搞定了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-4-15 08:41:55 | 显示全部楼层
人造人 发表于 2018-4-14 22:30
^_^

今天先就写到这里,我明天有事,要早点睡觉

又学到双向链表这好东西了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-15 09:58:39 | 显示全部楼层
ssg 发表于 2018-4-15 08:41
又学到双向链表这好东西了

^_^
我也写完了

应该没有问题了
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Postgraduate                        // 研究生
{
        int num;                                // 学号
        char name[10];                                // 姓名
        char sex1[4];                                // 性别
        int score;
        struct  Postgraduate *prior;
        struct  Postgraduate *next;
} Node;

struct Postgraduate *CreateListDebug(struct Postgraduate arr[], int count)
{
        // 头结点不存储数据
        struct Postgraduate *head = malloc(sizeof(Node));;
        head->num = 0;
        head->name[0] = '\0';
        head->sex1[0] = '\0';
        head->score = 0;
        head->prior = NULL;
        head->next = NULL;

        Node *p = head;
        for(int i = 0; i < count; ++i)
        {
                Node *n = malloc(sizeof(Node));

                n->num = arr[i].num;
                strcpy(n->name, arr[i].name);
                strcpy(n->sex1, arr[i].sex1);
                n->score = arr[i].score;
                n->prior = NULL;
                n->next = NULL;

                p->next = n;
                n->prior = p;
                p = p->next;
        }

        return head;
}

void Swap(Node *a, Node *b)
{
        // a和b紧挨着的时候需要另一种方法
        if((a->next == b) || (b->next == a))
        {
                // a在b的前面和b在a的前面不能使用同一种方法
                if(b->next == a)        // 让a始终在b的前面
                {
                        struct  Postgraduate *temp = a;
                        a = b;
                        b = temp;
                }

                struct  Postgraduate *a_prior = a->prior;
                struct  Postgraduate *b_next = b->next;

                if(b_next == NULL)        // b是尾结点
                {
                        a_prior->next = b;
                        b->prior = a_prior;
                        a->next = b_next;
                        a->prior = b;
                        b->next = a;
                }
                else
                {
                        a_prior->next = b;
                        b->prior = a_prior;
                        a->next = b_next;
                        a->prior = b;
                        b->next = a;
                        b_next->prior = a;
                }
        }
        else
        {
                struct  Postgraduate *a_prior = a->prior;
                struct  Postgraduate *a_next = a->next;
                struct  Postgraduate *b_prior = b->prior;
                struct  Postgraduate *b_next = b->next;


                if((a_next == NULL) || (b_next == NULL))        // 这两个结点中有一个是尾结点
                {
                        if(a_next == NULL)        // 让b成为尾结点
                        {
                                struct  Postgraduate *temp = a;
                                a = b;
                                b = temp;

                                // update
                                a_prior = a->prior;
                                a_next = a->next;
                                b_prior = b->prior;
                                b_next = b->next;
                        }

                        a_prior->next = b;
                        b->prior = a_prior;
                        a->next = b_next;
                        a->prior = b_prior;
                        a_next->prior = b;
                        b_prior->next = a;
                        b->next = a_next;
                }
                else
                {
                        a_prior->next = b;
                        b->prior = a_prior;
                        a->next = b_next;
                        a->prior = b_prior;
                        a_next->prior = b;
                        b_prior->next = a;
                        b->next = a_next;
                        b_next->prior = a;
                }
        }
}

void SortList(struct Postgraduate *head)
{
        for(Node *i = head->next; i != NULL; i = i->next)
        {
                for(Node *j = i->next; j != NULL; j = j->next)
                {
                        if(i->score > j->score)
                        {
                                Swap(i, j);

                                Node *temp = i;
                                i = j;
                                j = temp;
                        }
                }
        }
}

int main(void)
{
        struct Postgraduate arr[] =
        {
                {0, "小红", "女", 100},
                {1, "小明", "男", 10},
                {2, "小玲", "女", 50},
                {3, "小丽", "女", 40},
                {4, "小林", "男", 90},
                {5, "小张", "男", 5}
        };
        int arr_count = sizeof(arr) / sizeof(arr[0]);

        struct Postgraduate *head = CreateListDebug(arr, arr_count);

        /*Swap(head->next, head->next->next->next);
        Swap(head->next->next->next, head->next);
        
        Swap(head->next, head->next->next);
        Swap(head->next->next, head->next);
        
        Swap(head->next, head->next->next->next->next->next->next);
        Swap(head->next->next->next->next->next->next, head->next);
        
        Swap(head->next->next->next->next->next, head->next->next->next->next->next->next);
        Swap(head->next->next->next->next->next->next, head->next->next->next->next->next);*/

        SortList(head);

        return 0;
}

无标题.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-15 12:17:38 | 显示全部楼层
其实链表的排序挺简单的,但是楼主说的动态链表是要重新指向么,那个就有一些复杂了,但是单单排序非循环单链表的话这个就是和平常的数组的排序一个思想
下面是一个C++代码
有错请联系俺,会更正,
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <assert.h>
using namespace std;

class Student{
public:
        class LinkNode{
        public:
                int data;
                char name[128];
                LinkNode* next;
        };
        typedef LinkNode* NodePointer;

        Student();//构造默认函数
        void insert(int i, char *inputName, int value);
        friend ostream& operator<<(ostream& out, Student &other);
        friend void sortTheData(Student s1);
        
private:
        NodePointer head;
};

Student::Student(){
        head = NULL;
}

void Student::insert(int i, char *inputName, int value){
        if (i == 1){
                NodePointer s = new LinkNode;
                strcpy(s->name, inputName);
                s->data = value;
                s->next = head;
                head = s;
                cout << "头结点插入成功" << endl;
                return;
        }

        int j = 1;
        NodePointer p = head;
        NodePointer s;
        while (p && j < i - 1){
                j++;
                p = p->next;//找到前驱
        }
        s = new LinkNode;
        strcpy(s->name, inputName);
        s->data = value;
        s->next = p->next;
        p->next = s;
        cout << "插入成功" << endl;
        return;
}


ostream& operator<<(ostream& out, Student &other){
        Student::NodePointer p = other.head;
        while (p){
                out << "姓名:" << p->name << "\t" << "数据:" << p->data << endl;
                p = p->next;
        }
        return out;
}

void sortTheData(Student s1){
        Student::NodePointer p = s1.head;
        while (p){
                Student::NodePointer s = p;
                while (s){
                        if (s->data > p->data){
                                int temp = s->data;
                                s->data = p->data;
                                p->data = temp;

                                char tempName[128];
                                strcpy(tempName, s->name);
                                strcpy(s->name, p->name);
                                strcpy(p->name, tempName);
                        }
                        s = s->next;
                }
                p = p->next;
        }
}


int main(void){
        Student s1;
        int data;
        char name[128];
        for (int i = 1; i <= 3; i++){
                cout << "请输入姓名:";
                cin >> name;
                cout << "请输入数据:";
                cin >> data;

                s1.insert(i, name, data);
        }

        cout << "*****************************" << endl;
        cout << "s1是:" << endl;
        cout << s1 << endl;

        cout << "*****************************" << endl;
        cout << "排序后的s1是:" << endl;
        sortTheData(s1);
        cout << s1;
        return 0;
}

其中的67到86行就是排序的了,,,,
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2018-4-15 12:23:08 | 显示全部楼层
其中这个程序里只能插入三个结点,要是想插入任意个数结点的话就可以在main函数里修改一下就行了
下面是运行结果
请输入姓名:小甲鱼
请输入数据:89
头结点插入成功
请输入姓名:大甲鱼
请输入数据:67
插入成功
请输入姓名:周西瓜
请输入数据:100
插入成功
*****************************
s1是:
姓名:小甲鱼     数据:89
姓名:大甲鱼     数据:67
姓名:周西瓜     数据:100

*****************************
排序后的s1是:
姓名:周西瓜     数据:100
姓名:小甲鱼     数据:89
姓名:大甲鱼     数据:67
请按任意键继续. . .
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2018-4-15 21:03:47 | 显示全部楼层
溯影 发表于 2018-4-15 12:17
其实链表的排序挺简单的,但是楼主说的动态链表是要重新指向么,那个就有一些复杂了,但是单单排序非循环单 ...

谢谢大佬,我学习下
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-16 10:10:35 | 显示全部楼层
思路:按照分数排序,链表不变,把链表里面的数据交换。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Postgraduate      //研究生
{   
        int num;         //学号
        char name[10];   //姓名
        char sex1[4];    //性别
        int score;
        struct  Postgraduate *next;
};

struct Postgraduate *CreateList()
{
        struct Postgraduate *head = (struct Postgraduate *)malloc(sizeof(struct Postgraduate));
        struct Postgraduate *xiaoming = (struct Postgraduate *)malloc(sizeof(struct Postgraduate));
        struct Postgraduate *xiaojiayu = (struct Postgraduate *)malloc(sizeof(struct Postgraduate));
        struct Postgraduate *dajiayu = (struct Postgraduate *)malloc(sizeof(struct Postgraduate));
       
        xiaoming->num = 1;
        strcpy(xiaoming->name, "小明");
        strcpy(xiaoming->sex1, "男");
        xiaoming->score = 66;

        xiaojiayu->num = 2;
        strcpy(xiaojiayu->name, "小甲鱼");
        strcpy(xiaojiayu->sex1, "男");
        xiaojiayu->score = 99;

        dajiayu->num = 3;
        strcpy(dajiayu->name, "大甲鱼");
        strcpy(dajiayu->sex1, "女");
        dajiayu->score = 88;

        head->next = xiaoming;
        xiaoming->next = xiaojiayu;
        xiaojiayu->next = dajiayu;
        dajiayu->next = NULL;
       
        return head;       
}

void Log(struct Postgraduate *head)
{
        while(head->next != NULL)
        {
                head = head->next;
                printf("%d---%s---%s---%d\n", head->num, head->name, head->sex1, head->score);
        }
}

void sort(struct Postgraduate *list)
{
    struct Postgraduate *p = NULL;
    struct Postgraduate *q = NULL;
    int t = 0;
    char Tp[32];
    memset(Tp, 0, 32);
    p = list->next;
    for(; p!=NULL; p=p->next)
    {
        for(q=p->next; q!=NULL; q=q->next)
        {
            if(p->score < q->score)
            {
                t = q->num;
                q->num = p->num;
                p->num = t;

                                strcpy(Tp, q->name);
                                strcpy(q->name, p->name);
                                strcpy(p->name, Tp);
                                memset(Tp, 0, 32);

                                strcpy(Tp, q->sex1);
                                strcpy(q->sex1, p->sex1);
                                strcpy(p->sex1, Tp);
                                memset(Tp, 0, 32);

                t = q->score;
                q->score = p->score;
                p->score = t;
               
            }
        }
    }
    return;  
}

int main()
{
        struct Postgraduate *head = CreateList();
        Log(head);
        sort(head);
        printf("++++++++++++++++++++++++++\n");
        Log(head);
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-1 21:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表