Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我为c+中的字符链表设计了一个排序算法+;但它没有';行不通_C++_Linked List - Fatal编程技术网

C++ 我为c+中的字符链表设计了一个排序算法+;但它没有';行不通

C++ 我为c+中的字符链表设计了一个排序算法+;但它没有';行不通,c++,linked-list,C++,Linked List,我曾尝试为我的链表程序构建一个排序算法,通过检查第一个元素与第二个、第三个等元素,将链表按字母顺序排序,然后将第二个元素与其余元素进行比较等等,但它不起作用。怎么了 struct list* sort_list(struct list *head) { struct list *current= (struct list*)malloc(sizeof(struct list)); struct list *previous=(struct list*)malloc(

我曾尝试为我的链表程序构建一个排序算法,通过检查第一个元素与第二个、第三个等元素,将链表按字母顺序排序,然后将第二个元素与其余元素进行比较等等,但它不起作用。怎么了

   struct list* sort_list(struct list *head)
    {
    struct list *current= (struct list*)malloc(sizeof(struct list));
    struct list *previous=(struct list*)malloc(sizeof(struct list));
    struct list *point=(struct list*)malloc(sizeof(struct list));

    char tmp[30];

      current = head;
      previous = NULL;
      point=head;

        while(point!=NULL)
        {
           while (current != NULL) 
           {
               if(strcmp(point->data,current->data)>0)
               {
                  swap(head,point,current);
               }

               previous = current;

               current = current->next;
            }

          point=point->next;
      }

      return head;

}




    void swap(struct list *head,struct list *first,struct list *second) {

    char *tempValue;
    tempValue = first->data;
    first->data = second->data;
    second->data = tempValue;
}
列表结构:

struct list
{
char *data;
struct list *next;
}list;
替换

while (point != NULL) {
    while (current != NULL) {


将列出的所有代码替换为:

std::list<char> myList;

// fill my list with a bunch of characters

myList.sort();

你能提供列表结构的代码吗?这真的不是
C++
,这一事实的一些死赠品是原始指针、
struct
乱七八糟、
strcmp
malloc
。在C++中,你将用<代码> STD::清单,<代码> STD::String 和 STD::排序。一个问题是,分配代码< >当前< /代码>的空间来指向,然后通过分配<代码>流=头来泄漏它;代码>。上一个和点同上。这是个坏兆头。不必要的malloc会导致内存泄漏。”不使用tmp'和'previous',swap应该只使用2个参数(std::swap(point->data,current->data))。由于您的
swap
不使用第一个参数
head
,传递它真的没有意义,是吗?
std::sort
不适用于列表。你需要这个成员函数。哦。。。打字太快了。更正。有疑问。实际上,正如你在评论中提到的,我需要填写一些字符,例如,用于排序。我已经从一个文件(作业的一部分)中读到了列表中的单词。如何使用它?如果需要对单词进行排序,则可以将其更改为字符串列表:
std::list myList;/*填写字符串*/myList.sort()(注意:这将进行区分大小写的排序;如果需要区分大小写的排序,则需要编写一个比较函数并将其传递到排序方法调用中)
std::list<char> myList;

// fill my list with a bunch of characters

myList.sort();
std::string myList;

// fill my list with a bunch of characters

std::sort(myList.begin(), myList.end());