How to create a library for associated list functions


I want to create static or dynamic library for linked list functions, but when I try to compile function files with gcc to get object file it gives me error ‘struct node’ declared inside parameter list int GetNth(struct node* head, int index) & dereferencing pointer to incomplete type ‘struct node’ if(i == index) return head->data;. Not sure what's going on, may be because I haven't declared the structure in the file

#include <stdlib.h>
int count(struct node* head, int n) //counts the occurrence of n in the list
  int c = 0;
  while(head != NULL){
    if(head->data == n) c+=1;
    head = head->next;
  return c;

But if I'll declare it inside this file, I think it'll violate 1 definition rule. What to do?
structure is declared in the main function as

struct node{
  int data;
  struct node *next;

Continuing from the comment, in your code, the compiler does not know what node is because you have not provided its declaration. You can provide a declaration including a typedef for convenience. The following code declares a typedef to struct _node {...} as node; and then declares two nodes (statically for simplicity reasons), iterates over the list, outputting the data values and then ends, e.g.

#include <stdio.h>

typedef struct _node {
    int data;
    struct _node *next;
} node;

int main (void) {

    node n1 = {.data = 1},
         n2 = {.data = 2, .next = NULL}; = &n2;

    /* iterate over list outputting data values */
    for (node *n = &n1; n != NULL; n = n->next)
        printf ("%d\n", n->data);

    return 0;

Example Use/Output

$ ./bin/llstatic

As mentioned, you can create a header file for your list and include your declaration of struct _node and the typedef there, or if you simply use node within this source-file, you can declare it here. Let me know if you have further questions.