我想用下面的代碼實現一個鏈表,我得到段錯誤請告訴我問題在哪裏。由於gcc中的指針導致的分段錯誤
我使用Ubuntu的gcc編譯器,請建議我一些
#include<iostream>
using std::cout;
using std::cin;
class ll
{
struct node
{
int info;
node *nextnode ;
}*n;
public:
ll()
{
n=NULL;
}
void getinfo()
{
node *temp,*r;
if(n==NULL)
{
temp=new node;
cout<<" \n enter the first elements of linklist \n";
int z;
cin>>z;
//i guess problem starts here
temp->info=z;
cout<<"the value of info is";
temp->nextnode = NULL;
n=temp;
}
else{
temp=n;
cout<<"heheh balls";
while(temp->nextnode==NULL)
{
temp=temp->nextnode;
}
r=new node;
cout<<"enter the element \t";
int y;
cin>>y;
r->info=y;
r->nextnode=NULL;
temp=r;
}
}
void display()
{
node *temp=n;
while(temp->nextnode==NULL)
{
cout<<temp->info;
}
}
};
int main()
{
ll p;
int v;
cout<<"enter the number of elements to be added to linklist \t";
cin>>v;
//tryn to input linklist from terminal
for(int i=0;i<v;i++)
{
p.getinfo();
}
p.display();
return 0;
}
請選擇它並按下'Ctrl-K'來重新設置代碼格式。 – chill
那麼,在GDB中運行它,你會被告知崩潰的線路以及崩潰時所有變量的值。 –
感謝OliCharlesworth,但我在哪裏可以獲得GDB n我是否已經在g ++中編譯過? – Imposter