Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.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_Sorting_Pointers_Struct - Fatal编程技术网

C 如何对结构进行排序

C 如何对结构进行排序,c,sorting,pointers,struct,C,Sorting,Pointers,Struct,我有C语言的结构,它看起来像: typedef struct node { int id; int salary; struct node *next; char name[30]; char phone[10]; char position[30]; } List; 我还有两个变量列表类型,我想按id对结构进行排序(最大的id将是最后一个)。 我的想法是如何解决这个问题: Do**指针指向*头(*头=列表第一个成员上的指针) 检查**pointer.id是否大于**p

我有C语言的结构,它看起来像:

typedef struct node {
  int id;
  int salary;
  struct node *next;
  char name[30];
  char phone[10];
  char position[30];
} List;
我还有两个变量列表类型,我想按id对结构进行排序(最大的id将是最后一个)。 我的想法是如何解决这个问题:

  • Do**指针指向*头(*头=列表第一个成员上的指针)
  • 检查**pointer.id是否大于**pointer.next.id 如果是->**pointer.next将指向*pointer.next.next **pointer.next.next将指向pointer(&pointer)
  • 下面是解决这个问题的代码(我将在之后做代码和气泡排序算法的“化妆品”)。Firstable只想构建代码,这将是可行的:

    请给我一些方法来解决这个问题(不解决问题。只想知道我的代码中有什么错误,然后自己解决)

    主要功能:

    int main() {
      List *head = (List *) malloc(sizeof(List));
      if(head == NULL) {
        return 1;
      }
    
      head -> id = 332513075;
      head -> salary = 1000;
      strcpy(head -> name, "Name Lastname");
      strcpy(head -> phone, "0587885238");
      strcpy(head -> position, "cleaner");
    
      head -> next = (List *) malloc(sizeof(List));
    
      head -> next->id = 2;
      head -> next->salary = 2000;
      strcpy(head -> next -> name, "Another name");
      strcpy(head -> next -> phone, "1234567890");
      strcpy(head -> next -> position, "CEO");
      head -> next -> next = NULL;
    
      sort(*head);
      print_list(*head);
    
      return 0;
    }
    
    • 第一个错误:
      是一个
      列表
      ,因此
      将是一个
      列表*
      ;但您正试图将其分配到
      列表**

    • 其他原因是将
      currentNode
      视为一个
      列表*


    因此,将
    currentNode
    声明为
    列表*
    将解决列出的错误。

    我认为您需要进行以下修改:

  • 传递磁头地址,因为我认为排序功能需要在排序后更新磁头,并将磁头传递给打印列表: 排序(&head); 打印列表(标题)

  • 排序算法似乎不正确。我不共享算法中的修改,而是共享删除错误和更新头指针的修改:

    无效排序(列表**标题) { int length=getListLength(*head); 列表*currentNode=*头; //请在这里使用currentNode作为列表的指针来实现Algo *head=…;//更新head指针 }


  • 为什么是双指针**currentNode=&head立即修复是
    List*currentNode=&head
    但是我可以看到很多原因说明这个代码不起作用。@rootkonda因为head是第一个结构上的指针。@BalmainVuitton
    head
    在您发布的代码中不是指针。@BalmainVuitton我可以看到,为什么不将head作为指针传递给
    排序
    打印列表
    函数呢?这是正常的做法。
             ^             ~~~~~
    list.c:92:19: error: assigning to 'List' (aka 'struct node') from incompatible type 'List *' (aka 'struct node *'); remove &
        **currentNode = &head;
                      ^ ~~~~~
    list.c:94:21: error: member reference base type 'List *' (aka 'struct node *') is not a structure or union
          if(currentNode->id > currentNode->next->id) {
             ~~~~~~~~~~~^ ~~
    list.c:94:39: error: member reference base type 'List *' (aka 'struct node *') is not a structure or union
          if(currentNode->id > currentNode->next->id) {
                               ~~~~~~~~~~~^ ~~~~
    list.c:95:20: error: member reference base type 'List *' (aka 'struct node *') is not a structure or union
            currentNode->next = currentNode->next->next;
            ~~~~~~~~~~~^ ~~~~
    list.c:95:40: error: member reference base type 'List *' (aka 'struct node *') is not a structure or union
            currentNode->next = currentNode->next->next;
                                ~~~~~~~~~~~^ ~~~~
    list.c:96:20: error: member reference base type 'List *' (aka 'struct node *') is not a structure or union
            currentNode->next->next = &currentNode;
            ~~~~~~~~~~~^ ~~~~
    
    int main() {
      List *head = (List *) malloc(sizeof(List));
      if(head == NULL) {
        return 1;
      }
    
      head -> id = 332513075;
      head -> salary = 1000;
      strcpy(head -> name, "Name Lastname");
      strcpy(head -> phone, "0587885238");
      strcpy(head -> position, "cleaner");
    
      head -> next = (List *) malloc(sizeof(List));
    
      head -> next->id = 2;
      head -> next->salary = 2000;
      strcpy(head -> next -> name, "Another name");
      strcpy(head -> next -> phone, "1234567890");
      strcpy(head -> next -> position, "CEO");
      head -> next -> next = NULL;
    
      sort(*head);
      print_list(*head);
    
      return 0;
    }