2010-07-01 44 views
0

嘿傢伙,抱歉,但我是新來的雙鏈表,並想知道如果有人能告訴我爲什麼我的程序崩潰時,我使用add_end()?在C++中的鏈接列表

#include <iostream> 
using namespace std; 

node *start_ptr = NULL; 
node *current; 
int option = 0; 

void add_end() 
{ 
    node *temp, *temp2; 
    temp = new node; 
    cout << "Enter name: "; 
    cin >> temp->name; 
    cout << "Enter profession: "; 
    cin >> temp->profession; 
    cout << "Enter age: "; 
    cin >> temp->age; 
    temp->nxt = NULL; 
    if (start_ptr = NULL) 
    { 
    start_ptr = temp; 
    current = start_ptr; 
    } 
    else 
    { 
     temp2 = start_ptr; 
     while (temp2->nxt != NULL) 
     { 
      temp2 = temp2->nxt; 
     } 
     temp2->nxt = temp; 
     temp->prv = temp2; 
    } 
} 
+2

請定義 「撞車」。 – 2010-07-01 00:39:34

+1

定義了哪個節點?任何原因,特別是你不使用'std :: list'(或者更好的'std :: vector')? – 2010-07-01 00:39:41

+4

一個可能的錯誤:如果「(start_ptr = NULL)」將start_ptr設置爲NULL。我認爲你的意思是「if(start_ptr == NULL)」 – 2010-07-01 00:41:03

回答

2

我敢打賭,這是if (start_ptr = NULL)不是你打算什麼...你忘了=? if條件將永遠不會被滿足,因爲該語句相當於start_ptr = 0; if (0),然後您的代碼將假定start_ptr可以被取消引用。你把它assing到temp2,然後解引用NULL訪問static_cast<node*>(0)->next ...

0

此行顯然是錯誤的:

if (start_ptr = NULL)