#Whats wrong with following Linked List insertion? (I just put one insertion function call to test)

23 messages · Page 1 of 1 (latest)

pastel finch
#
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct Song {
    char name[25];
    int duration;
    struct Song* next_alpha;
} Song;
typedef Song* SongPtr;

typedef struct SongList {
    int size;
    SongPtr head_alpha;
} SongList;
typedef SongList* SongListPtr;

void insert(SongListPtr*, char[], int);

int main() {
    SongList songs;
    SongListPtr songsPtr = &songs;
    songsPtr->size = 0;
    songsPtr->head_alpha = NULL;
    insert(&songsPtr, "HelloWorld", 150);
    printf("End\n");
    return 0;
}

void insert(SongListPtr* songsPtr, char name[], int duration) {
    SongPtr newSongPtr = (SongPtr)malloc(sizeof(Song));
    if(newSongPtr != NULL) {
        strncpy(newSongPtr->name, name, 24);
        newSongPtr->duration = duration;
        (*songsPtr)->size += 1;

        SongPtr previousPtr = NULL;
        SongPtr currentPtr = (*songsPtr)->head_alpha;
        while(currentPtr->next_alpha != NULL  &&  strcmp(newSongPtr->name, currentPtr->next_alpha->name) > 0) {
            previousPtr = currentPtr;
            currentPtr = currentPtr->next_alpha;
        }

        if(previousPtr == NULL) {
            newSongPtr->next_alpha = (*songsPtr)->head_alpha;
            (*songsPtr)->head_alpha = newSongPtr;
        }
        else {
            previousPtr->next_alpha = newSongPtr;
            newSongPtr->next_alpha = currentPtr;
        } 
        printf("done\n");
        
    }
    else { 
        printf("Memory can not allocated.");
    }
}```
oblique anvilBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

plush wren
#

What's happening that shouldn't be?

hazy jackal
#

You don't check if currentPtr is NULL.
If the list is empty, so (*songsPtr)->head_alpha is NULL, then so is currentPtr.

#

;compile

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct Song {
    char name[25];
    int duration;
    struct Song* next_alpha;
} Song;
typedef Song* SongPtr;

typedef struct SongList {
    int size;
    SongPtr head_alpha;
} SongList;
typedef SongList* SongListPtr;

void insert(SongListPtr*, char[], int);

int main() {
    SongList songs;
    SongListPtr songsPtr = &songs;
    songsPtr->size = 0;
    songsPtr->head_alpha = NULL;
    insert(&songsPtr, "HelloWorld", 150);
    printf("End\n");
    return 0;
}

void insert(SongListPtr* songsPtr, char name[], int duration) {
    SongPtr newSongPtr = (SongPtr)malloc(sizeof(Song));
    if(newSongPtr != NULL) {
        strncpy(newSongPtr->name, name, 24);
        newSongPtr->duration = duration;
        (*songsPtr)->size += 1;

        SongPtr previousPtr = NULL;
        SongPtr currentPtr = (*songsPtr)->head_alpha;
        while(currentPtr->next_alpha != NULL  &&  strcmp(newSongPtr->name, currentPtr->next_alpha->name) > 0) {
            previousPtr = currentPtr;
            currentPtr = currentPtr->next_alpha;
        }

        if(previousPtr == NULL) {
            newSongPtr->next_alpha = (*songsPtr)->head_alpha;
            (*songsPtr)->head_alpha = newSongPtr;
        }
        else {
            previousPtr->next_alpha = newSongPtr;
            newSongPtr->next_alpha = currentPtr;
        } 
        printf("done\n");
        
    }
    else { 
        printf("Memory can not allocated.");
    }
}
glad ploverBOT
#
Compiler Output
Program terminated with signal: SIGSEGV
pastel finch
#

;compile

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct Song {
    char name[25];
    int duration;
    struct Song* next_alpha;
} Song;
typedef Song* SongPtr;

typedef struct SongList {
    int size;
    SongPtr head_alpha;
} SongList;
typedef SongList* SongListPtr;

void insert(SongListPtr*, char[], int);

int main() {
    SongList songs;
    SongListPtr songsPtr = &songs;
    songsPtr->size = 0;
    songsPtr->head_alpha = NULL;
    insert(&songsPtr, "HelloWorld", 150);
    printf("End\n");
    return 0;
}

void insert(SongListPtr* songsPtr, char name[], int duration) {
    SongPtr newSongPtr = (SongPtr)malloc(sizeof(Song));
    if(newSongPtr != NULL) {
        strncpy(newSongPtr->name, name, 24);
        newSongPtr->duration = duration;
        (*songsPtr)->size += 1;

        SongPtr previousPtr = NULL;
        SongPtr currentPtr = (*songsPtr)->head_alpha;
        if(currentPtr == NULL  ||  strcmp(newSongPtr->name, currentPtr->next_alpha->name) < 0) {
            newSongPtr->next_alpha = (*songsPtr)->head_alpha;
            (*songsPtr)->head_alpha = newSongPtr;
        }
        else {
            while(currentPtr->next_alpha != NULL  &&  strcmp(newSongPtr->name, currentPtr->next_alpha->name) > 0) {
                previousPtr = currentPtr;
                currentPtr = currentPtr->next_alpha;
            }

            if(previousPtr == NULL) {
                newSongPtr->next_alpha = (*songsPtr)->head_alpha;
                (*songsPtr)->head_alpha = newSongPtr;
            }
            else {
                previousPtr->next_alpha = newSongPtr;
                newSongPtr->next_alpha = currentPtr;
            } 
        }
        printf("done\n");
        
    }
    else { 
        printf("Memory can not allocated.");
    }
}```
glad ploverBOT
#
Program Output
done
End
pastel finch
#

uhmm

#

idk why but i dont get any output on my own

#

;compile

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct Song {
    char name[25];
    int duration;
    struct Song* next_alpha;
} Song;
typedef Song* SongPtr;

typedef struct SongList {
    int size;
    SongPtr head_alpha;
} SongList;
typedef SongList* SongListPtr;

void insert(SongListPtr*, char[], int);

int main() {
    SongList songs;
    SongListPtr songsPtr = &songs;
    songsPtr->size = 0;
    songsPtr->head_alpha = NULL;
    insert(&songsPtr, "HelloWorld", 150);
    insert(&songsPtr, "Olala", 200);
    printf("End\n");
    return 0;
}

void insert(SongListPtr* songsPtr, char name[], int duration) {
    SongPtr newSongPtr = (SongPtr)malloc(sizeof(Song));
    if(newSongPtr != NULL) {
        strncpy(newSongPtr->name, name, 24);
        newSongPtr->duration = duration;
        (*songsPtr)->size += 1;

        SongPtr previousPtr = NULL;
        SongPtr currentPtr = (*songsPtr)->head_alpha;
        if(currentPtr == NULL  ||  strcmp(newSongPtr->name, currentPtr->next_alpha->name) < 0) {
            newSongPtr->next_alpha = (*songsPtr)->head_alpha;
            (*songsPtr)->head_alpha = newSongPtr;
        }
        else {
            while(currentPtr->next_alpha != NULL  &&  strcmp(newSongPtr->name, currentPtr->next_alpha->name) > 0) {
                previousPtr = currentPtr;
                currentPtr = currentPtr->next_alpha;
            }

            if(previousPtr == NULL) {
                newSongPtr->next_alpha = (*songsPtr)->head_alpha;
                (*songsPtr)->head_alpha = newSongPtr;
            }
            else {
                previousPtr->next_alpha = newSongPtr;
                newSongPtr->next_alpha = currentPtr;
            } 
        }
        printf("done\n");
        
    }
    else { 
        printf("Memory can not allocated.");
    }
}```
glad ploverBOT
#
Compiler Output
Program terminated with signal: SIGSEGV
pastel finch
#

@hazy jackal it's fine to inserting the first node but i cant insert more

hazy jackal
#

You should really use a debugger. It would help instead of guessing where the crash happened.
You check if currentPtr is NULL, but then you use currentPtr->next_alpha without checking.

pastel finch
#

oh im done, i corrected my code rn

#

thanks

hazy jackal
#

np

#

you're over complicating this though.

oblique anvilBOT
#

@pastel finch Has your question been resolved? If so, type !solved :)

pastel finch
#

i noticed it..

#

!solved