Vielen Dank im Voraus.Doppelt verkettete Liste Core Dump
Ich mache eine doppelt verkettete Liste.
Alles funktionierte gut, aber ich erkannte, dass, wenn ich einen neuen Klassenknoten irgendwo in der Mitte hinzufügte, der linke Zeiger immer noch auf den zuvor davor liegenden Knoten zeigen würde (jetzt zwei Leerzeichen entfernt).
So habe ich einen neuen Knotenzeiger auf der Leitung 46.
Dann on line 51 Ich sagte, dass der Knoten nun auf den neuen Knoten zu zeigen.
So:
Zuerst musste ich neue Knoten Temperatur im Raum off
Dann habe ich Zeiger
temp2
Schleife durch die ListeSchließlich ich
temp3
machen sagen, an den Knoten-zu-Punkt nachtemp2
's Knoten
Nachdem die Funktion ausgeführt wird, sollte die Reihenfolge temp2->temp->temp3
Mein Hauptpunkt sein: Nachdem ich die Leitung 51, Dumps mein Programm hinzugefügt Kern (Segmentation Fault) und schließt aus.
Wie kann ich das beheben? Es passiert nur, wenn ich etwas hinzufüge, das nicht vom Kopfzeiger stattfindet.
void add(node *&head, node *&tail, node *¤t)
{
node *temp = new node; //creates a pointer pointing to a new class node
cin >> temp->letter; // user input
current = head; // creates a pointer to point at the first node
while (current != NULL) // while list isn't empty
{
if (current->letter == temp->letter)
{ // letter already exists
cout << "DUPLICATE: " << temp->letter << endl << endl;
return;
}
else
{ // loop through list moving tail pointer to the end while checking for 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 and
tail = temp; // tail both point to first and only node.
}
else
{ // if new letter value is less than head value
if(temp->letter < head->letter)
{
temp->right_link = head; // node points (right) to head
temp->left_link = NULL; // left most node point to nothing.
head->left_link = temp; // head (currently the second node) points (left) to first node
head = temp; // head pointer moves to the first position
}
else
{ // if new node goes anywhere other than head
node *temp2 = head; // new node to cycle through list
while(temp2->right_link != NULL && temp2->right_link->letter < temp->letter)
{ // if temp2 points to a node and that node's value is less than temp node value
temp2 = temp2->right_link;
}
node *temp3 = temp2->right_link;
temp->right_link = temp2->right_link; // when temp2 stops looping, temp will point to
// the same node as temp2.
temp2->right_link = temp; // temp2's current node will point to temp, causing temp
// to be added into the list (after temp2)
temp3->left_link = temp; // point the node (after the newly inserted node) left to new node
temp->left_link = temp2; // connects the left pointer between temp and temp2
if(temp->right_link == NULL)
tail = temp;
}
}
cout << "ADDED : " << temp->letter << endl << endl;
}
Könnten Sie einen [mcve] posten? Hast du auch einen Debugger benutzt? – xvan