gpt4 book ai didi

c - 删除最后一个链接列表

转载 作者:行者123 更新时间:2023-11-30 16:01:43 25 4
gpt4 key购买 nike

我似乎无法摆脱额外的链表。我知道有很多方法可以做到这一点,但我只想一种简单的方法来释放它,因为它是作为我的循环中的额外内容创建的。这是代码:

    current = first = malloc (sizeof (NODE));
while( fscanf( fin, "%s", current -> seq) != EOF) {
for (i = 0; i < 300; i++){
if (current->seq[i] == 'a')
current->seq[i] = 'A';
else if (current->seq[i] == 't')
current->seq[i] = 'T';
else if(current->seq[i] == 'g')
current->seq[i] = 'G';
else if(current->seq[i] == 'c')
current->seq[i] = 'C';
}
if ( (current -> next = malloc ( sizeof(NODE) ) ) == NULL){
fprintf(fout, "Out of memory\nCan't add more DNA sequences\n");
return EXIT_FAILURE;
}
current = current -> next;
}

最佳答案

将代码的最后一部分替换为

prev= NULL;
current = first =...
...
} //end if
prev = current;
current = current->next;
} //end while

free(current)
if(prev !=NULL)
prev->next = Null;

关于c - 删除最后一个链接列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6210425/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com