Home > front end >  Linked list: head always NULL and gives error (C)
Linked list: head always NULL and gives error (C)

Time:08-22

i'm trying to implement a linked list in my program but i don't understand why it does not work... everytime "search" is called it returns 0 (and the program stops) because it says *head is always NULL... where am i wrong? I checked multiple times and everything seems okay, i got the code from some guides online so i dont't really understand where i could have made error.. my guess is that the only problem is head not getting updated well (even if running with debugger it is modified sometimes correctly, but most of the times it stays NULL)

Here's the code

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


struct node{
    char* data;
    struct node* next;
};

int Search(struct node** head, char* word);

void Print(struct node* head);

struct node* NewNode(char* data);

void Insert(struct node** head, struct node* newnode);

void Insert_Word(char* data, struct node* head);

int temp;
char word[15];

int main() {

    static struct node* head = NULL;

    char tempword[15];
    int check;

    while(check != EOF){
        check = scanf("%s",tempword); //From stdin
        Insert_Word(tempword,head);
    }

    Print(head);
    
    temp = Search(&head, word);

    if (temp == 0) {
        printf("error\n");
        return 1;
    }

    else {
        ...
        other stuff
                ...
    }
}

void Insert_Word(char* data, struct node* head) {
    struct node* newNode = NewNode(data);
    Insert(&head,newNode);
}

int Search(struct node** head, char* word1) {
    struct node* temp1 = *head;

    while (temp1 != NULL) {
        if (strcmp(temp1->data,word1) == 0)
            return 1;
        temp1 = temp1->next;
    }
    return 0;
}


void Print(struct node* head){
    struct node* temp2 = head;
    while (temp2 != NULL) {
        printf("%s  ", temp2->data);
        temp2 = temp2->next;
    }
}


struct node* NewNode(char* data){
    struct node* newNode = (struct node*)malloc(sizeof(struct node*));
    newNode->data = malloc(strlen(data));
    newNode->data = strcpy(newNode->data, data);
    newNode->next = NULL;

    return newNode;
}

void Insert(struct node** head, struct node* newNode)
{
    struct node* temp3;
    if (*head == NULL || strcmp((*head)->data, newNode->data) >= 0) {
        newNode->next = *head;
        *head = newNode;
    }
    else {
        temp3 = *head;
        while (temp3->next != NULL && strcmp(temp3->next->data, newNode->data) <0 ) {
            temp3 = temp3->next;
        }
        newNode->next = temp3->next;
        temp3->next = newNode;
    }
}

Thanks in advance

CodePudding user response:

Now that you have the "head" node defined, I was able to test out your code. Testing it out, I saw that it was necessary to initialize the "head" node so that the program had a start to the list. Also, I tweaked the "while" loop to allow for the user to exit the "while" loop once all words had been entered and the linked list had been built. And, finally, the program prompts for a word to chase down the list. Following is a revised version of your code.

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

struct node
{
    char* data;
    struct node* next;
};

int Search(struct node** head, char* word);

void Print(struct node* head);

struct node* NewNode(char* data);

void Insert(struct node** head, struct node* newnode);

void Insert_Word(char* data, struct node* head);

int temp;
char word[15];

int main()
{

    static struct node* head = NULL;

    head = NewNode("-#-#");     /* Need a starting point for the node list */

    char tempword[15];

    printf("Enter words\n");

    while(1)                    /* Made the while loop more robust with an exit option*/
    {
        scanf("%s",tempword); //From stdin
        if (strcmp(tempword, "q") == 0)
        {
            break;
        }
        Insert_Word(tempword,head);
    }

    printf("Enter word to search for: ");   /* Then, ask the user to search for a word */

    scanf("%s", word);

    temp = Search(&head, word);

    if (temp == 0)
    {
        printf("Error - word was not found\n");
        return 1;
    }

    else
    {
        printf("Found\n");

    }
}

void Insert_Word(char* data, struct node* head)
{
    struct node* newNode = NewNode(data);
    Insert(&head,newNode);
}

int Search(struct node** head, char* word)
{
    struct node* temp = *head;

    while (temp != NULL)
    {
        if (strcmp(temp->data,word) == 0)
            return 1;
        temp = temp->next;
    }
    return 0;
}


void Print(struct node* head)
{
    struct node* temp = head;
    while (temp != NULL)
    {
        printf("%s  ", temp->data);
        temp = temp->next;
    }
}


struct node* NewNode(char* data)
{
    struct node* newNode = (struct node*)malloc(sizeof(struct node*));
    newNode->data = malloc(strlen(data));
    newNode->data = strcpy(newNode->data, data);
    newNode->next = NULL;

    return newNode;
}

void Insert(struct node** head, struct node* newNode)
{
    struct node* temp;
    if (*head == NULL || strcmp((*head)->data, newNode->data) >= 0)
    {
        newNode->next = *head;
        *head = newNode;
    }
    else
    {
        temp = *head;
        while (temp->next != NULL && strcmp(temp->next->data, newNode->data) <0 )
        {
            temp = temp->next;
        }
        newNode->next = temp->next;
        temp->next = newNode;
    }
}

Here was some output from a trial run.

@Una:~/C_Programs/Console/ListNull/bin/Release$ ./ListNull 
Enter words
turkey
algebra
fancy
horse
sunflower
autumn
q
Enter word to search for: sunflower
Found

Give that a try.

CodePudding user response:

seems that you have not initialized head to be anything in main - you have simply declared a struct node* head and sent it to search(), which in turn returns 0 because obviously, *head is null

  • Related