[英]c++ list char acquisition issue, bad memory allocation?
这是我的结构
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的调用也会如此。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.