2010-07-01 58 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)