c++ - Doubly linked list Core dump -
thanks in advance.
i making doubly linked list.
everything working fine, realized when added new class node somewhere in middle, left pointer still pointing @ node before (now 2 spaces away).
so added new node pointer on line 46.
then on line 51 told node point new node.
so :
first had new node temp off in space
then make pointer
temp2
loop through listlastly tell
temp3
point node aftertemp2
's node
after function runs, order should temp2->temp->temp3
my main point: after added line 51, program core dumps(segmentation fault) , closes out.
how can fix this? happens when add isn't taking place of head pointer.
void add(node *&head, node *&tail, node *¤t) { node *temp = new node; //creates pointer pointing new class node cin >> temp->letter; // user input current = head; // creates pointer point @ first node while (current != null) // while list isn't empty { if (current->letter == temp->letter) { // letter exists cout << "duplicate: " << temp->letter << endl << endl; return; } else { // loop through list moving tail pointer end while checking duplicates tail = current; current = current->right_link; } } current = temp; // current = new added node if (isempty(head)) { // if first node temp->left_link = null; temp->right_link = null; head = temp; // head , tail = temp; // tail both point first , node. } else { // if new letter value less head value if(temp->letter < head->letter) { temp->right_link = head; // node points (right) head temp->left_link = null; // left node point nothing. head->left_link = temp; // head (currently second node) points (left) first node head = temp; // head pointer moves first position } else { // if new node goes anywhere other head node *temp2 = head; // new node cycle through list while(temp2->right_link != null && temp2->right_link->letter < temp->letter) { // if temp2 points node , node's value less temp node value temp2 = temp2->right_link; } node *temp3 = temp2->right_link; temp->right_link = temp2->right_link; // when temp2 stops looping, temp point // same node temp2. temp2->right_link = temp; // temp2's current node point temp, causing temp // added list (after temp2) temp3->left_link = temp; // point node (after newly inserted node) left new node temp->left_link = temp2; // connects left pointer between temp , temp2 if(temp->right_link == null) tail = temp; } } cout << "added : " << temp->letter << endl << endl; }
if temp2->right_link == null
46 node *temp3 = temp2->right_link;
is null pointer, can't
51 temp3->left_link = temp;
which should have been obvious if used debugger.
Comments
Post a Comment