I'm struggling with this problem: I want to insert a string into a sorted linked list of strings but for some reason it doesn't work. Here is the code:
void insert(node** head, const char* word){
node* newElem = malloc(sizeof(node));
newElem->data = malloc(sizeof (char)*(wordLenght+1));
strcpy(newElem->data, word);
if (*head == NULL || strcmp(newElem->data, (*head)->data) < 0){
newElem->next = *head;
*head = newElem;
return;
}
nodo* cursor = *head;
while (cursor->next != NULL && strcmp(newElem->data, cursor->data) < 0){
cursor = cursor->next;
}
newElem->next = cursor->next;
cursor->next = newElem;
}
I've tried with this set of strings
7DJL,-kiF, 8F4r, 7D7d, -D7w, -b7f
and it didn't work. The output should be:
-D7w, -b7f, -kiF, 7D7d, 7DJL, 8F4r
Thank you for your help!