1

here's part of my code for the linked list:

struct node {
    float data;
    int key;
    struct node* next;
};

typedef struct{
    struct node *head;
    struct node *current;
    int length;
} linked_list;

linked_list *init_list(){
    linked_list *out = malloc(sizeof(linked_list));
    struct node *head = NULL;
    struct node *current = NULL;
    out->head = head;
    out->current = current;
    out->length = 0;
    return out;
}

void push_core(struct node *head, int key, float data){
    struct node *link = malloc(sizeof(struct node));
    link->data = data;
    link->key = key;
    link->next = head;
    // readjust to point at the new first node
    head = link;
    printf("%f; ", head->data);
}
void push(linked_list *list, int key, float data){
    push_core(list->head, key, data);
    list->length ++;
}

void print_list_core(struct node *head){
    struct node* ptr = head;
    printf("\n[");
    while(ptr != NULL){
        printf("(%d,%f)", ptr->key, ptr->data);
        ptr = ptr->next;
    }
}

void print_list(linked_list *list){
    print_list_core(list->head);
}

But in the main, after I initialized the linked list structure, I wasn't able to use push() to link new pointers, why is that?

linked_list *S = init_list();
for (int i = 0; i < n; i++)
{
    push(S,i,0);
    print_list(S);
    printf("%d;", S->length);
}

To clarify, the length of the list does update correctly. But when I try to print the list it doesn't work. Also, it's interesting that in another file when I initially just worked with the node struct and defined global variables for head and current, the code works fine. But when I try to wrap them up inside this linked_list struct, things aren't quite working as expected.

13
  • 1
    The C language passes function arguments by value. So updating head in push_core doesn't do anything to the head pointer in the list structure. Commented Feb 18, 2021 at 1:02
  • @user3386109 I thought I passed a pointer so it should write on that? So how should I edit the code to make it work? Should I change that to a double pointer? Thanks! Commented Feb 18, 2021 at 1:04
  • Can you debug your code? Commented Feb 18, 2021 at 1:06
  • If I was writing the code, I'd get rid of the push_core and print_list_core functions, and just implement push and print_list. Commented Feb 18, 2021 at 1:07
  • I don't mean to be rude, but there are quite a few issues that you should look into with the debugger Commented Feb 18, 2021 at 1:07

1 Answer 1

1

The problem occurred because you passed the pointer value of list->head to your push_code function as a parameter. This is a function call-by-value. So, when you change the head pointer inside the push_core function, it actually do not change the list->head pointer that you are expecting to. One quick fix would be returning the newly created link pointer from the push_core function and save it as list->head. The following code should fix your problem.

struct node * push_core(struct node *head, int key, float data){
    struct node *link = malloc(sizeof(struct node));
    link->data = data;
    link->key = key;
    link->next = head;
    
    return link;
}

void push(linked_list *list, int key, float data){
    list->head = push_core(list->head, key, data);
    list->length ++;
}
Sign up to request clarification or add additional context in comments.

Comments

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.