我做了更改,但 我不能添加超过2个节点它将freez,但如果1或2节点将工作良好,原因是什么?我gave_up 我也没有办法为 这是我的代码,直到时间无法添加节点到链接列表
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
struct info{
int num;
char name[15];
struct info *next;
};
struct info *first,*current,*new_s;
int struct_num;
void add_struct(void);
int main(){
first=NULL;
add_struct();
puts("done");
add_struct();
puts("done");
add_struct();
puts("done");
return(0);
}
//结构添加功能
void add_struct(void){
new_s= malloc (sizeof(struct info));
if(!new_s){
puts("error");
exit (1);
}
if(first==NULL){
first = current= new_s;
first->next = NULL;
}else{
current=first;
while(current->next!=NULL){
current=current->next;
}
current->next=new_s;
current=new_s;
}
struct_num++;
}
标准警告:请[不要投(http://stackoverflow.com/q/605845/2173917)的malloc'的返回值() ''和'C'中的家庭。 –
使用[sentry节点](http:// pastebin。com/JAfq6ep1)可以帮助您避免所有特殊情况,例如空列表,第一个节点,最后一个节点等 – sp2danny