Похоже, что в «SortedInsert» голова всегда равна нулю, а затем код segfaults в любом случае ... действительно разочаровывает. Любая идея, почему голова всегда равна нулю, хотя я ее что-то задал, а затем почему код segfaults вообще? БлагодаряВставить в отсортированный связанный список segfaults
#include <iostream>
#include <cassert>
#include <string>
#include <stdlib.h>
#include <sstream>
using namespace std;
struct Node {
Node* next = 0;
int data;
~Node(){
if (next != 0){
delete next;
}
}
};
void SortedInsert(Node* head, int value){
if(head == 0){
Node* header = new Node;
header->data = value;
head = header;
return;
}
cout << "TEST" << endl;
Node* temp = head;
while(temp != 0){
if(value > temp->data){
Node* insert = temp->next;
Node* otherTemp = new Node;
otherTemp->data = value;
temp->next= otherTemp;
temp->next->next = insert;
}
temp=temp->next;
}
return;
}
int main() {
srand(32);
Node* sortedList = 0;
for (int i = 0; i < 10; i++){
SortedInsert(sortedList, rand() % 100);
}
Node* temp = sortedList;
for (int i=0; i < 9; i++){
assert(temp->data <= temp->next->data);
temp = temp->next;
}
delete sortedList;
}
Вы, вероятно, разыскиваете неинициализированный указатель. Пройдите через свой отладчик, чтобы найти источник ошибки. –