
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Explain insertion of elements in linked list using C language
Linked lists use dynamic memory allocation i.e. they grow and shrink accordingly. They are defined as a collection of nodes. Here, nodes have two parts, which are data and link. The representation of data, link and linked lists is given below −
Operations on linked lists
There are three types of operations on linked lists in C language, which are as follows −
- Insertion
- Deletion
- Traversing
Insertion
Consider an example, wherein we insert node 5 in between node 2 and node 3.
Now, insert node 5 at the beginning.
Insert node 5 at the end.
Insert node 5 at the end.
Note:
- We cannot insert node 5 before node 2 as the nodes are not named.
- We can insert node 5 before 2, if its position is given.
Program
Following is the C program for inserting an element in linked list −
#include <stdio.h> #include <stdlib.h> struct node{ int val; struct node *next; }; void print_list(struct node *head){ printf("H->"); while(head){ printf("%d->", head->val); head = head->next; } printf("……
"); } void insert_front(struct node **head, int value){ struct node * new_node = NULL; new_node = (struct node *)malloc(sizeof(struct node)); if (new_node == NULL){ printf(" Out of memory"); } new_node->val = value; new_node->next = *head; *head = new_node; } void insert_end(struct node **head, int value){ struct node * new_node = NULL; struct node * last = NULL; new_node = (struct node *)malloc(sizeof(struct node)); if (new_node == NULL){ printf(" Out of memory"); } new_node->val = value; new_node->next = NULL; if( *head == NULL){ *head = new_node; return; } last = *head; while(last->next) last = last->next; last->next = new_node; } void insert_after(struct node *head, int value, int after){ struct node * new_node = NULL; struct node *tmp = head; while(tmp) { if(tmp->val == after) { /*found the node*/ new_node = (struct node *)malloc(sizeof(struct node)); if (new_node == NULL) { printf("Out of memory"); } new_node->val = value; new_node->next = tmp->next; tmp->next = new_node; return; } tmp = tmp->next; } } void insert_before(struct node **head, int value, int before){ struct node * new_node = NULL; struct node * tmp = *head; new_node = (struct node *)malloc(sizeof(struct node)); if (new_node == NULL){ printf("Out of memory"); return; } new_node->val = value; if((*head)->val == before){ new_node->next = *head; *head = new_node; return; } while(tmp && tmp->next) { if(tmp->next->val == before) { new_node->next = tmp->next; tmp->next = new_node; return; } tmp = tmp->next; } /*Before node not found*/ free(new_node); } void main(){ int count = 0, i, val, after, before; struct node * head = NULL; printf("Enter no: of elements: "); scanf("%d", &count); for (i = 0; i < count; i++){ printf("Enter %dth element: ", i); scanf("%d", &val); insert_front(&head, val); } printf("starting list: "); print_list(head); printf("enter front element: "); scanf("%d", &val); insert_front(&head, val); printf("items after insertion: "); print_list(head); printf("enter last element: "); scanf("%d", &val); insert_end(&head, val); printf("items after insertion: "); print_list(head); printf("Enter an ele to insert in the list: "); scanf("%d", &val); printf("Insert after: "); scanf("%d", &after); insert_after(head, val, after); printf("List after insertion: "); print_list(head); printf("Enter an ele to insert in the list: "); scanf("%d", &val); printf("Insert before: "); scanf("%d", &before); insert_before(&head, val, before); printf("List after insertion: "); print_list(head); }
Output
When the above program is executed, it produces the following result −
Enter no: of elements: 4 Enter 0th element: 1 Enter 1th element: 2 Enter 2th element: 3 Enter 3th element: 4 starting list: H->4->3->2->1->...... enter front element: 5 items after insertion: H->5->4->3->2->1->...... enter last element: 0 items after insertion: H->5->4->3->2->1->0->...... Enter an ele to insert in the list: 6 Insert after: 0 List after insertion: H->5->4->3->2->1->0->6->...... Enter an ele to insert in the list: 7 Insert before: 5 List after insertion: H->7->5->4->3->2->1->0->6->......
- Related Articles
- Explain queue by using linked list in C language
- Explain the stack by using linked list in C language
- Explain the concept of Linked list in C language
- Explain the insertion sort by using C language.
- Recursive insertion and traversal linked list in C++
- Deletion of head and tail element logic in a linked list using C language.
- Print nodes of linked list at given indexes in C language
- Print reverse of a Linked List without actually reversing in C language
- Print the alternate nodes of linked list (Iterative Method) in C language
- Add elements to a linked list using Javascript
- Remove elements from a linked list using Javascript
- C++ Pairwise Swap Elements of a Given Linked List
- Insertion Sort List in C++
- Insertion Sort List C++
- Sum of smaller elements of nodes in a linked list in C++

Advertisements