【问题标题】:Bubble Sorting Linked List冒泡排序链表
【发布时间】:2017-11-11 02:32:52
【问题描述】:

我已经用一个 helper swapper() 制定了一个排序函数。该函数按节点在内存中的地址以降序(从最高地址到最低地址)对列表中的节点进行排序。

只要列表的头部没有改变,函数就可以很好地排序,但是当头部改变时,返回的只是头部以及它之后的任何内容。某处我丢失了列表的其余部分,我无法弄清楚。

到目前为止我的功能:

void swapper(NODE *left, NODE *right)
{
    if(left->prev)
        left->prev->next = right;
    if(right->next)
        right->next->prev = left;

    left->next = right->next;
    right->prev = left->prev;

    right->next = left;
    left->prev = right;
}
NODE *sort_nodes(NODE *head)
{
    NODE *new_second, *new_first, *list = head;
    int swaps;
    do{
        swaps = 0;
        while(list)
        {
            if(&(*list) < &(*(list->next)))
            {
                swapper(list, list->next);
                swaps = 1;
            }
            list = list->next;
        }
        list = head;
    }while(swaps);
    return list;
}

如果列表的头部是列表中声明的第三个节点,则输出示例:

Unsorted: 0x93657050 -> 0x936570d0 -> 0x93657070 -> 0x93657090 -> 0x93657030 -> 0x936570b0 -> 0x93657010 -> NULL
Sorted:   0x93657050 -> 0x93657030 -> 0x93657010 -> NULL

【问题讨论】:

  • if (&amp;(*list) &lt; .. 很奇怪。为什么不只是if (list &lt; ...)
  • 我对您按 pointer 而不是存储在节点中的“值”排序的原因更感兴趣?这真的没有意义,因为指向节点的指针真的彼此没有关系,它们只是任意地址。这有什么要求?
  • 我知道它们的地址确实没有任何价值,但无论如何我想按内存中的属性对它们进行排序。因此,为了获取 struct _Node (NODE) 的地址,我获取了解引用指针的地址。
  • 至于解决问题的另一种可能方法:改为重新创建列表。 “弹出”(删除)列表中的第一个节点,使其成为新列表的头部。然后“弹出”下一个节点,并将其插入新列表中的正确位置。迭代直到没有更多节点。

标签: c sorting linked-list bubble-sort


【解决方案1】:

想想就很简单。

你有

head -> A -> B

然后你交换A和B而不改变头,所以你得到

head -|
      v
 B -> A

如果交换头部元素,则需要将头部指针移动到新头部。

【讨论】:

  • 完美!非常感谢!
【解决方案2】:
#include<stdio.h>
#include<stdlib.h>

/* structure for a node */
struct Node
{
    int data;
    struct Node *next;
};

/* Function to insert a node at the begining of a linked lsit */
void insertAtTheBegin(struct Node **start_ref, int data);

/* Function to bubble sort the given linked lsit */
void bubbleSort(struct Node *start);

/* Function to swap data of two nodes a and b*/
void swap(struct Node *a, struct Node *b);

/* Function to print nodes in a given linked list */
void printList(struct Node *start);

int main()
{
    int arr[] = {12, 56, 2, 11, 1, 90};
    int list_size, i;

    /* start with empty linked list */
    struct Node *start = NULL;

    /* Create linked list from the array arr[].
      Created linked list will be 1->11->2->56->12 */
    for (i = 0; i< 6; i++)
        insertAtTheBegin(&start, arr[i]);

    /* print list before sorting */
    printf("\n Linked list before sorting ");
    printList(start);

    /* sort the linked list */
    bubbleSort(start);

    /* print list after sorting */
    printf("\n Linked list after sorting ");
    printList(start);

    getchar();
    return 0;
}


/* Function to insert a node at the begining of a linked lsit */
void insertAtTheBegin(struct Node **start_ref, int data)
{
    struct Node *ptr1 = (struct Node*)malloc(sizeof(struct Node));
    ptr1->data = data;
    ptr1->next = *start_ref;
    *start_ref = ptr1;
}

/* Function to print nodes in a given linked list */
void printList(struct Node *start)
{
    struct Node *temp = start;
    printf("\n");
    while (temp!=NULL)
    {
        printf("%d ", temp->data);
        temp = temp->next;
    }
}

/* Bubble sort the given linked lsit */
void bubbleSort(struct Node *start)
{
    int swapped, i;
    struct Node *ptr1;
    struct Node *lptr = NULL;

    /* Checking for empty list */
    if (ptr1 == NULL)
        return;

    do
    {
        swapped = 0;
        ptr1 = start;

        while (ptr1->next != lptr)
        {
            if (ptr1->data > ptr1->next->data)
            { 
                swap(ptr1, ptr1->next);
                swapped = 1;
            }
            ptr1 = ptr1->next;
        }
        lptr = ptr1;
    }
    while (swapped);
}

/* function to swap data of two nodes a and b*/
void swap(struct Node *a, struct Node *b)
{
    int temp = a->data;
    a->data = b->data;
    b->data = temp;
}

【讨论】:

    猜你喜欢
    • 2011-09-04
    • 1970-01-01
    • 2012-07-19
    • 2015-08-20
    • 2012-09-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多