clinked-listnodestree-nodes

Linked List in C


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!


Solution

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