1

I have been working hard with speller but I am stuck when it comes to inserting the nodes in alphabetical order. I am pretty sure I am doing something wrong, but I am not sure exactly where the problem(s) lie. Maybe in the way I am handling the "head" of the linked list. If anyone could give me a hint, I would be very greatful.

Here's the relevant part of my code:

while(!feof(dic)

// define new node

    node* newnode = malloc(sizeof(node));

    // read word 
    fscanf(dic, "%s", newnode->word);

    // initialize next pointer to null

    newnode->next = NULL;

    // use hash function to know in which bucket it goes
    int index = hashf(newnode->word);

    // add in empty bucket


    if (tabla[index] == NULL)
    {
        tabla[index] = newnode;          
    }

    // add in first position

    else if (tabla[index] != NULL && strcmp(tabla[index]->word, newnode->word) > 0)
    {
        newnode->next = tabla[index];
        tabla[index] = newnode;
    }

    // add in middle or final position

    else
    {
        // crear variables para ubicar
        node* prev = tabla[index];
        node* cursor = tabla[index]->next;

        while(cursor->next != NULL)
        {
            // if cursor newnode is bigger than the cursor, move forward
            if (strcmp(newnode->word,cursor->word) > 0)      
            {
                prev = cursor;
                cursor = cursor->next;
            }

           // if newnode is smaller than cursor, then we should insert it before cursor
           else if (strcmp(newnode->word,cursor->word) < 0)
           {
                newnode->next = cursor;
                prev->next = newnode;

           }

           // insert in last position
           else if (cursor->next == NULL)
           {
                cursor->next = newnode;
           }
       }
    }
}        
2
  • What is the type of newnode->word?
    – kzidane
    Jan 4 '15 at 10:17
  • it is char[length+1]
    – Federico
    Jan 4 '15 at 17:22
2

Regardless of whether the code does what it should do or not, the problem is likely to be caused by the fact that you are trying to access cursor->next when cursor is actually a null-pointer.

This would be the situation in case tabla[index] was the only node in this linked-list and newnode->word does not equal tabla[index]->word which would mean that tabla[index]->next was NULL and the else branch would be executed.

You could use gdb and valgrind to get more information about the line where the segmentation fault happens. Watch the shorts on gdb and valgrind for more information!

2
  • thank you very much for looking into the code. Finally I had to work over it completely and the problem disappeared, but your comment made me train more in gdb!
    – Federico
    Jan 6 '15 at 15:33
  • @Federico you are welcome! You may up-vote the answer and accept it if it answers your question to mark it as solved! Thank you!
    – kzidane
    Jan 6 '15 at 18:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .