小编Viv*_*vek的帖子

使用 C 对链表进行排序

我正在尝试对链接列表进行排序,但无法做到。下面是我的代码。谁能帮我。我也看过一些程序,排序链表的方法也是这样的。

#include <stdio.h>
#include <stdlib.h>

struct node {
    int data;
    struct node *next;
};

int push(struct node **h, int x)
{
    struct node *temp = (struct node*)malloc(sizeof(struct node));
    temp->data = x;
    temp->next = *h;
*h = temp;
    return 0;
}

void print(struct node *head)
{
    struct node *temp = head;
    while(temp != NULL)
    {
        printf("%d ",temp->data);
        temp = temp->next;
    }
    printf("\n");
}

void sort(struct node **h)
{
    int i,j,a;

    struct node *temp1;
    struct node *temp2;

    for(temp1=*h;temp1!=NULL;temp1=temp1->next)
    {
        for(temp2=temp1->next;temp2!=NULL;temp2=temp2->next)
        {
            a …
Run Code Online (Sandbox Code Playgroud)

c linked-list data-structures

4
推荐指数
1
解决办法
2万
查看次数

标签 统计

c ×1

data-structures ×1

linked-list ×1