排序链表最简单的方法

时间:2016-03-10 11:01:25

标签: c singly-linked-list

我正在尝试为链接列表编写非常基本的排序方法。我得到了未处理的异常。我犯的错是什么?这是我的代码: -

object.incrementKey("Likes")
object.addObject(PFUser.currentUser()!, forKey: "LikesUsers")
object.saveEventually()

以下函数创建链接列表

   struct LinkedNode// structure for linked list
        {
            int data;
            struct LinkedNode *next;
        }*start = NULL;

以下函数用于对链表节点进行排序

 void CreateLinkedList()
        {
        LinkedNode *newNode, *current;
        printf("enter 5 numbers to create linked list\n");
        for(int i=0; i<5; i++)
            {
            newNode = (struct LinkedNode *)malloc(sizeof(LinkedNode));
            scanf("%d", &newNode->data);
            newNode->next = NULL;
            if(start == NULL)
                {
                start = newNode;
                current = newNode;
                }
            else
                {
                current->next = newNode;
                current = newNode;
                }
           }
        }

3 个答案:

答案 0 :(得分:0)

尝试此代码

void SortLinkedList()
    {
    struct LinkedNode *node=NULL, *temp = NULL;
    int tempvar;//temp variable to store node data
    node = start;
    //temp = node;//temp node to hold node data and next link
    while(node != NULL)
    {
        temp=node; 
        while (temp->next !=NULL)//travel till the second last element 
        {
           if(temp->data > temp->next->data)// compare the data of the nodes 
            {
              tempvar = temp->data;
              temp->data = temp->next->data;// swap the data
              temp->next->data = tempvar;
            }
         temp = temp->next;    // move to the next element 
        }
        node = node->next;    // move to the next node
    }
}

1 - 外部while循环用于对链表进行排序所需的传递总数。

2-在第二个while循环中,我们实际上是在比较我们想要排序的节点的数据

答案 1 :(得分:0)

是的,使用节点/链接对链接列表进行排序是一项艰巨的工作。自己花几个小时做,但是既然我做了,为什么不帮助别人。 您只需要在列表中找到最小值即可。将其与头节点和递归替换为head-> next。 如果您具有FindMin()和Swap()函数,则排序代码仅3至4行。 这是sort(),swap()和findmin()的完整代码。

void sort(node **start)
{
    if (((*start)->next == NULL) || (*start == NULL))
    {
        return;
    }
    node *min = findmin(*start);
    swap(*start, min, start);
    sort(&((*start)->next));
}

void swap(node *p1, node *p2, node **start)
{
    node *p1pre = NULL;
    node *p1curr = *start;
    while (p1curr!=p1)
    {
        p1pre = p1curr;
        p1curr = p1curr->next;
    }
    node *p2pre = NULL;
    node *p2curr = *start;
    while (p2curr != p2)
    {
        p2pre = p2curr;
        p2curr = p2curr->next;
    }
    if (p1pre != NULL)
    {
        p1pre->next = p2curr;
    }
    else
    {
        *start = p2curr;
    }
    if (p2pre != NULL)
    {
        p2pre->next = p1curr;
    }
    else
    {
        *start = p1curr;
    }
    node *temp = p2curr->next;
    p2curr->next = p1curr->next;
    p1curr->next = temp;
}

node* findmin(node *start)
{
    int flag = 0;
    if (start == NULL)
    {
        cout << "list is empty" << endl;
    }
    else
    {
        node *curr = start->next;
        node *min = start;
        while (curr->next != NULL)
        {
            if (min->value > curr->value)
            {
                min = curr;
                flag++;
            }
            curr = curr->next;
        }
        if ((curr->next == NULL) && (min->value > curr->value))
        {
            min = curr;
            flag++;
        }
        if (flag > 0)
        {
            return min;
        }
    }
}

答案 2 :(得分:0)

您可以只使用qsort来代替实现自己的排序。当然,qsort需要一个数组,但是创建起来很容易。

在这种情况下,您知道列表有5个成员。但是,如果您不知道,就可以算一下。

int size_of_list = 0;
struct LinkedNode *p;

for (p = start; p != NULL; p = p->next) ++size_of_list;

if (size_of_list == 0) {
    // Nothing to do
    return;
}

现在您可以创建数组;

struct LinkedNode *arr[size_of_list + 1], **arrp = arr;
for (p = start; p != NULL; p = p->next) *arrp++ = p;
*arrp = NULL;

然后,在qsort上使用arr。有很多示例,但诀窍是编写适当的比较函数。

int cmp_LinkedNode(const void *a, const void *b) {
    const struct LinkedNode * const *aa = a;
    const struct LinkedNode * const *bb = b;
    return ((*aa)->data > (*bb)->data) - ((*aa)->data < (*bb)->data);
}

//...
qsort(arr, size_of_list, sizeof(*arr), cmp_LinkedNode);

然后,将列表重新排列为已排序的顺序。

for (int i = 0; i < size_of_list; ++i) arr[i]->next = arr[i+1];
start = arr[0];
相关问题