这是我的结构
struct list{
char c;
struct list * next;
};
我必须在这个列表中放入一个字符序列,但当我尝试时,我得到了所有时间分段错误
struct list * insert_list(){
//function for add a new string in the list
struct list * tmp;
string car;
int i=0, len;
cin >> car;
len=car.size();
for (i=0; i<len; i++){
tmp=new list;
tmp->c=car[i];
tmp->next=NULL;
tmp=tmp->next;
}
return tmp;
}
struct list * head(struct list *top){
//this function adds the new string on ***TOP*** of the list
struct list *tmp=NULL;
tmp=insert_list();
tmp->next=top;
return tmp;
}
struct list * tail(struct list * top){
//this function adds the new string in the ***END*** of the list
if (top==NULL){
top=insert_list();
}
else{
top->next=tail();
}
return top;
}
我认为问题出在insert_list函数中,我也尝试过使用
while(tmp->c!='n'){
tmp=new list;
cin >> tmp->c; //also with cin.get
tmp=tmp->next;
tmp->next=NULL;
}
但我总是犯同样的错误。我该怎么解决这个问题?
insert_list
将始终返回null,因此对head
的任何调用都将出错。尝试使用结果的对insert_list的任何其他调用也是如此。