2011-11-11 56 views
-1

我想用下面的代碼實現一個鏈表,我得到段錯誤請告訴我問題在哪裏。由於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; 
} 
+0

請選擇它並按下'Ctrl-K'來重新設置代碼格式。 – chill

+5

那麼,在GDB中運行它,你會被告知崩潰的線路以及崩潰時所有變量的值。 –

+0

感謝OliCharlesworth,但我在哪裏可以獲得GDB n我是否已經在g ++中編譯過? – Imposter

回答

2
while(temp->nextnode==NULL) 
{ 
    temp=temp->nextnode; 
} 
.... 
temp=r; 

應該

while(temp->nextnode!=NULL) 
{ 
    temp=temp->nextnode; 
} 
.... 
temp->nextnode=r; 

也是一樣的:

void display() 
{ 
    node *temp=n; 
    while(temp->nextnode==NULL) 
    { 
     cout<<temp->info; 

    } 
} 

應該是:

void display() 
{ 
    node *temp=n; 
    while(temp!=NULL) 
    { 
     cout<<temp->info; 
     temp = temp->nextnode; 
    } 
} 
+0

謝謝@KoKoToru你可以告訴部分代碼導致段錯誤 – Imposter

+0

你訪問了0分..因爲你的時間錯了 – KoKuToru

2

有在代碼中幾個錯誤,下面看:

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->nextnode = r; 
     } 
    } 

下一頁功能:

void 
    display() 
    { 
    node *temp = n; 

下一行改變,保持印刷數量,而你有一個節點

while (temp) 
    { 
    cout << temp->info; 

和唐別忘了搬到下一個節點

 temp = temp->nextnode; 
     } 
    } 

就是這樣,它的工作原理。

+0

謝謝寒意,但我對語句「temp = new node;」有疑問。當我們聲明節點* temp;上面「temp = new node」的用法是什麼? – Imposter

+0

@Imposter,它只是爲列表分配一個新的元素。對於'n == NULL'的情況,你可以簡單地說'n = new node;'並且在'if'的整個分支中使用'n'。 – chill

+0

感謝您的關注。所以如果我不寫下語句「temp = new node;」並繼續在上面的代碼中的節點* temp將會導致什麼後果。 – Imposter