Linked List definition

List or linked list is a collection of nodes/recodes linked with each other using pointer. A node data structure has at least a member to hold the current element value and a pointer to point to the next node.

Linked List data-structure

typedef struct _slist_node
  int node_value;            /* value/data of this node */
  struct _slist_node * next; /* point to next node */

In this example nodes will be object of structure slist_node. Node_value member will be used to hold a user value. Member “next” will be used to point the next member in the list.

The first node of the linked list is called head and last node is the tail. When there is no element both head = tail = null. A new node object is created from heap using malloc/new and added to the list by setting proper value of the next/previous pointer. When we need to delete a particular node, we modify the next/previous pointer of the neighboring node and delete the node from heap using free/delete. This is a vital advantage list over array. We can add or remove any node at any location and memory is allocated or de-allocated at the run time.

Allocate New node

new_node() function allocates new node for the given input value. It also takes care memory allocation error.

slist_node * new_node(int value)
  slist_node * new_n = NULL;
  new_n = (slist_node *) malloc(sizeof(slist_node));
  if(new_n == NULL) {
    return NULL;
  memset(new_n, 0, sizeof(slist_node));
  new_n->next = NULL;
10    new_n->node_value = value;
11    return new_n;
12  }

Add at head

add_head() function allocates and adds a node before the existing head. New allocated node becomes the new head for the list.

slist_node * add_head(slist_node * head, int value)
  slist_node * new_n = NULL;
  new_n = new_node(value);
  if(new_n == NULL) {
    return NULL;
  new_n->next = head;
  return new_n;
10  }

Add at end/tail

add_tail() function allocates and adds a node after the tail. New allocated node becomes the new head for the list.

void add_tail(slist_node * head, int value)
  slist_node * new_n = NULL;
  slist_node * tmp_n;
  new_n = new_node(value);
  if(new_n == NULL) {
10      return NULL;
11    }
12    tmp_n = head;
13    while (tmp_n && tmp_n->next) {
14      tmp_n = tmp_n->next;
15    }
16    /* Reached the tail, now add this after tail */
17    tmp_n->next = new_n;
18    return;
19  }

Delete head

delete_head() function deletes head and returns the new head which is the node next to the head.

slist_node * delete_head(slist_node * head)
  slist_node *tmp_n;
  tmp_n = head;
  if (!head) {
    return NULL;
  tmp_n = head->next;
10    return tmp_n;
11  }

Delete tail

delete_tail() function deletes tail and makes the previous node as tail. If head is the only node in the list then list becomes empty.

void delete_tail(slist_node ** head)
  slist_node *tmp_n, *tmp_prev;
  /* Head is NULL or head pointer is NULL or empty */
  if (!head || !*head) {
  /* Head is the only one node */
  if(!*head->next) {
10     free(*head);
11     *head = NULL;
12    }
13    /* when list has more than one node */
14    tmp_n = *head;
15    while (tmp_n && tmp_n->next) {
16      tmp_prev = tmp_n; /* point to previous node */
17      tmp_n = tmp_n->next; /* point to next node */
18    }
19    tmp_prev->next = NULL; /* make it tail */
20    free(tmp_n); /* delete the original tail */
21    return;
22  }


  1. Best utilization of memory at run time.
  2. Addition and removal of any element is at any position is possible. Thus it eliminates constrain of the array where we cannot add or remove elements after the whole array has been allocated once.


  1. It uses some few bytes(pointer 4 byte for 32 bit) extra for each element.
  2. Finding any element is not straight forward as array. It needs iteration and more CPU cycle.

Types of Linked lists

There are four types of list used in common programming practices. They are -

  1. Single Linked List: Each node has only next pointer to point the next node in the list.
  2. Doubly Linked List: Each node has both next and previous pointer to point next and previous node in the list.
  3. Circular Linked List: Next pointer of tail node points to again head node.
  4. Doubly Circular Linked List: This list has both the properties of doubly linked list and circular linked list.
Single/Doubly Linked list can be used to construct some other useful data structures like queues, stack etc.

You have viewed 1 page out of 248. Your C learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

stack grows up or down, C and Data Structures, Data Structures using array, Linked List, Single Linked list, Doubly Linked list, Circular Linked list, Doubly Circular Linked list,

# C Programming Language (Prentice Hall Software)
# Let Us C Paperback - 2006 by Yashavant Kanetkar
# Understanding and Using C Pointers Core techniques for memory management
# Data Structures Using C and C++ Paperback - 1998
# Data Structures In C Paperback - August 11, 2008 by Noel Kalicharan