#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.");
}
}```
#Whats wrong with following Linked List insertion? (I just put one insertion function call to test)
23 messages · Page 1 of 1 (latest)
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.
What's happening that shouldn't be?
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.");
}
}
Compiler Output
Program terminated with signal: SIGSEGV
vexeduxr | 173ms | c | x86-64 gcc 14.1 | godbolt.org
oh, right.. thx
;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.");
}
}```
Program Output
done
End
hwlios | 66ms | c | x86-64 gcc 14.1 | godbolt.org
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.");
}
}```
Compiler Output
Program terminated with signal: SIGSEGV
hwlios | 125ms | c | x86-64 gcc 14.1 | godbolt.org
@hazy jackal it's fine to inserting the first node but i cant insert more
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 Has your question been resolved? If so, type !solved :)
oh i just used totally different code, i used a different algorithm to implement it
i noticed it..
!solved