C Program to Insert Element in Linked List

Here we’ll see how to write C program to insert a new element in a singly linked list at four possible positions:

  • at the front of the list
  • at the end of the list
  • before a specified element
  • after a specified element

If the linked list does not exist while inserting the new element, in other words head points to NULL, then the linked list will be created in all above cases.
Note: All the programming examples below were tested on Linux (Fedora) environment and cc compiler was used. If you have any problem in trying these examples on other systems, let us know by commenting, we’ll try to solve.

Insert New Element at the Front of the Linked List

To insert an element at the front of the linked list, just after the head, you have to do the following.

  • Create the new element.
  • Point new element where head is currently pointing to.
  • Point head to the new element.

Here is the insert function.

Below is the full program including the above function.

Output of the above program:

Insert New Element at the End of the Linked List

To insert a new element at the end of the list, you have to do the following

  • Create the new element.
  • Point the new element where the last element used to point. In other words, point the new element to NULL because last element of a linked list always points to NULL.
  • Point the last element to the new element.

If the list is empty, i.e., no element is present in the list, then

  • Create the new element.
  • Point new element to NULL.
  • Point head to the new element.

Output of the above program:

Insert New Element after a Specified Node

In our example linked lists, all elements (nodes) have two parts, 1) one integer value, 2) pointer that points to the next element (node) or NULL in case is of the last element. In this sections we’ll see how to insert element after a specified element and the element will be specified using the value of the element (node). To do this you have to do the following

  • Find the element (node) in the list whose value is equal to the specified value. In our example the find function will return the pointer to the specified element. If such node is not found, then the insertion process should be aborted.
  • Create the new element.
  • Point the new element where the specified element used to point.
  • Point the specified element to the new element.

Output of the above program:

Insert New Element before a Specified Node

Inserting element before a specified element in a linked list is similar to the previous example. You have to do the following

  • Find the element (pointer to the element) in the list whose value is equal to the specified value.
  • Create the new node.
  • If the specified node is the first node of the list, i.e. head points to the specified node.
    • Point the new node where head used to point, in other words point the new node to the first element of the list.
    • Point head to the new element.
  • Otherwise,
    • Go to the previous element of the specified node.
    • Point the new node to the specified node.
    • Point the previous node to the new node.

Output of the above program:

Read also:

One thought on “C Program to Insert Element in Linked List”

Leave a Reply

Your email address will not be published. Required fields are marked *