2

I'm having problem with double linked lists: I can't get data from nodes throgh another node. This way: node->prev->prev. But node->prev is fine. Anyone knows why?

The code:

/*Add value - right side*/
void addListRight(doubleList *node, int value)
        {
        doubleList *newNode;
        newNode = createList();
        newNode->val = value;
        newNode->right = node->right;
        newNode->left = node;
        node->right->left = newNode; /*<-Error start here - 'segmentation error' or something like this*/
        node->right = newNode;
        }

Using Google, I found that some guys put () like this: (node->right)->right. I tryed it but the result is the same.

Using GCC/Ubuntu 10.10

*I would like to say the proprer word for '->' but I don't know it in English. My bad.. sorry! Same about the tags of this question!

Shuryon
  • 55
  • 1
  • 1
  • 4
  • 1
    If `node` is the beginning of the list, are you sure that there is something (other than the null pointer) in the field `right`? – Felix Dombek Jan 31 '11 at 02:18
  • Uhn.. I wasn't sure.. More precisely, I thought It was right :P But Thanks for your help! I'm a dunce that couldn't figure the answer between the lines of every right answer of you guys! The entire code was: http://www.shuryon.com.br/C-CPP/list.c The functional one is: http://www.shuryon.com.br/C-CPP/list-ok1.c Problem solved! – Shuryon Jan 31 '11 at 13:17

2 Answers2

2

You should check for NULL before using a pointer like that. There will be no prev at the head of the list.

Eric Giguere
  • 3,495
  • 15
  • 11
  • Yeah, I did (not int he code above). Here: www.shuryon.com.br/C-CPP/list.c Can u check? – Shuryon Jan 31 '11 at 03:07
  • @Shuryon: Your code at that URL does *not* check it. In `addNodeRight()`, you need `if (node->right) { node->right->left = newnode; }`. – caf Jan 31 '11 at 04:12
  • Thanks man! With this and one more if, I can see it in action! http://www.shuryon.com.br/C-CPP/list-ok1.c And here one version without this 'ifs': http://www.shuryon.com.br/C-CPP/list-ok2.c – Shuryon Jan 31 '11 at 13:06
1

It looks like you're getting a segmentation fault error. This means you are trying to access invalid memory. My guess is that you haven't allocated node->right or it's NULL. Make sure all your pointers are valid and have been allocated properly.

For reference, here is an example linked list implementation:

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

typedef struct doubleList doubleList;
struct doubleList
{
   int value;
   doubleList *left, *right;
};

doubleList *addListRight(doubleList *node, int value)
{
    doubleList *newNode;
    newNode = malloc(sizeof(doubleList));
    if(newNode == NULL)
    {
        return NULL;
    }
    newNode->value = value;
    newNode->left = NULL;
    newNode->right = NULL;
    if(node != NULL)
    {
        newNode->left = node;
        node->right = newNode;
    }
    return newNode;
}

int main(int argc, char **argv)
{
    doubleList *list = addListRight(NULL, 5);
    addListRight(list, 2);

    // Outputs: 5, 2
    printf("%d, %d", list->value, list->right->value);

    return 0;
}
  • I checked (not the way u did) Man o_O It's strange! I dunno what is, really! I will post the entire code here: www.shuryon.com.br/C-CPP/list.c – Shuryon Jan 31 '11 at 03:05