title: How can i modify my header file of linked list to work with many type of data?
body: <p>I made <code>a header file for linked list</code> like that. It works properly with any <code>data_t</code>, but whenever I use it, I always have to modify <code>struct data_t</code> in the header file.</p>
  <p>How can I declare <code>struct data_t</code> in the main file?</p>
  <p>Otherwhile, I have a linked list connecting to the other:</p>
  <pre><code>struct food_t{
          char fname[20];
          int time;
          food_t* f_next;
      };
  
  struct person_t{
          char pname[20];
          food_t* food;
          person_t* p_next;
      };
  </code></pre>
  <p>How can I use functions in the .h file with both 2 linked lists?</p>
  <p>Here is my header file:</p>
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;math.h&gt;
  #include&lt;stdlib.h&gt;
  #include&lt;conio.h&gt;
  #include&lt;string.h&gt;
  
  struct data_t{
      char name[20];
      char food[20];
  };
  
  struct node_t{
      data_t data;
      node_t *pNext;
  };
  
  struct list_t{
      node_t *pHead;
      node_t *pTail; 
  };
  
  //make new node with input data
  node_t *newNode(data_t data){
      node_t *ptr= (node_t *) malloc(sizeof(node_t));
      
      if(ptr==NULL){
          printf(&quot;\nCan not allocate memory!!!&quot;);
          return NULL;
      }
      else{
          ptr-&gt;data=data;
          ptr-&gt;pNext=NULL;
          return ptr;
      }
  }
  
  //initial list
  void initList(list_t&amp; ls){ 
      ls.pHead = ls.pTail = NULL;
  }
  
  //free list
  void lsFree(list_t &amp;ls){
      node_t *tmp;
      while((tmp=ls.pHead)!=NULL){        
          ls.pHead=ls.pHead-&gt;pNext;
          free(tmp);
      }
  }
  
  //check empty
  bool isEmpty(list_t ls){
      if(ls.pHead==NULL)
          return true;
      else return false;
  }
  
  //get size
  int getSize(list_t ls){
      if(isEmpty(ls)){
          return 0;
      }
      else{
          int size=0;
          node_t *ptr;
          ptr=ls.pHead;
          while(ptr!=NULL){
              size++;
              ptr=ptr-&gt;pNext;
          }
          return size;
      }
  }
  
  //Search node with input data
  node_t *search(list_t ls, data_t data);
  
  //get id of node. For root(head): root_id=0
  int get_node_index(list_t ls, node_t *node){
      if(isEmpty(ls)){
          printf(&quot;List is empty!!!\n&quot;);
          return -1;
      }
      else
      {
          int index=0;
          node_t *ptr=ls.pHead;
          for(ptr; ptr!=node; ptr=ptr-&gt;pNext){ 
              index++;
          }
          return index;
      }
  }
  
  //find pointer of node
  node_t *findNode(list_t ls, int id){
      node_t *ptr=ls.pHead;
      while(get_node_index(ls, ptr)!=id){
          ptr=ptr-&gt;pNext;
      }
      return ptr;
  }
  
  //add node to head
  void addHead(list_t&amp; ls, node_t *node){
      if(isEmpty(ls)){
          ls.pHead= ls.pTail= node;
      }
      else {
          node-&gt;pNext = ls.pHead; 
          ls.pHead= node;
      }
  }
  
  //del node from head
  void delHead(list_t &amp;ls){
      if(isEmpty(ls)){
          printf(&quot;List is empty!!!\n&quot;);
      }
      else
      {
          node_t *ptr;
          ptr=findNode(ls, 0); 
          ls.pHead= ls.pHead-&gt;pNext;
          free(ptr); 
      }
  }
  
  //add node to tail
  void addTail(list_t &amp;ls, node_t *node){
      if(isEmpty(ls)){
          ls.pHead=ls.pTail=node;
      }
      else{
          ls.pTail-&gt;pNext=node;
          ls.pTail=node;
      }
  }
  
  //del node from tail
  void delTail(list_t &amp;ls){
      if(isEmpty(ls)){
          printf(&quot;List is empty!!!&quot;);
      }
      else{
          int n= getSize(ls);
          node_t *ptr=findNode(ls, n-2);
          ls.pTail= ptr;
          free(ptr-&gt;pNext);
          ptr-&gt;pNext=NULL;
      }
  }
  
  //add node at id: new node will have this id
  void insert_at_id(list_t &amp;ls, int id, node_t *node){
      if(isEmpty(ls)){
          ls.pHead= ls.pTail= node;
      }
      else
      {
          node_t *ptr=findNode(ls, id-1);
          node-&gt;pNext= ptr-&gt;pNext;
          ptr-&gt;pNext=node;
      }   
  }
  
  //Del node at id 
  void del_at_id(list_t &amp;ls, int id){
      if(isEmpty(ls)){
          printf(&quot;List is empty!!!\n&quot;);
      }
      else if(id==0) delHead(ls);
      else
      {
          node_t *ptr=findNode(ls, id-1);
          node_t *tmp=ptr-&gt;pNext;
          ptr-&gt;pNext=tmp-&gt;pNext;
          if(tmp==ls.pTail) ptr=ls.pTail;
          free(tmp); 
      }
  }
  
  //Reverse list
  void reverse_list(list_t &amp;ls){
      int size=getSize(ls);
      node_t *tail= findNode(ls, size-1);
      node_t *head= findNode(ls, 0);
      for(int i=size-1; i&gt;=0; i--){
          if(i&gt;0){
              node_t *ptr=findNode(ls, i);
              node_t *ptr_before=findNode(ls, i-1);
              ptr-&gt;pNext=ptr_before;
          }
          else{
              node_t *ptr=findNode(ls, 0);
              ptr-&gt;pNext=NULL;
          }        
      }
      ls.pHead=tail;
      ls.pTail=head;
  }
  
  //Export to file
  void lsOut(list_t ls, FILE *fout){
      if(isEmpty(ls)){
          printf(&quot;List is emptry!!!\n&quot;);
      }
      else{
          node_t *ptr=ls.pHead;
          while(ptr!= NULL){
              fwrite(ptr, sizeof(data_t),1, fout);
              ptr=ptr-&gt;pNext;
          }
      }
  }
  
  
  //Search and move to head
  void move_to_head(list_t &amp;ls, node_t *ptr){
      node_t *tmp=newNode(ptr-&gt;data);
      addHead(ls, tmp);
      del_at_id(ls, get_node_index(ls, ptr));
  }
  
  //Swap data 2 nodes
  void swap(node_t *n1, node_t *n2){
      data_t tmp= n1-&gt;data;
      n1-&gt;data=n2-&gt;data;
      n2-&gt;data=tmp;
  }
  </code></pre>