0

I'm having some problems with adding to a dynamic linked list. Basically it seems like my first node is overwritten. Here is the code:

struct palNode {
    int number;
    int pointer_index;
    char* solving_array;
    int solved;
    struct palNode* next;
}

And here is my add method:

struct palNode* add_palNode_from_keyboard(struct palNode* head, int num, int pos){
    struct palNode* newnode = (struct palNode*) malloc(1 * sizeof(struct palNode));

    struct palNode* current_node = head;

    if (current_node == NULL)
    {
        head = newnode;
    }
    else 
    {
        while ((*current_node).next != NULL)
        {
            current_node = (*current_node).next;
        }
        (*current_node).next = newnode;
    }

    (*newnode).number = num;
    (*newnode).pointer_index = pos;

    (*newnode).next = NULL;

    printf("Operation completed\n");

    return newnode;
}

And here are my questions: What am I doing wrong? Is there a more correct way of doing it? I have seen other similar questions but I still dont get them

3
  • 2
    Please see this discussion on why not to cast the return value of malloc() and family in C.. Commented Dec 20, 2015 at 14:13
  • Also, 1 * sizeof(struct palNode) is really unecessary. Note that you can also write sin(x)^2 + cos(x)^2 or exp(0) but just because you can it doesn't mean you have to. And (*newnode).number you don't need to dereference newnode, just newnode->number would be ok. Commented Dec 20, 2015 at 14:15
  • return newnode; --> return head;, at callerside head = add_palNode_from_keyboard(... Commented Dec 20, 2015 at 14:23

1 Answer 1

1

If the list is initially empty, you set head to the new node, however the pointer head is passed by value so changes to it don't appear in the calling function.

You need to pass in address of the head pointer and modify that so that changes appear outside the function:

struct palNode* add_palNode_from_keyboard(struct palNode** head, int num, int pos){
    // no cast needed here, and no need to multiply by 1
    struct palNode* newnode = malloc(sizeof(struct palNode));

    struct palNode* current_node = *head;

    if (current_node == NULL)
    {
        *head = newnode;
    }
    ...
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the tips on malloc. The thing is that I can't modify the function: struct palNode* add_palNode_from_keyboard(struct palNode** head, int num, int pos) Also when im returning it's like there is no connection when i add more than one node /: Any ideas ? E.g. It return the first node but nothing after.
@Username You may need to provide a little more code. You're returning the new node. That is (of course) the last node. It's not obvious how you're retaining the head-node in the calling function. If you can't change the function signature as recommended you need some nasty code like newnode=add_palNode_from_keyboard(head,num,pos);if(head==NULL)head=newnode;.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.