简体   繁体   English

对 c 中结构的链表进行排序

[英]sort a linked list of struct in c

I am trying to sort a linked list in an ascending order according to the element of the struct"capacite".我正在尝试根据结构“容量”的元素以升序对链表进行排序。 But it doesn't give a result.但它没有给出结果。 What is the mistake i am making?我犯了什么错误? Here is my code这是我的代码

typedef struct avion avion;

typedef avion*  aliste;

struct avion
{
    char code[20];
    int capacite;
    char etat;
    int date;
    int nvols;
    aliste svt;
};

i have created the list.我已经创建了列表。 and following the function that sort it:并按照 function 对其进行排序:

void tri(aliste L)
{
   aliste i,j,min,tmp;
   for (i=L; i->svt != NULL; i=i->svt)
   {
      min=i;
      for (j=i->svt; j != NULL; j=j->svt)
      {
        if (j->capacite < min->capacite)
           min=j;
      }
       if (min != i)
      {
       tmp=min;
       min=i;
       i=tmp;
      }
    }
}

It looks like you are trying to bubble sort your list.看起来您正在尝试对列表进行冒泡排序。 A quick and dirty fix would be to swap the contents of your nodes:一个快速而肮脏的解决方法是交换节点的内容:

void tri(aliste L)
{
   aliste i,j,min;
   avion tmp;
      ...
      if (min != i)
      {
       tmp=*min;
       *min=*i;
       *i=tmp;
      }
    }
}

The good news here is that the L pointer passed from the caller still points to the beginning of the list.这里的好消息是,从调用者传递的 L 指针仍然指向列表的开头。 The downside if that it copies the structures while changing only the svt pointers would be much more efficient.如果它在仅更改svt指针的同时复制结构,那么它的缺点会更加有效。 But:但:

  • you will have to return to the caller a pointer to the new head您将不得不向调用者返回一个指向新头的指针
  • moving elements from a singly linked list requires to record the previous element从单链表中移动元素需要记录前一个元素

As you use bubble sort which is not a very efficient algorithme, I assume that you only intend to process small lists and that performance is not essential, so you are on your own...由于您使用的冒泡排序不是一种非常有效的算法,因此我假设您只打算处理小列表并且性能不是必需的,因此您只能靠自己...

A common way to implement a linked list of structs in C is in https://www.geeksforgeeks.org/linked-list-set-1-introduction/在 C 中实现结构链表的常用方法是在https://www.geeksforgeeks.org/linked-list-set-1-introduction/

Basically inside the struct there is a pointer next to the next node in the linked list, what establishes the linked list functionality:基本上在结构内部,链表中的下一个节点next有一个指针,它建立了链表功能:

struct Node { 
    int data; 
    struct Node* next; 
}; 

For this more common implementation of a linked list in C you find source code for many sorting algorithms (qsort, mergesort,...) on the net.对于 C 中这种更常见的链表实现,您可以在网上找到许多排序算法(qsort、mergesort...)的源代码。 You have to implement functionality for more than one parameter in such an example because your nodes (of the linked list) have multiple data fields...在这样的示例中,您必须为多个参数实现功能,因为您的节点(链表的)具有多个数据字段......

Sorting linked list by multiple parameters 通过多个参数对链表进行排序

typedef avion* aliste; Is it a good idea to typedef pointers? typedef 指针是个好主意吗?

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

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