This is a class assignment to do ordered insertion into a sorted linked list. I've avoided accessing null pointers, and I've added some lines of debugging output to narrow the seg fault down to the conditional statement of the while loop itself.
I can add a node to the empty list and add nodes with smaller keys to the head of the list, but I run into a seg fault when adding a key to the end of a non-empty list. As displayed by the debug output, the problematic while loop cycles successfully until it sees cursor->next == NULL. That's when it throws the seg fault. I'm not trying to access the empty pointer as far as I can tell. I'm really pretty stumped and would appreciate any help. Thanks!
I've denoted the troublesome line in the code below.
// Headers
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
struct Node {
int id;
string initials;
double score;
Node* next;
};
const int ESC = -9999;
const int ID_LIMIT = 1000000000;
int main(){
// initialize variables
int temp_id = 0; // temporary input
Node* Start=NULL; // first node in linked list
// get input until escape value is entered or input stream fails
while ( temp_id != ESC && !cin.fail() ){
cout << "\nPlease enter the student id, or "
<< ESC << " to finish inputing: ";
cin >> temp_id;
if ( temp_id >=0 && temp_id < ID_LIMIT ) {
// if valid input, continue input & create node
string temp_inits;
double temp_score;
cout << "Please enter the student initials: " ;
cin >> temp_inits;
cout << "Please enter the student's test score: " ;
cin >> temp_score;
// create new node with input values
Node* temp = new Node;
temp->id = temp_id;
temp->initials = temp_inits;
temp->score = temp_score;
temp->next = NULL;
// TASK 4: SORTED INPUT
if ( Start == NULL ) { // if first node to be input
Start = temp; // point the first pointer to it
}else{ // otherwise
if( temp->id < Start->id ){ // if new node should go before Start,
temp->next = Start; // move start after temp
Start = temp; // and reassign Start
}else{
Node* cursor = Start; // parse list for proper place or end
cout << "\nDEBUG: while ( cursor-id:" << cursor->id ;
cout << " < temp-id:" << temp->id;
cout << " && cursor-next:" << cursor->next << " != NULL )";
// I THINK THE NEXT LINE IS THE CULPRIT
while ( cursor->id < temp->id && cursor->next != NULL ) {
cout << "\nDEBUG: { old cursor:" << cursor->id ;
cursor = cursor->next;
cout << ", new cursor:" << cursor->id << " }";
cout << "\nDEBUG: while ( cursor-id:" << cursor->id ;
cout << " < temp-id:" << temp->id;
cout << " && cursor-next:" << cursor->next << " != NULL )";
}
cout << "\nDEBUG: insert temp-id:" << temp->id
<< " after cursor-id:" << cursor->id ;
temp->next = cursor->next;
cursor->next = temp; // inject new node into list.
cursor = temp->next;
cout << " before id " << cursor->id;
}
}
cout << "Node with id=" << temp->id << ", initials=" << temp->initials
<< ", and test score=" << temp->score << " added to the list.\n";
}else{ // if invalid input & not escape value, print error
if ( temp_id != ESC ) cout << "!!! Invalid input !!!\n";
}
}
return 0;
}
Output:
Please enter the student id, or -9999 to finish inputing: 654
Please enter the student initials: abc
Please enter the student's test score: 99.9
Node with id=654, initials=abc, and test score=99.9 added to the list.
Please enter the student id, or -9999 to finish inputing: 312
Please enter the student initials: qwe
Please enter the student's test score: 54.8
Node with id=312, initials=qwe, and test score=54.8 added to the list.
Please enter the student id, or -9999 to finish inputing: 987
Please enter the student initials: rty
Please enter the student's test score: 87.5
DEBUG: while ( cursor-id:312 < temp-id:987 && cursor-next:0x1c26040 != NULL )
DEBUG: { old cursor:312, new cursor:654 }
DEBUG: while ( cursor-id:654 < temp-id:987 && cursor-next:0 != NULL )
Segmentation fault
I have also tried the loop with ( ... cursor->next != 0 ) and ( ... cursor->next )