我做了更改,但 我不能添加超過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