[英]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.