首页 > 编程语言 > 详细

C语言动态链表的排序(从小到大) 2021.07.14

时间:2021-07-14 18:20:41      阅读:25      评论:0      收藏:0      [点我收藏+]
//现在的主要问题是,排序的列表中不能出现0,原因是与NULL冲突。
#include <stdio.h>
#include <stdlib.h>
#define MAX 999999

typedef struct LNode//重命名struct LNode为LNode 
{
    int data;
    LNode* next;//在结构体中可以直接使用新名字LNode 
}LNode;

void CreateLinklist(LNode*& head)
{
    printf("请输入数字创建链表,以9999结束。\n");
    head = (LNode*)malloc(sizeof(LNode));
    head->next = nullptr;
    LNode* flag;
    flag = head;
    int t;
    scanf_s("%d", &t);
    while (t != 9999)
    {
        flag->data = t;
        flag->next = (LNode*)malloc(sizeof(LNode));
        flag = flag->next;
        scanf_s("%d", &t);
    }
    flag->next = nullptr;
    flag = head;
}

void printLinklist(LNode* p)
{
    if (p->data == NULL)
        printf("链表为空。\n");
    else
    {
        printf("链表的结构为:\n");
        while (p->next != nullptr)
        {
            printf("%d", p->data);
            p = p->next;
            if (p->next != nullptr)
                printf(" -> ");
        }
    }
}

void CopyLinklist(LNode*& head, LNode* b)
{
    head = (LNode*)malloc(sizeof(LNode));
    head->next = nullptr;
    LNode* flag;
    flag = head;
    while (b->next != nullptr)
    {
        flag->data = b->data;
        flag->next = (LNode*)malloc(sizeof(LNode));
        flag = flag->next;
        b = b->next;
    }
    flag->next = nullptr;
    flag = head;
}


void sort(LNode*& headA, LNode* p)
{
    LNode* copy;
    CopyLinklist(copy, p);
    headA = (LNode*)malloc(sizeof(LNode));
    headA->next = nullptr;
    LNode* flagA;
    LNode* flagB;
    LNode* flagC;
    flagA = headA;
    flagB = copy;
    flagC = copy;
    int g;
    int num = 0;
    while (copy->next != nullptr)
    {
        copy = copy->next;
        num++;
    }
    printf("该排序链表共有%d个结点。\n", num);
    copy = flagB;
    g = copy->data;
    if (num == 0)
        printf("该排序链表为空!");
    else
    {
        while (num > 0)
        {
            while (copy->next != nullptr)
            {
                if (g > copy->data && copy->data != NULL)
                {
                    g = copy->data;
                    flagC = copy;
                }
                copy = copy->next;
            }
            flagC->data = MAX;//将当前最小值设为一个原链表绝对不可能达到的极大值
            flagA->data = g;
            flagA->next = (LNode*)malloc(sizeof(LNode));
            flagA = flagA->next;
            flagA->next = nullptr;
            copy = flagB;
            g = MAX;
            num--;
        }
        flagA = headA;
    }
}

int main()
{
    LNode* LA;
    CreateLinklist(LA);
    printf("\n");
    printLinklist(LA);
    printf("\n");
    printf("\n");
    LNode* LB;
    sort(LB , LA);
    printf("\n");
    printLinklist(LB);
    printf("\n");
    printLinklist(LA);
    return 0;
}
现在的主要问题是,排序的列表中不能出现0,原因是与NULL冲突。

C语言动态链表的排序(从小到大) 2021.07.14

原文:https://www.cnblogs.com/XueQun/p/15011260.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!