Linked List Improvements in C I'm trying to write a concise linked list implementation in C and so far it looks pretty good. I'd like some pointers (pun) on how I can improve what I've got in terms of naming, consistency, efficiency, and anything else that's relevant.

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

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

int length(node_t *head){
    int length = 0;
    for(node_t *cur = head; cur; cur = cur-&gt;next)
        length++;
    return length;
}

void printList(node_t *head){
    for(node_t *cur = head; cur; cur = cur-&gt;next)
        printf("%d\n", cur-&gt;data);
}

void deleteList(node_t **head){
    node_t *current = *head, *next;
    while (current != NULL) {
        next = current-&gt;next;
        free(current);
        current = next;
    }
   *head = NULL;
}

void deleteNode(node_t *node, node_t *head){
    node_t *cur = head;
    while (cur-&gt;next != node)
        cur = cur-&gt;next;
    cur-&gt;next = node-&gt;next;
    free(node); node=NULL;
}

node_t *getNode(int key, node_t *head){
    node_t *node = head;
    while(node){
        if (node-&gt;data == key){ return node; }
        else { node = node-&gt;next; }
    }
    return NULL;
}

node_t *insert(int key, node_t *head){
    node_t *cur = head;
    while(cur-&gt;next)
        cur = cur-&gt;next;
    node_t *new = malloc(sizeof(node_t));
    if (new == NULL){ exit(-1); }
    new-&gt;data = key;
    new-&gt;next = NULL;
    cur-&gt;next = new;
    return new;
}

node_t *createList(int key) {
    node_t *head = malloc(sizeof(node_t));
    if (head == NULL){ exit(-1); }
    head-&gt;data = key;
    head-&gt;next = NULL;
    return head;
}

int main(){
    node_t *head = createList(1);
    insert(2, head);
    node_t *nodeA = insert(3, head);
    insert(4, head);
    printf("1) Printing list:\n");
    printList(head);
    node_t *nodeB = getNode(4, head);
    deleteNode(nodeA, head);
    deleteNode(nodeB, head);
    printf("2) Printing list:\n");
    printList(head);
    deleteList(&amp;head);
    printf("3) Printing list:\n");
    printList(head);
    return 0;
}
Bug: can't delete head node

Your delete_node() function doesn't work if the node you want to delete is the head of the list.  In fact, the function signature can't work:


void deleteNode(node_t *node, node_t *head);



Since the head pointer can change if you delete it, your signature has to be one of these two instead;

void deleteNode(node_t *node, node_t **head);
node_t *deleteNode(node_t *node, node_t *head);


Insert should be renamed

Since your "insert" function adds to the end of the list, I think it should be called append() instead.