Creating an insert at front function for a circular doubly linked list I have only been working with C for a couple of months and I think I have a pretty good function to  insert a node in the front of the circular doubly linked list.  I have tested in adding several nodes to it, but would like some feedback.

struct used in functions:

typedef struct TCB_t
{
    struct TCB_t * next;
    struct TCB_t * previous;
    ucontext_t context;
    int val;
}TCB_t;


Here is the function that calls the add_to_list() function:

void start_thread(void (*function)(void), int *arg)
{
    printf("In main: creating thread\n");
    struct stack * stackP = (struct stack*)malloc(8192);
    tcb = (struct TCB_t *)malloc(sizeof(struct TCB_t));
    init_TCB (tcb, function, stackP,  8192);
    tcb-&gt;val = iter;
    iter++;
    add_to_list( &amp;ptr, &amp;tcb);
}


This is the function in which I am not sure if I am adding the nodes correctly:

void add_to_list( struct TCB_t **ptrBlock, struct TCB_t **addQ)
{
    (*addQ)-&gt;val = iter;
    iter++;

    if(!*ptrBlock)
    {
        *ptrBlock = *addQ;
        (*ptrBlock)-&gt;next= *ptrBlock;
        (*ptrBlock)-&gt;previous = *ptrBlock;
    }
    else
    {
        (*ptrBlock)-&gt;previous-&gt;next = *addQ;
        (*addQ)-&gt;previous = (*ptrBlock)-&gt;previous;
        (*addQ)-&gt;next = *ptrBlock;
        (*ptrBlock)-&gt;previous = *addQ;
    }
}
Here are some things that will help you improve your code.

Provide complete code to reviewers

This is not so much a change to the code as a change in how you present it to other people.  Without the full context of the code and an example of how to use it, it takes more effort for other people to understand your code.  This affects not only code reviews, but also maintenance of the code in the future, by you or by others.  One good way to address that is by the use of comments.  Another good technique is to include test code showing how your code is intended to be used.

Consider how your code is used

When somebody uses this code, they will probably want to insert a value into a data structure, and have the data structure handle all of the messy details, such as memory management. However, this code only seems to store successive integers (from a global variable) and requires the caller to allocate memory for the entire structure.  So instead of an interface like this:

void add_to_list( struct TCB_t **ptrBlock, struct TCB_t **addQ)


a better interface would probably be this:

void add_to_list(struct TCB_t *node, int value)


This interface would allocate space for an additional TCB_t, add the passed value to it and link it appropriately to the existing node.

Provide convenience functions for testing

In order to test your function, it would be useful to be able to do things like print out the contents of the list.  One way to do that is to provide a convenience functions such as this one:

void print_list(const struct TCB_t *n)
{
    if (n == NULL) {
        printf("empty list\n");
        return;
    }
    printf("%d", n-&gt;val);
    for (struct TCB_t *p=n-&gt;next; p!=n; p = p-&gt;next) 
        printf(", %d", p-&gt;val);
    printf("\n");
}


Consider providing a whole interface

Instead of providing just one function, it is often more useful to provide a couple of related functions to deal with a particular structure.  For example, in the case of a circular doubly-linked list, node insertion, node deletion, printing, initialization and destruction are all reasonable things to expect from an interface.  Providing only one fails to provide a sufficient spectrum that makes the data structure actually usable.