Home > OS >  Wrong value when inserting into a linked list in c
Wrong value when inserting into a linked list in c

Time:11-11

I am trying to insert words into a hash table and it looks like it works but when I try to print the word inside the node (just to check if its still correct) I get a bogus value. When my code prompts for the word I said 'Hey' and when it prompts for place I said '5'. The string that is printed out(which is supposed to be the word inside the node) is HH9[]A\A]A^A_f. What is happening to the word inside the node and am I inserting the node correctly?

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

typedef struct node
{
    char word[20];
    struct node *next;
}
node;

int main (void)
{
    node* table[10];
 
    char wrd[10];
    
    printf("Word to insert: ");
    fgets(wrd, 10, stdin);
    
    int place; 
    printf("Place to insert word: ");
    scanf("%d", &place);
    
    
    node *n = malloc(sizeof(node));
        
    if(n == NULL)
    {
        return 1;
    }
        
    strcpy(n->word, wrd);
        
    if(table[place] == NULL)
    {
        n = table[place];
        n->next = NULL;
    }
    else
    {
        n->next = table[place];
        
        n = table[place];
    }
    
    printf("Word inside node: %s \n" , n->word);
}


EDIT

I changed the code and tried to implement it on a larger scale but my while loop gives me a segfault. This is the function I put it in:

    FILE* dct = fopen ("/dictionaries/large", "r");
    char *wrd = NULL;
    
    while(fscanf(dct, "%s", wrd) != EOF)
    {

        int place = hash(wrd);
        
        node *n = malloc(sizeof(node));
        node *anchor = NULL;
        node *end = NULL;
        
        if(n == NULL)
        {
            return 1;
        }
        
        strcpy(n->word, wrd);

        n->next = NULL;

        if (!end)    //Initial state
            anchor = end = n;
        else        //Every following node.
            end = end->next = n;
        
        strcpy(n->word, wrd);
        
        n->next = table[place];

        table[place] = n;
        
        counter  ;
    }
    
    return false;

It has to read from the dictionary file and load the word into memory(or a hash table).

CodePudding user response:

A linked list is a linked list because it does not have a fixed size. The table array is therefor superfluous. What you need for your linked list to work is to remember the anchor and nothing more.

A small example:

Node *anchor = NULL;
Node *end = NULL;
Node *node = malloc(sizeof(Node));

node->next = NULL;

if (!end)    //Initial state
    anchor = end = node;
else        //Every following node.
    end = end->next = node;

At this point, you can still access the node you've just filled. Don't forget to iterate over your list later and free those allocations though.

CodePudding user response:

This code doesn't make any sense:

    if(table[place] == NULL)
    {
        n = table[place]; // since we know table[place] is null, that sets n to null!
        n->next = NULL;   // We just set n to NULL, we can't access n->next!
    }
    else
    {
        n->next = table[place]; // This sets n to a garbage value since table[place] was never assigned a value
   
        n = table[place]; // This leaks the value we malloc'ed. We were supposed to link it to the list!
    }
  • Related