How to implement a linked list in C?

后端 未结 14 820
予麋鹿
予麋鹿 2020-12-06 02:49

I am creating a linked list as in the previous question I asked. I have found that the best way to develop the linked list is to have the head and tail in another structure.

相关标签:
14条回答
  • 2020-12-06 03:35

    You're calloc'ing space for your list_t struct, just pointers to list head and tail, which isn't what you want to do.

    When you add to a linked list, allocate space for an actual node in the list, which is your product_data_t struct.

    0 讨论(0)
  • 2020-12-06 03:36

    In C language, we need to define a Node to store an integer data and a pointer to the next value.

    struct Node{
        int data;
        struct Node *next;
    };
    

    To add a new node, we have a function add which has data as an int parameter. At first we create a new Node n. If the program does not create n then we print an error message and return with value -1. If we create the n then we set the data of n to have the data of the parameter and the next will contain the root as it has the top of the stack. After that, we set the root to reference the new node n.

    0 讨论(0)
提交回复
热议问题