How to implement a linked list in C?

后端 未结 14 865
予麋鹿
予麋鹿 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: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.

提交回复
热议问题