我想刪除所有與key具有相同idteam的節點,但它會崩潰......我知道它也應該釋放()內存,但無論如何,我認爲這應該工作:S從C上的簡單鏈表中刪除多個節點
//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
}
}
http://stackoverflow.com/a/30578168/971127 – BLUEPIXY