从C上的简单链表中删除多个节点

从C上的简单链表中删除多个节点,c,list,linked-list,nodes,erase,C,List,Linked List,Nodes,Erase,我想删除与密钥具有相同idteam的所有节点,但它将崩溃。。。我知道它也应该释放内存,但不管怎样,我认为这应该行得通:S //defining the struct struct players { int idplayer; int idteam; struct players *next; }; struct players *first, *last; //the function to delete the nodes void delete(int key){

我想删除与密钥具有相同idteam的所有节点,但它将崩溃。。。我知道它也应该释放内存,但不管怎样,我认为这应该行得通:S

//defining the struct
struct players {
   int idplayer;
   int idteam;
   struct players *next;
 };

 struct players *first, *last;

//the function to delete the nodes
void delete(int key){
 struct players *post;
 struct players *pre;
 struct players *auxi;

 auxi = first; //initialization of auxi
 while(auxi != NULL) //this should run the loop till the end of the list?
 {
    if(auxi->idteam == key){ //condition to delete
        if(auxi == first) first = auxi->next; //erase for the case the node is the first one
        else pre->next = post; //erase in the case the node is anywhere else
      }
      pre = auxi; //saves the current value of auxi
      auxi = auxi->next; //increments the position of auxi
      post = auxi->next; //saves the position of the next element
     }
}
auxi
变为
NULL
时,您将执行
post=(NULL)->next,这是访问冲突(崩溃)

您不需要发布
,只需执行以下操作:

if(auxi->idteam == key){ 
    if(auxi == first) first = auxi->next; 
    else pre->next = auxi->next; // We know auxi is not NULL, so this is safe.
  }
auxi
变为
NULL
时,您将执行
post=(NULL)->next,这是访问冲突(崩溃)

您不需要发布
,只需执行以下操作:

if(auxi->idteam == key){ 
    if(auxi == first) first = auxi->next; 
    else pre->next = auxi->next; // We know auxi is not NULL, so this is safe.
  }
auxi
变为
NULL
时,您将执行
post=(NULL)->next,这是访问冲突(崩溃)

您不需要发布
,只需执行以下操作:

if(auxi->idteam == key){ 
    if(auxi == first) first = auxi->next; 
    else pre->next = auxi->next; // We know auxi is not NULL, so this is safe.
  }
auxi
变为
NULL
时,您将执行
post=(NULL)->next,这是访问冲突(崩溃)

您不需要发布
,只需执行以下操作:

if(auxi->idteam == key){ 
    if(auxi == first) first = auxi->next; 
    else pre->next = auxi->next; // We know auxi is not NULL, so this is safe.
  }
这个函数是错误的

在这个代码片段中

  pre = auxi; //saves the current value of auxi
  auxi = auxi->next; //increments the position of auxi
  post = auxi->next; //saves the position of the next element
事后陈述

  auxi = auxi->next; //increments the position of auxi
  post = auxi->next; //saves the position of the next element
auxi可以等于NULL,因此下一个语句

  auxi = auxi->next; //increments the position of auxi
  post = auxi->next; //saves the position of the next element
导致未定义的行为

但这并不是唯一的错误。此外,还必须正确设置节点
last

您必须释放已删除的节点

该函数可以按以下方式显示

void delete( int key )
{
    struct players *prev = NULL;
    struct players *auxi = first;;

    while ( auxi != NULL )
    {
        if ( auxi->idteam == key )
        {
            struct players *tmp = auxi;

            if ( auxi == first ) first = auxi->next;
            else prev->next = auxi->next;

            if ( auxi == last ) last = prev; 

            auxi = auxi->next; 

            free( tmp );
      }
}
这个函数是错误的

在这个代码片段中

  pre = auxi; //saves the current value of auxi
  auxi = auxi->next; //increments the position of auxi
  post = auxi->next; //saves the position of the next element
事后陈述

  auxi = auxi->next; //increments the position of auxi
  post = auxi->next; //saves the position of the next element
auxi可以等于NULL,因此下一个语句

  auxi = auxi->next; //increments the position of auxi
  post = auxi->next; //saves the position of the next element
导致未定义的行为

但这并不是唯一的错误。此外,还必须正确设置节点
last

您必须释放已删除的节点

该函数可以按以下方式显示

void delete( int key )
{
    struct players *prev = NULL;
    struct players *auxi = first;;

    while ( auxi != NULL )
    {
        if ( auxi->idteam == key )
        {
            struct players *tmp = auxi;

            if ( auxi == first ) first = auxi->next;
            else prev->next = auxi->next;

            if ( auxi == last ) last = prev; 

            auxi = auxi->next; 

            free( tmp );
      }
}
这个函数是错误的

在这个代码片段中

  pre = auxi; //saves the current value of auxi
  auxi = auxi->next; //increments the position of auxi
  post = auxi->next; //saves the position of the next element
事后陈述

  auxi = auxi->next; //increments the position of auxi
  post = auxi->next; //saves the position of the next element
auxi可以等于NULL,因此下一个语句

  auxi = auxi->next; //increments the position of auxi
  post = auxi->next; //saves the position of the next element
导致未定义的行为

但这并不是唯一的错误。此外,还必须正确设置节点
last

您必须释放已删除的节点

该函数可以按以下方式显示

void delete( int key )
{
    struct players *prev = NULL;
    struct players *auxi = first;;

    while ( auxi != NULL )
    {
        if ( auxi->idteam == key )
        {
            struct players *tmp = auxi;

            if ( auxi == first ) first = auxi->next;
            else prev->next = auxi->next;

            if ( auxi == last ) last = prev; 

            auxi = auxi->next; 

            free( tmp );
      }
}
这个函数是错误的

在这个代码片段中

  pre = auxi; //saves the current value of auxi
  auxi = auxi->next; //increments the position of auxi
  post = auxi->next; //saves the position of the next element
事后陈述

  auxi = auxi->next; //increments the position of auxi
  post = auxi->next; //saves the position of the next element
auxi可以等于NULL,因此下一个语句

  auxi = auxi->next; //increments the position of auxi
  post = auxi->next; //saves the position of the next element
导致未定义的行为

但这并不是唯一的错误。此外,还必须正确设置节点
last

您必须释放已删除的节点

该函数可以按以下方式显示

void delete( int key )
{
    struct players *prev = NULL;
    struct players *auxi = first;;

    while ( auxi != NULL )
    {
        if ( auxi->idteam == key )
        {
            struct players *tmp = auxi;

            if ( auxi == first ) first = auxi->next;
            else prev->next = auxi->next;

            if ( auxi == last ) last = prev; 

            auxi = auxi->next; 

            free( tmp );
      }
}