【问题标题】:Sort a Linked list using C使用 C 对链表进行排序
【发布时间】:2016-11-16 03:08:22
【问题描述】:

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

#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 = temp1->data;
            temp1->data = temp2->data;
            temp2->data = a;
        }
    }
}

int main()
{
    struct node * head = NULL;
    push(&head,5);
    push(&head,4);
    push(&head,6);
    push(&head,2);
    push(&head,9);
    printf("List is : ");
    print(head);
    sort(&head);
    printf("after sorting list is : ");
    print(head);
    return 0;
}

下面是我得到的输出:

List is : 9 2 6 4 5 
after sorting list is : 5 4 6 2 9

【问题讨论】:

  • 需要交换条件。
  • 如果要排序,则必须比较值。不只是交换它们。

标签: c data-structures linked-list


【解决方案1】:

无论如何,您都在切换元素。先比较一下,如果 temp2 小于 temp1 则交换它们:

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)
          { 
            if(temp2->data < temp1->data)
              {
                a = temp1->data;
                temp1->data = temp2->data;
                temp2->data = a;
              }
           }
       }
}

【讨论】:

  • 这段代码在哪里将*h重置为列表的新头部?
  • @JonathanLeffler - 节点未排序。节点的内容被交换了:(。在我看来,链接列表排序应该交换节点,而不是节点的内容
  • @alvits 确实如此,尤其是当节点不仅仅是数量时。但是对于这个例子来说,交换实际数字可能更容易
  • 谢谢。其实我错过了。我以为我做到了。
【解决方案2】:

在冒泡排序中,您忘记了交换条件。 在我看来,我建议插入排序

#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;
    if (*h == NULL) {
        temp->next = *h;
        *h = temp;
    } else {
        struct node *tmp = *h;
        struct node *prev = NULL;
        while (1) {
            if (tmp == NULL || tmp->data >= temp->data)
                break;
            prev = tmp;
            tmp = tmp->next; 
        }
        temp->next = tmp;
        if (prev != NULL)
            prev->next = temp;
        else 
            *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");
}

int main()
{
    struct node * head = NULL;
    push(&head,5);
    push(&head,4);
    push(&head,6);
    push(&head,2);
    push(&head,9);
    printf("List is : ");
    print(head);
    //sort(&head);
    printf("after sorting list is : ");
    print(head);
    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-01
    • 2012-08-02
    • 2012-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-27
    • 1970-01-01
    相关资源
    最近更新 更多