Mert Can Celik
Mert Can Celik

Reputation: 3

The reasons for segmentation fault while adding someone by sorting them into the linked list

List item

void MovieDatabase::  addActor(const string movieTitle, const string actorFirstName, const string actorLastName, const string actorRole ){
    bool found = false;
    Movie* temp;
    for(Movie* m= headMovie; m != NULL;  m = m-> next){
            if(m-> title == movieTitle){
                found = true;
                temp = m;
                break;
            }
    }

    if(found){
            if(headCast == NULL)
                    headCast =  new Cast(actorFirstName, actorLastName, actorRole, movieTitle);
            else{
                    Cast *c =  new Cast(actorFirstName, actorLastName, actorRole, movieTitle);
                    Cast *temp = headCast;
                    for(Cast* cur = headCast; cur -> next != NULL;  cur = cur -> next){
                             if(cur ->next -> lastName <  actorLastName){
                                    temp = temp -> next;
                            }
                            else{
                                    c -> next = cur -> next;
                                    temp  -> next= c;
                                    break;
                            }
                    }
            }
    }
    else
    cout << "The movie with a " << movieTitle << "  does not exist in the database,you can not add the actor. " << endl;
    size++;
}

Hi everyone i edited the code, now I'm not taking any fault. However, it does not show anything as an output. This means it does not add anyhing. In any case, I'm sending the code for displaying.

void MovieDatabase:: showActors(){
    for(Cast * cur= headCast;  cur != NULL; cur = cur -> next){
         cout  << cur  -> lastName << endl;
    }
}

Upvotes: 0

Views: 61

Answers (1)

JasonD
JasonD

Reputation: 16582

In this bit of code:

for(Cast* cur = headCast; cur != NULL;  cur = cur -> next){
    if(cur ->next -> lastName <  actorLastName){

You are checking only that cur != NULL, then dereferencing cur->next. cur->next could be NULL.

temp and cur both seem to be pointing at the same node. Perhaps you intended one to be ahead of the other, in order that you have a pointer to the previous node for inserting?

Here's an (untested) implementation:

    Cast *c =  new Cast(actorFirstName, actorLastName, actorRole, movieTitle);
    Cast *prev = NULL; // maintain a pointer to the previous node. No previous node initially.
    Cast *cur;
    for(cur = headCast; cur != NULL;  cur = cur -> next)
    {
        if(cur -> lastName > actorLastName) break; // stop if we find a place to insert.
        prev = cur;                                // update previous pointer before looping.
    }
    if(prev != NULL)
    {
        prev -> next = c; // if there is a previous node, use 'c' as its next node.
    } else {
        headCast = c;     // otherwise 'c' is the new head node.
    }
    c -> next = cur;      // the 'current' node always goes after 'c'.

Upvotes: 1

Related Questions