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

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
Username
  • 41
  • 1
  • 7
  • 2
    [Please see this discussion on why not to cast the return value of `malloc()` and family in `C`.](http://stackoverflow.com/q/605845/2173917). – Sourav Ghosh Dec 20 '15 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. – Iharob Al Asimi Dec 20 '15 at 14:15
  • `return newnode;` --> `return head;`, at callerside `head = add_palNode_from_keyboard(...` – BLUEPIXY Dec 20 '15 at 14:23

1 Answers1

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;
    }
    ...
dbush
  • 205,898
  • 23
  • 218
  • 273
  • 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 Dec 20 '15 at 14:31
  • @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;`. – Persixty Dec 20 '15 at 15:27