Linked List Programs in C

C Programming Examples - Linked List

A linked list is a collection of objects called nodes that are stored in memory at random locations. Each node has two fields, one for storing data and the other for storing the address of the next node in memory. The head node contains the address of the first node and the last node of the linked list contains a pointer to the null. Linked list operations such as insert, delete, traverse, search, and sort allow to perform a wide range of operations on linked lists.

Types of Linked List:

There are three types of Linked List in C:

  • Singly Linked List: Singly linked list is the most common linked list type. Each node stores data as well as a pointer to the next node.
  • Doubly Linked List: A doubly linked list is created by adding pointers to the previous and next nodes.
  • Circular Linked List: A circular linked list is a variation of a linked list in which the last element is linked to the first element.

Linked List Applications:

  • Stacks and queues implementation
  • Dynamic memory allocation
  • Graphs implementation
  • Representing sparse matrices
  • Precision arithmetic

The following section contains various C programs about linked lists, linked list operations, singly-linked lists, doubly linked lists, and data structures using linked lists. It also includes different programs on a linked list using recursion. Each sample program on the linked list includes a program description, C code, and program output. All examples have been compiled and tested on Windows and Linux systems.

advertisement
advertisement

Here is the listing of C programming examples on Linked List:

  1. C Programs on Linked List
  2. C Programs on Linked List Operations
  3. C Programs on Singly Linked List
  4. C Programs on Doubly Linked List
  5. C Programs on Data Structures using Linked List
  6. C Programs on Interchange and Modify Operations
  7. C Programs on Linked List using Recursion

1. C Programs on Linked List

Program Description
Create and Display Linked List in C C Program to Create a Linked List and Display it
Search an Element in Linked List in C C Program to Search an Element in a Linked List
Search an Element in Linked List using Recursion in C C Program to Search an Element in a Linked List using Recursion
Search an Element in Linked List without Recursion in C C Program to Search an Element in Linked List without Recursion
Remove Duplicates from Linked List in C C Program to Remove Duplicates from a Linked List
Intersection & Union of 2 Linked Lists in C C Program to Find Intersection and Union of Two Linked Lists
Adjacency List in C C Program to Implement Adjacency List
Skip List in C C Program to Implement Skip List
Vlist in C C Program to Implement Vlist
Xor Linked List in C C Program to Implement Xor Linked List

2. C Programs on Linked List Operations

Program Description
Reverse a Linked List in C C Program to Reverse a Linked List
Reverse First N Elements of a Linked List in C C Program to Reverse First N Elements of a Linked List
Common Element in Two Linked Lists in C C Program to Find the First Common Element in Two Linked Lists
Count Occurrences of Elements in a Linked List in C C Program to Count the Occurrences of Elements in a Linked List
Check if Two Lists are Equal in C C Program to Check if Two Lists are Equal
Add Positioned Elements of 2 Linked Lists in C C Program to Add Corresponding Positioned Elements of Two Linked Lists
Detect Cycle in Linked List in C C Program to Detect Cycle in a Linked List

3. C Programs on Singly Linked List

Program Description
Singly Linked List Operations in C C Program to Perform Singly Linked List Operations
Circular Single Linked List in C C Program to Demonstrate Circular Single Linked List
Check if Singly Linked List is Palindrome in C C Program to Check if Singly Linked List is Palindrome
Singly Linked List to Circular List in C C Program to Convert Singly Linked List to Circular List
Singly Linked List using Dynamic Memory Allocation in C C Program to Implement Singly Linked List using Dynamic Memory Allocation
Convert Binary Tree to Singly Linked List in C C Program to Convert Binary Tree to Singly Linked List

4. C Programs on Doubly Linked List

Program Description
Circular Doubly Linked List in C C Program to Implement Circular Doubly Linked List
Largest Element in a Doubly Linked List in C C Program to Find the Largest Element in a Doubly Linked List
Doubly Linked List using Singly Linked List in C C Program to Implement Doubly Linked List using Singly Linked List
Doubly Linked List Operations in C C Program to Implement Doubly Linked List Operations
Doubly Linked List to Balanced BST in C C Program to Convert Doubly Linked List to Balanced BST
Convert Binary Tree to Circular Doubly Linked List in C C Program to Convert Binary Tree to Circular Doubly Linked List

advertisement

5. C Programs on Data Structures using Linked List

Program Description
Stack using Linked List in C C Program to Implement Stack using Linked List
Queue using Linked List in C C Program to Implement Queue using Linked List
Binary Tree using Linked List in C C Program to Implement Binary Tree using Linked List
Find Middle Element of a Linked List in C C Program to Find Middle Element of a Linked List
Find Nth Node from End of Linked List in C C Program to Find Nth Node from End of Linked List

6. C Programs on Interchange and Modify Operations

Program Description
Swap Adjacent Nodes in a Circular Linked List in C C program to Swap Two Nodes in a Circular Linked List
Segregate Even and Odd Nodes in a Linked List in C C Program to Segregate Even and Odd Nodes in a Linked List
Swap Elements of List without Key Field in C C Program to Swap Two Elements of the List without Key Field
Arbitrary Precision Arithmetic in C C Program to Support Arbitrary Precision Arithmetic
Josephus Problem using Linked List in C C Program to Solve Josephus Problem using Linked List

7. C Programs on Linked List using Recursion

Program Description
Length of Linked List using Recursion in C C Program Find the Length of Linked List using Recursion
Length of Linked List without Recursion in C C Program Find the Length of Linked List without Recursion
Print All Nodes of Linked List using Recursion in C C Program to Print All Nodes of Linked List using Recursion
Print All Nodes of Linked List without Recursion in C C Program to Print All Nodes of Linked List without Recursion
Reverse a Linked List using Recursion in C C Program to Reverse a Linked List using Recursion
Reverse a Linked List without Recursion in C C Program to Reverse a Linked List without Recursion
Print Alternate Nodes of a Linked List using Recursion in C C Program to Print Alternate Nodes of a Linked List using Recursion
Print Alternate Nodes of a Linked List without Recursion in C C Program to Print Alternate Nodes of a Linked List without Recursion
Count Occurrences of Elements in a Linked List using Recursion in C C Program to Count the Occurrences of Elements in a Linked List using Recursion
Count Occurrences of Elements in a Linked List without Recursion in C C Program to Count the Occurrences of Elements in a Linked List without Recursion

Sample Linked List Program Using C

Here is source code of the C program to create a linked list & display the elements in the list.

advertisement
/*
 * C program to create a linked list and display the elements in the list
 */
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
 
void main()
{
    struct node
    {
        int num;
        struct node *ptr;
    };
    typedef struct node NODE;
 
    NODE *head, *first, *temp = 0;
    int count = 0;
    int choice = 1;
    first = 0;
 
    while (choice)
    {
        head  = (NODE *)malloc(sizeof(NODE));
        printf("Enter the data item\n");
        scanf("%d", &head-> num);
        if (first != 0)
        {
            temp->ptr = head;
            temp = head;
        }
        else
        {
            first = temp = head;
        }
        fflush(stdin);
        printf("Do you want to continue(Type 0 or 1)?\n");
        scanf("%d", &choice);
 
    }
    temp->ptr = 0;
    /*  reset temp to the beginning */
    temp = first;
    printf("\n status of the linked list is\n");
    while (temp != 0)
    {
        printf("%d=>", temp->num);
        count++;
        temp = temp -> ptr;
    }
    printf("NULL\n");
    printf("No. of nodes in the list = %d\n", count);
}

advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.