Home Download Change log

Introduction

C does not have functions for dynamic lists. This small dynamic list library solve this problem. It is written in standard C (I hope ANSI C) and simple, but also flexible and powerful. If you are looking for dynamic list functions, feel free to use the library.

Features

Examples

/* list node */
typedef struct DataNode
{
  LIST_NODE_HEADER(struct DataNode);

  char s[100];
} DataNode;

/* list */
typedef struct
{
  LIST_HEADER(DataNode);
} DataList;

void freeDataNode(DataNode *dataNode, void *userData)
{
  /* code to free the data of a single node */
}

int compareDataNodes(DataNode *dataNode1, DataNode *dataNode2, void *dummy)
{
  return strcmp(dataNode1->s,dataNode2->s);
}

int main(int argc, char *argv[])
 {
  DataList DataList;
  DataNode *dataNode;

  /* create a list, traverse it, delete list */
  List_init(&dataList);

  dataNode = LIST_NEW_NODE(DataNode);
  strcpy(dataNode->s,"Hello");
  List_append(&dataList,dataNode);

  dataNode = LIST_NEW_NODE(DataNode);
  strcpy(dataNode->s," ");
  List_append(&dataList,dataNode);

  dataNode = LIST_NEW_NODE(DataNode);
  strcpy(dataNode->s,"World");
  List_append(&dataList,dataNode);

  dataNode = LIST_NEW_NODE(DataNode);
  strcpy(dataNode->s,"\n");
  List_append(&dataList,dataNode);

  dataNode = dataList.head;
  while (dataNode != NULL)
  {
    printf(dataNode->s);
    dataNode = dataNode->next;
  }

  List_done(&dataList,(ListNodeFreeFunction)freeDataNode,NULL);

  /* create a list, print it, sort it, print it, delete list */
  List_init(&dataList);

  dataNode = LIST_NEW_NODE(DataNode);
  strcpy(dataNode->s,"b");
  List_append(&dataList,dataNode);
  dataNode = LIST_NEW_NODE(DataNode);
  strcpy(dataNode->s,"a");
  List_append(&dataList,dataNode);
  dataNode = LIST_NEW_NODE(DataNode);
  strcpy(dataNode->s,"y");
  List_append(&dataList,dataNode);
  dataNode = LIST_NEW_NODE(DataNode);
  strcpy(dataNode->s,"x");
  List_append(&dataList,dataNode);
  dataNode = LIST_NEW_NODE(DataNode);
  strcpy(dataNode->s,"z");
  List_append(&dataList,dataNode);

  printf("not sorted:\n");
  dataNode = dataList.head;
  while (dataNode != NULL)
  {
    printf("%p: %s\n",dataNode,dataNode->s);
    dataNode = dataNode->next;
  }

  List_sort(&dataList,
            (ListNodeCompareFunction)compareDataNodes,
            NULL
           );

  printf("sorted:\n");
  dataNode = dataList.head;
  while (dataNode != NULL)
  {
    printf("%p: %s\n",dataNode,dataNode->s);
    dataNode = dataNode->next;
  }

  List_done(&dataList,(ListNodeFreeFunction)freeDataNode,NULL);

  return(0);
 }

Compile

Sorry, there is no makefile, but compilation is simple:
    
gcc -I. -c common/lists.c -D_GNU_SOURCE
gcc -I. -c common/global.c -D_GNU_SOURCE
gcc -I. -c errors.c -D_GNU_SOURCE
gcc -I. -c lists_demo.c -D_GNU_SOURCE
gcc -o lists_demo lists_demo.o lists.o global.o -lpthread

License

List library is currently under GPL version 2.

Download

lists-0.08.tar.bz2

ChangeLog

2023-06-18 0.08
  * new include folder structure

2016-11-18 0.07
  * implemented List_findAndRemove()

2016-01-03 0.06
  * implemented LIST_FIND
  * renamed ListFunctionCopyNode -> ListFunctionDuplicateNode

2012-07-19 0.05
  * fixed possible race-condition in debug code
  
2012-07-07 0.04
  * added debug code: track allocated list nodes
  * clean-up List_getFirst()/List_getLast()

2010-12-17 0.03
  * fixed sorting (prev-pointers)

2009-02-13 0.02
  * clean-up

2008-12-13 0.01
  * initial public release
      

    
Back to top