title: How to implement a linked list in C?
body: <p>I am creating a <a href="https://stackoverflow.com/questions/973654/single-linked-list">linked list</a> 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. My products struct will be nested inside this structure. And I should be passing the list to the function for adding and deleting. I find this concept confusing. </p>
  
  <p>I have implemented the initialize, add, and clean_up. However, I am not sure that I have done that correctly.</p>
  
  <p>When I add a product to the list I declare some memory using calloc. But I am thinking shouldn't I be declaring the memory for the product instead. I am really confused about this adding. </p>
  
  <p>Many thanks for any suggestions,</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  #define PRODUCT_NAME_LEN 128
  
  typedef struct product_data 
  {
      int product_code;
      char product_name[PRODUCT_NAME_LEN];
      int product_cost;
      struct product_data_t *next;
  }product_data_t;
  
  typedef struct list 
  {
      product_data_t *head;
      product_data_t *tail;
  }list_t;
  
  void add(list_t *list, int code, char name[], int cost); 
  void initialize(list_t *list);
  void clean_up(list_t *list);
  
  int main(void)
  {
      list_t *list = NULL;
  
      initialize(list);
      add(list, 10, "Dell Inspiron", 1500);
      clean_up(list);
  
      getchar();
  
      return 0;
  }
  
  void add(list_t *list, int code, char name[], int cost)
  {
      // Allocate memory for the new product
      list = calloc(1, sizeof(list_t));
      if(!list)
      {
          fprintf(stderr, "Cannot allocated memory");
          exit(1);
      }
  
      if(list)
      {
          // First item to add to the list
          list-&gt;head-&gt;product_code = code;
          list-&gt;head-&gt;product_cost = cost;
          strncpy(list-&gt;head-&gt;product_name, name, sizeof(list-&gt;head-&gt;product_name));
          // Terminate the string
          list-&gt;head-&gt;product_name[127] = '/0';
      } 
  }
  
  // Initialize linked list
  void initialize(list_t *list)
  {
      // Set list node to null
      list = NULL;
      list = NULL;
  }
  
  // Release all resources
  void clean_up(list_t *list)
  {    
      list_t *temp = NULL;
  
      while(list)
      {
          temp = list-&gt;head;
          list-&gt;head = list-&gt;head-&gt;next;
          free(temp);    
      }
      list = NULL;
      list = NULL;
      temp = NULL;
  }
  </code></pre>
  
  <p>============================== Edited ============================</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  #define PRODUCT_NAME_LEN 64
  
  // typedef struct product_data product_data_t;
  typedef struct product_data 
  {
      int product_code;
      char product_name[PRODUCT_NAME_LEN];
      int product_cost;
  }product_data_t;
  
  typedef struct list
  {
      struct list *head;
      struct list *tail;
      struct list *next;
      struct list *current_node;
      product_data_t *data;
  
  }list_t;
  
  void add(list_t *list, int code, char name[], int cost); 
  
  int main(void)
  {
      list_t *list = NULL;
      list = initialize(list);
      add(list, 1001, "Dell Inspiron 2.66", 1299);
  
      add(list, 1002, "Macbook Pro 2.66", 1499);
  
      clean_up(list);
  
      getchar();
  
      return 0;
  }
  
  void add(list_t *list, int code, char name[], int cost)
  {
      /* Allocate memory for the new product */
      product_data_t *product = (product_data_t*) calloc(1, sizeof(*product));
      if(!product)
      {
          fprintf(stderr, "Cannot allocate memory.");
          exit(1);
      }
  
      /* This is the first item in the list */
      product-&gt;product_code = code;
      product-&gt;product_cost = cost;
      strncpy(product-&gt;product_name, name, sizeof(product-&gt;product_name));
      product-&gt;product_name[PRODUCT_NAME_LEN - 1] = '\0';
  
      if(!list-&gt;head)
      {
          /* Assign the address of the product. */
          list = (list_t*) product;   
          /* Set the head and tail to this product */
          list-&gt;head = (list_t*) product;
          list-&gt;tail = (list_t*) product;
      }
      else
      {
          /* Append to the tail of the list. */
          list-&gt;tail-&gt;next = (list_t*) product;
          list-&gt;tail = (list_t*) product;
      }
  
      /* Assign the address of the product to the data on the list. */
      list-&gt;data = (list_t*) product;
  }
  </code></pre>