简体   繁体   English

qsort() C 库 function 不能在链表上工作吗?

[英]Doesn't qsort() C library function work on linked lists?

I am trying to sort a singly linked list which has been created and all its items,pointers initialized.我正在尝试对已创建的单链表及其所有项目、指针进行排序。 I am trying to use qsort() C library function as shown below.我正在尝试使用 qsort() C 库 function ,如下所示。 It doesn't seem to sort the list.它似乎没有对列表进行排序。 It is giving me compiler error saying: left of 'item' specifies undefined struct/union 'LINKED_LIST_S' at line shown below in the code.它给了我编译器错误说: 'item' 的左边在代码下面显示的行中指定了未定义的结构/联合'LINKED_LIST_S'

struct LINKED_LIST_S
{
         int item;
         struct LINKED_LIST_S * next; 
 } ;

typedef int (*cmpfn)(const void *ptr1, const void *ptr2);

int mylistsort(my_list_t list, cmpfn f1)
{

    qsort ( list , 100, sizeof (struct LINKED_LIST_S), (fn) );
   return -1;
}


int sort_fn_ascend(const void *ptr1, const void *ptr2)
{
    int a = (*(LINKED_LIST_S *)ptr1).item; //Compiler error
   int b = (*(LINKED_LIST_S *)ptr2).item; //Compiler error

  return b - a;

}

int sort_fn_descend(const void *ptr1, const void *ptr2)
{   
   int a = ((struct LINKED_LIST_S *)ptr1)->item; //Compiler error
   int b = ((struct LINKED_LIST_S *)ptr2)->item; //Compiler error

  return a - b;

}

this is how the function mylistsort() is called at 2 places:这就是在 2 个地方调用 function mylistsort() 的方式:

mylistsort(list, sort_fn_descend);//where list a properly initialized linked list pointer. mylistsort(list, sort_fn_descend);//其中list是正确初始化的链表指针。

mylistsort(list, sort_fn_ascend);//where list a properly initialized linked list pointer. mylistsort(list, sort_fn_ascend);//其中list是一个正确初始化的链表指针。

1] Doesn't qsort() work on linked list if the head node poitner is passed as base array(first argument) to qsort. 1] 如果头节点指针作为基数组(第一个参数)传递给 qsort,qsort() 对链表不起作用。

2] How do I achieve the sorting of this linked list using qsort() in above code? 2] 如何在上面的代码中使用 qsort() 实现这个链表的排序?

EDIT: Thanks for the answers.编辑:感谢您的回答。 Now I have implemented a way to sort a list as mentioned by @muksie approach 1] he suggested, as code below.现在我已经实现了一种对列表进行排序的方法,正如他建议的@muksie 方法 1] 所提到的,如下面的代码。 Still qsort() does not return a sorted array of ints.仍然 qsort() 不返回整数的排序数组。 First I want to sort an array of 100 elements, in descending order, but passed array is exact reverse, ie the elements are in ascending order of from 1,2,...100.首先,我想对一个包含 100 个元素的数组按降序排序,但传递的数组完全相反,即元素按从 1,2,...100 的升序排列。 What am I doing wrong?我究竟做错了什么? How can I fix it?我该如何解决?

int linkedListSort(LINKED_LIST_T list, newCompareFunction fn, void *usr_info)
{
    LINKED_LIST_T tmpptr = list;
    int newitems[N_ITEMS];
    int i=0;

    //Logic to get all the items in the list in a array
    while(tmpptr != NULL)
    {
        newitems[i] = tmpptr->item;
        tmpptr = tmpptr->next;
        i++;
    }
    tmpptr = list;
    i = 0;
    //Sort that array
    //qsort ( list , 100, sizeof (struct LINKED_LIST_S), (fn) );
    qsort ( newitems , 100, sizeof(list->item), (fn) );

    //store the sorted items back into the list.
    while(tmpptr != NULL)
    {
        tmpptr->item = newitems[i];
        tmpptr = tmpptr->next;
        i++;
    }

   return -1;
}

int sort_fn_descend(void *ptr1,void *ptr2)
{   
   int a = *((int*)(ptr1));
   int b = *((int*) (ptr2));

  return a - b;

}


int sort_fn_ascend(const void *ptr1, const void *ptr2)
{
    int a = *((int*)(ptr1));
   int b = *((int*) (ptr2));

  return b - a;

}

qsort works on plain arrays of equally sized data, not linked lists. qsort适用于相同大小数据的普通 arrays,而不是链表。

To sort your linked list you can consider the following options:要对链接列表进行排序,您可以考虑以下选项:

  1. Create a plain array with all values of the linked list, sort this, and transform back into a linked list.使用链表的所有值创建一个普通数组,对其进行排序,然后转换回链表。
  2. Implement the quicksort algorithm for your linked list data structure.为您的链表数据结构实现快速排序算法

Since I was able to solve the problem, using approach 2] suggested by @muskie in his answer, I am posting the answer here.由于我能够解决问题,使用@muskie 在他的回答中建议的方法2],我在这里发布了答案。 A bug fix in functions sort_fn_descend/ascend, mentioned in the edited OP was needed, and the code is sorting the list jsut fine.需要在已编辑的 OP 中提到的函数 sort_fn_descend/ascend 中的错误修复,并且代码正在对列表进行很好的排序。 Bug fix FYI - the descend function above should return b - a and ascend fn should return a - b.错误修复仅供参考 - 上面的下降 function 应该返回 b - a 并且上升 fn 应该返回 a - b。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM