⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sort.c

📁 链表排序功能的实现
💻 C
字号:
/***************************************
Filename:linksort
Programmer:Zheng Bolong(ZBL)
Teacher:Wu Tao(WT)
Created:09.3.24
****************************************/


#include<stdio.h>

struct number
{
    int data;                                                /*建立结构体*/
    struct number *next;
};



/*struct number *sort(struct number *head)
{
    struct number *temp,*p;
    for(p=head;p->next->next!=NULL;p=p->next)
    {
        for(;)
        {
            temp=p->next->next;
            p->next->next=p;
            p->next=temp;

        }
    }
    return(head);
}
交换指针的方法不会实现*/


struct number *sort(struct number *head)
{
    struct number *p1,*p;
    int temp;
    for(p=head;p!=NULL;p=p->next)
    {
        for(p1=p->next;p1!=NULL;p1=p1->next)
        {
            if((p->data)>(p1->data))                          /*排序函数*/
            {
                temp = p1->data;
                p1->data = p->data;
                p->data = temp;
            }
        }
    }
    return(head);
}





struct number *creat(void)
{
    struct number *head;
    struct number *p1,*p2;
    int n = 0;
    p1=p2=(struct number *)malloc(sizeof(struct number));
    scanf("%d",&p1->data);
    head=NULL;
    while(p1->data!=0)
    {
        n=n+1;                                                /*建立链表*/
        if(n==1)head=p1;
        else p2->next=p1;
        p2=p1;
        p1=(struct number *)malloc(sizeof(struct number));
        scanf("%d",&p1->data);
    }
    p2->next=NULL;
    return(head);
}

void main()
{
    int n;
    struct number *head;
    struct number *p;
    printf("Enter the number of nodes to sort:");
    scanf("%d",&n);
    printf("The number of nodes to sort is:%d\n",n);
    head=creat();
    head=sort(head);                                          /*主函数*/
    p=head;
    while(p->data!=0)
    {
        printf("%d\n",p->data);
        p=p->next;
    }
    getch();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -