0

I've created a program that would insert a node into a linked list based on alphabetical order (A-Z). I'm able to do it, however, I'm not able to insert at the front of the linked list. We can assume that the list is never initially empty. Any help? Here is my code with the relevant structs. The problem is where // Inserting at the front of the list

typedef struct fileNode {
    char *tokenName; // hi 
    double token; // 0.5 
    struct fileNode *next; 
} fileNode; 

typedef struct fileStruct {
    char *name; // a.txt 
    int numberOfTokens; // 4 
    fileNode *tokenLL;
    struct fileStruct *next; 
} fileStruct;

void insertLL(struct fileNode *list, char *token){

    struct fileNode *ptr = list;
    struct fileNode *prev = NULL;

    struct fileNode *newNode = malloc(sizeof(struct fileNode));
    newNode->tokenName = token;
    newNode->token = 1; 
    bool inserted = false;
     
    while (ptr != NULL && inserted == false) {

        if (strcmp(ptr->tokenName, token) > 0){
            count++;

            if (prev == NULL){ // Inserting at the front of the list
                newNode->next = ptr;
                ptr = newNode;
                list = newNode;
                inserted = true;
                return;
            }

            else {
                prev->next = newNode; 
                newNode->next = ptr;
                inserted = true;
            }

        } // end of if

        prev = ptr; 
        ptr = ptr->next;

    } // end of while

    if (!inserted){
        prev->next = newNode;
    } // adding to the end of the list

} 
econCodergirl
  • 315
  • 1
  • 3
  • 8
  • Does this answer your question? [How do I modify a pointer that has been passed into a function in C?](https://stackoverflow.com/questions/766893/how-do-i-modify-a-pointer-that-has-been-passed-into-a-function-in-c) –  Nov 28 '20 at 20:22
  • Does this answer your question? [What is the reason for using a double pointer when adding a node in a linked list?](https://stackoverflow.com/questions/7271647/what-is-the-reason-for-using-a-double-pointer-when-adding-a-node-in-a-linked-lis) – n. m. could be an AI Nov 28 '20 at 20:26
  • Just return the address of the list in insertLL() and in other functions that can change the start of the list, line delete()... Use a typedef for the list. Is is easier to read and less error prone – arfneto Nov 28 '20 at 20:56

1 Answers1

1

To modify the direct object pointed by *list , you need to declare it as a **list in your function arguments.

if you declare it as a *list you're going to modify the object *list only inside the function. It's because when you call a function the arguments used inside the called function are a copy of the variables used to call the function in the calling function.

#include <stdio.h>

void add(int x,int y){
x=y;
}

void add_ptr(int *x,int y){
*x=y;
}

int main(int argc, char *argv[]) {
int x=1;
int y=2;
add(x,y);
printf("add\t x:%d\n",x);
add_ptr(&x,y);
printf("add_ptr\tx:%d\n",x);

return 0;
}

if you declare it as a **list you're going to modify the object pointed at the adress pointed by **list when you set: *(list)=Newnode; (the change is going to be permanent )

void insertLL(struct fileNode **list, char *token){

struct fileNode *ptr = *list;
struct fileNode *prev = NULL;

struct fileNode *newNode = malloc(sizeof(struct fileNode));
newNode->tokenName = token;
newNode->token = 1; 
bool inserted = false;
 
while (ptr != NULL && inserted == false) {

    if (strcmp(ptr->tokenName, token) > 0){
        count++;

        if (prev == NULL){ // Inserting at the front of the list
            newNode->next = *list; /*it's easier to understand with only *list*/
            *list = newNode;
            inserted = true;
            return;
        }

        else {
            prev->next = newNode; 
            newNode->next = ptr;
            inserted = true;
        }

    } // end of if

    prev = ptr; 
    ptr = ptr->next;

} // end of while

if (!inserted){
    prev->next = newNode;
} // adding to the end of the list

}