Link lists in C++
Hey guys, sorry for, but I'm new to doubly-linked lists and was wondering if anyone could tell me why my program crashes when i use add_end()?
#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;
}
e开发者_JS百科lse
{
temp2 = start_ptr;
while (temp2->nxt != NULL)
{
temp2 = temp2->nxt;
}
temp2->nxt = temp;
temp->prv = temp2;
}
}
I bet that this if ( start_ptr = NULL )
is not what you intended... Did you forget an =
? The if condition will never be met, as that statement is equivalent to start_ptr = 0; if (0)
, and then your code will assume that start_ptr
can be dereferenced. You assing it to temp2
and then dereference NULL
to access static_cast<node*>(0)->next
...
This line is obviously wrong:
if (start_ptr = NULL)
精彩评论