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/9/ios/109.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_Struct_Singly Linked List_Selection Sort - Fatal编程技术网

C中不兼容指针类型的赋值

C中不兼容指针类型的赋值,c,sorting,struct,singly-linked-list,selection-sort,C,Sorting,Struct,Singly Linked List,Selection Sort,我有一个结构的链表, 这是我的结构: typedef struct avion { int code; int capacite; char etat[1]; int date; int nvols; } avion; typedef struct element *list; typedef struct element { avion A; struct element *svt; } element; 我想根据结构“capacle

我有一个结构的链表, 这是我的结构:

typedef struct avion
{
    int code;
    int capacite;
    char etat[1];
    int date;
    int nvols;
} avion;

typedef struct element *list;
typedef struct element
{
    avion A;
    struct element *svt;
} element;
我想根据结构“capacle”的元素对链表进行升序排序。 以下是功能tri的代码:

list *tri(list *L)
{
   list *i,*j,*min;
   avion  x;
   for (i=L; (*i)->svt != NULL; i=(*i)->svt)
   {
      min=i;
      for (j=(*i)->svt; j != NULL; j=(*j)->svt)
      {
        if ((*j)->A.capacite < (*min)->A.capacite)
           min=j;
      }
       if (min != i)
      {
       x=(*min)->A;
       (*min)->A = (*i)->A;
       (*i)->A = x;
    }
}
return(L);
}
list*tri(list*L)
{
列表*i,*j,*min;
avion x;
对于(i=L;(*i)->svt!=NULL;i=(*i)->svt)
{
min=i;
对于(j=(*i)->svt;j!=NULL;j=(*j)->svt)
{
如果((*j)->A.电容<(*min)->A.电容)
min=j;
}
如果(最小!=i)
{
x=(*min)->A;
(*min)->A=(*i)->A;
(*i)->A=x;
}
}
返回(L);
}
但是我有一个警告:来自不兼容指针的赋值(在for循环的两行中):我不知道如何修复它


有没有更好的方法根据这个标准对我的链表进行排序?

例如在这个For循环中

for (i=L; (*i)->svt != NULL; i=(*i)->svt)
变量
i
声明为

list *i
具有类型
struct element**
。另一方面,数据成员
svt
具有类型
struct element*
。因此此赋值

i=(*i)->svt
包含不同类型的操作数,并且没有从类型
struct元素*
到类型
struct元素**
的隐式转换

请注意,如果由于此表达式而调用空列表,则函数可以调用未定义的行为

(*i)->svt != NULL;
还有一个元素数组的声明

char etat[1];
没有什么意义

为这样的指针引入这样的别名

typedef struct element *list;
一般来说,这不是一个好主意。它只会让代码的读者感到困惑

无需通过指向头部节点的指针,通过引用将指向头部节点的指针传递给函数
tri
(实现选择排序),因为指针本身在函数中未被更改

该函数可以按照下面的演示程序中所示的方式进行声明和定义

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef struct avion
{
    int capacite;
} avion;

typedef struct element list;
typedef struct element
{
    avion A;
    struct element *svt;
} element;

int push_front( list **head, int capacite )
{
    element *new_element = malloc( sizeof( element ) );
    int success = new_element != NULL;
    
    if ( success )
    {
        new_element->A.capacite = capacite;
        new_element->svt = *head;
        
        *head = new_element;
    }
    
    return success;
}

void display( const list *head )
{
    for ( ; head != NULL; head = head->svt )
    {
        printf( "%d -> ", head->A.capacite );
    }
    
    puts( "null" );
}

void tri( list *head )
{
    for ( ; head != NULL; head = head->svt )
    {
        element *min = head;
        
        for ( element *current = head->svt; current != NULL; current = current->svt )
        {
            if ( current->A.capacite < min->A.capacite )
            {
                min = current;
            }
        }
        
        if ( min != head )
        {
            avion tmp = min->A;
            min->A = head->A;
            head->A = tmp;
        }
    }
}

int main(void) 
{
    enum { N = 10 };
    list *head = NULL;
    
    srand( ( unsigned int )time( NULL ) );

    for ( int i = 0; i < N; i++ )
    {
        push_front( &head, rand() % N );
    }
    
    display( head );
    
    tri( head );
    
    display( head );

    return 0;
}
如果要使用名称的别名定义
列表
,则该函数的外观与此演示程序中所示的相同

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef struct avion
{
    int capacite;
} avion;

typedef struct element *list;
typedef struct element
{
    avion A;
    struct element *svt;
} element;

int push_front( list *head, int capacite )
{
    element *new_element = malloc( sizeof( element ) );
    int success = new_element != NULL;
    
    if ( success )
    {
        new_element->A.capacite = capacite;
        new_element->svt = *head;
        
        *head = new_element;
    }
    
    return success;
}

void display( list head )
{
    for ( ; head != NULL; head = head->svt )
    {
        printf( "%d -> ", head->A.capacite );
    }
    
    puts( "null" );
}

void tri( list head )
{
    for ( ; head != NULL; head = head->svt )
    {
        element *min = head;
        
        for ( element *current = head->svt; current != NULL; current = current->svt )
        {
            if ( current->A.capacite < min->A.capacite )
            {
                min = current;
            }
        }
        
        if ( min != head )
        {
            avion tmp = min->A;
            min->A = head->A;
            head->A = tmp;
        }
    }
}

int main(void) 
{
    enum { N = 10 };
    list head = NULL;
    
    srand( ( unsigned int )time( NULL ) );

    for ( int i = 0; i < N; i++ )
    {
        push_front( &head, rand() % N );
    }
    
    display( head );
    
    tri( head );
    
    display( head );

    return 0;
}
#包括
#包括
#包括
类型定义结构
{
内部电容;
}avion;
类型定义结构元素*列表;
类型定义结构元素
{
avion A;
结构元素*svt;
}元素;
内部推前(列表*头部,内部电容)
{
元素*新元素=malloc(sizeof(元素));
int success=new_元素!=NULL;
如果(成功)
{
新元件->A.capacity=电容;
新_元素->svt=*头部;
*head=新的_元素;
}
回归成功;
}
无效显示(列表头)
{
for(;head!=NULL;head=head->svt)
{
printf(“%d->”,头部->A.capacity);
}
看跌期权(“空”);
}
无效tri(列表标题)
{
for(;head!=NULL;head=head->svt)
{
元素*min=头部;
对于(元素*current=head->svt;current!=NULL;current=current->svt)
{
如果(电流->交流电容<最小->交流电容)
{
最小值=电流;
}
}
如果(最小!=头部)
{
avion tmp=min->A;
最小->A=头部->A;
头部->A=tmp;
}
}
}
内部主(空)
{
枚举{N=10};
列表头=空;
srand((无符号整数)时间(NULL));
对于(int i=0;i我不是元素上的指针,而是元素指针上的指针。

你的
列表
是一个
结构元素*
,一个指针。因此你的
列表*
结构元素**
。当你必须更新指针时,例如当你插入或删除项目或当您交换它们时。但您的代码不会这样做:它不会交换节点,它只交换数据。因此,头也不会更改。只需使用
struct element*
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef struct avion
{
    int capacite;
} avion;

typedef struct element *list;
typedef struct element
{
    avion A;
    struct element *svt;
} element;

int push_front( list *head, int capacite )
{
    element *new_element = malloc( sizeof( element ) );
    int success = new_element != NULL;
    
    if ( success )
    {
        new_element->A.capacite = capacite;
        new_element->svt = *head;
        
        *head = new_element;
    }
    
    return success;
}

void display( list head )
{
    for ( ; head != NULL; head = head->svt )
    {
        printf( "%d -> ", head->A.capacite );
    }
    
    puts( "null" );
}

void tri( list head )
{
    for ( ; head != NULL; head = head->svt )
    {
        element *min = head;
        
        for ( element *current = head->svt; current != NULL; current = current->svt )
        {
            if ( current->A.capacite < min->A.capacite )
            {
                min = current;
            }
        }
        
        if ( min != head )
        {
            avion tmp = min->A;
            min->A = head->A;
            head->A = tmp;
        }
    }
}

int main(void) 
{
    enum { N = 10 };
    list head = NULL;
    
    srand( ( unsigned int )time( NULL ) );

    for ( int i = 0; i < N; i++ )
    {
        push_front( &head, rand() % N );
    }
    
    display( head );
    
    tri( head );
    
    display( head );

    return 0;
}