econCodergirl
econCodergirl

Reputation: 313

Not able to add to the head of a Linked List

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

} 

Upvotes: 0

Views: 30

Answers (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

} 

Upvotes: 1

Related Questions