Singly Linked List in C


The following programs are covered as part of the linked list.

  • Appending the linked list 
  • Insert the node after nth position 
  • Delete the node in the linked list 
  • Count the number of nodes in linked list
  • Reverse the linked list 
  • Find the list is circular or not 

string.h library is included for malloc function call.

Append/Add the node in the linked list or Create linked list

#include <stdio.h>
#include <string.h>

typedef struct list_n {
    int data;
    struct list_n *next;
}list_t;

void list_append(list_t **list_head, int num)
{
    list_t *temp, *iterator;
    
    temp = (list_t *)malloc(sizeof(list_t));
    temp->data = num;
    temp->next = NULL;

    if (*list_head == NULL) {
        // If list is empty
        *list_head = temp;
        //printf("%s %d %d\n",__func__,__LINE__,(*list_head)->data);
    } else { 
        iterator = *list_head;
        while (iterator->next != NULL) {
             iterator = iterator->next;
        }
        iterator->next = temp;
        //printf("%s %d %d\n",__func__,__LINE__,temp->data);
    }

    return 0; 
}

void print_list (list_t *list_p) 
{
     list_t *temp;
     temp = list_p;
     printf("List Elements\n");
     while (temp != NULL) {
         printf("%d ",temp->data);
         temp = temp->next;
     }
     return 0;
}

void main() 
{
    list_t *list; 
    int num;

    for(num=0; num<5; num++ ) {
        list_append(&list, num);
    }
    print_list(list);
    return 0;
}


Output:
1
2
List Elements
0 1 2 3 4 

Add at the beginning of the list

#include <stdio.h>
#include <string.h>

typedef struct list_n {
    int data;
    struct list_n *next;
}list_t;

void list_append(list_t **list_head, int num)
{
    list_t *temp, *iterator;
    
    temp = (list_t *)malloc(sizeof(list_t));
    temp->data = num;
    temp->next = NULL;

    if (*list_head == NULL) {
        // If list is empty
        *list_head = temp;
        //printf("%s %d %d\n",__func__,__LINE__,(*list_head)->data);
    } else { 
        iterator = *list_head;
        while (iterator->next != NULL) {
             iterator = iterator->next;
        }
        iterator->next = temp;
        //printf("%s %d %d\n",__func__,__LINE__,temp->data);
    }

    return; 
}

void addbeginning_list(list_t **list_head, int num) 
{
    list_t *temp;
    temp = (list_t *)malloc(sizeof(list_t));
    temp->data = num;
    
    if (*list_head == NULL) {
        *list_head = temp;
        (*list_head)->next = NULL;
    } else {
        temp->next = *list_head;
        *list_head = temp;
    }
    printf("\nList's head is changed to %d\n", (*list_head)->data);
    return;
}

void print_list (list_t *list_p) 
{
     list_t *temp;
     temp = list_p;
     printf("List Elements\n");
     while (temp != NULL) {
         printf("%d ",temp->data);
         temp = temp->next;
     }
     return;
}

void main() 
{
    list_t *list; 
    int num;

    for(num=0; num<5; num++ ) {
        list_append(&list, num);
    }
    print_list(list);
    addbeginning_list(&list, 8);
    print_list(list);
    return;
}

Output:
1
2
3
4
5
List Elements
0 1 2 3 4 
List's head is changed to 8
List Elements
8 0 1 2 3 4 

Add a node after nth position in the linked list

#include <stdio.h>
#include <string.h>

typedef struct list_n {
    int data;
    struct list_n *next;
}list_t;

void list_append(list_t **list_head, int num)
{
    list_t *temp, *iterator;
    
    temp = (list_t *)malloc(sizeof(list_t));
    temp->data = num;
    temp->next = NULL;

    if (*list_head == NULL) {
        // If list is empty
        *list_head = temp;
        //printf("%s %d %d\n",__func__,__LINE__,(*list_head)->data);
    } else { 
        iterator = *list_head;
        while (iterator->next != NULL) {
             iterator = iterator->next;
        }
        iterator->next = temp;
        //printf("%s %d %d\n",__func__,__LINE__,temp->data);
    }

    return; 
}

void addafter_list(list_t *list_aft, int pos, int data)
{   
    list_t *temp, *travel; 
    int count = 1;
    travel = list_aft;

    temp = (list_t *)malloc(sizeof(list_t));
    temp->data = data;
    
    while (travel != NULL) {
        travel = travel->next;
        count++;
        if (pos == count) {
            temp->next = travel->next;
            travel->next = temp;
            printf("\nInserted a node with value %d after a node's value %d with position %d\n",
                   temp->data, travel->data, pos);
            break;
        } 
    }
    return;
}

void print_list (list_t *list_p) 
{
     list_t *temp;
     temp = list_p;
     printf("List Elements\n");
     while (temp != NULL) {
         printf("%d ",temp->data);
         temp = temp->next;
     }
     return;
}

void main() 
{
    list_t *list; 
    int num, pos = 2;

    for(num=0; num<5; num++ ) {
        list_append(&list, num);
    }
    print_list(list);
    addafter_list(list,pos, 13);
    print_list(list);
    return;
}

Output:
1
2
3
4
5
List Elements
0 1 2 3 4 
Inserted a node with value 13 after a node's value 1 with position 2
List Elements
0 1 13 2 3 4 


Delete the node in the linked list


#include <stdio.h>
#include <string.h>

typedef struct list_n {
    int data;
    struct list_n *next;
}list_t;

void list_append(list_t **list_head, int num)
{
    list_t *temp, *iterator;
    
    temp = (list_t *)malloc(sizeof(list_t));
    temp->data = num;
    temp->next = NULL;

    if (*list_head == NULL) {
        // If list is empty
        *list_head = temp;
        //printf("%s %d %d\n",__func__,__LINE__,(*list_head)->data);
    } else { 
        iterator = *list_head;
        while (iterator->next != NULL) {
             iterator = iterator->next;
        }
        iterator->next = temp;
        //printf("%s %d %d\n",__func__,__LINE__,temp->data);
    }

    return; 
}

void print_list (list_t *list_p) 
{
     list_t *temp;
     temp = list_p;
     printf("List Elements\n");
     while (temp != NULL) {
         printf("%d ",temp->data);
         temp = temp->next;
     }
     return;
}

void delete_list(list_t **list_del, int num)
{
    list_t *temp, *prev;
    temp = *list_del;
    while(temp != NULL) {
        if (temp->data == num) {
            if (temp == *list_del) {
                *list_del = temp->next;
            } else {
                prev->next = temp->next;    
            }
            printf("\nThe node with data %d is deleted\n", temp->data);
            free(temp);
            return;
        } else {
            prev = temp; 
            temp = temp->next;
        } 
    }
    printf("\nThe data to be deleted %d is not found", num);
    return;
}

void main() 
{
    list_t *list; 
    int num, pos = 2;

    for(num=0; num<5; num++ ) {
        list_append(&list, num);
    }
    print_list(list);
    delete_list(&list, 2);
    print_list(list);
    return;
}

Output:
1
2
3
4
5
List Elements
0 1 2 3 4 
The node with data 2 is deleted
List Elements
0 1 3 4 


Count the number of nodes in the list

#include <stdio.h>
#include <string.h>

typedef struct list_n {
    int data;
    struct list_n *next;
}list_t;

void list_append(list_t **list_head, int num)
{
    list_t *temp, *iterator;
    
    temp = (list_t *)malloc(sizeof(list_t));
    temp->data = num;
    temp->next = NULL;

    if (*list_head == NULL) {
        // If list is empty
        *list_head = temp;
        //printf("%s %d %d\n",__func__,__LINE__,(*list_head)->data);
    } else { 
        iterator = *list_head;
        while (iterator->next != NULL) {
             iterator = iterator->next;
        }
        iterator->next = temp;
        //printf("%s %d %d\n",__func__,__LINE__,temp->data);
    }

    return; 
}

void print_list (list_t *list_p) 
{
     list_t *temp;
     temp = list_p;
     printf("List Elements\n");
     while (temp != NULL) {
         printf("%d ",temp->data);
         temp = temp->next;
     }
     return;
}

int count_list (list_t *list_c) 
{
    int count = 0;
    while(list_c != NULL) {
        count++;
        list_c = list_c->next;
    }
    return count;
}

void main() 
{
    list_t *list; 
    int num, pos = 2, count = 0;

    for(num=0; num<5; num++ ) {
        list_append(&list, num);
    }
    print_list(list);
    count = count_list(list);
    printf("\nNumber of elements in the list is %d\n", count);
    return;
}


Output:
1
2
3
List Elements
0 1 2 3 4 
Number of elements in the list is 5


Reverse the linked list

#include <stdio.h>
#include <string.h>

typedef struct list_n {
    int data;
    struct list_n *next;
}list_t;

void list_append(list_t **list_head, int num)
{
    list_t *temp, *iterator;
    
    temp = (list_t *)malloc(sizeof(list_t));
    temp->data = num;
    temp->next = NULL;

    if (*list_head == NULL) {
        // If list is empty
        *list_head = temp;
        //printf("%s %d %d\n",__func__,__LINE__,(*list_head)->data);
    } else { 
        iterator = *list_head;
        while (iterator->next != NULL) {
             iterator = iterator->next;
        }
        iterator->next = temp;
        //printf("%s %d %d\n",__func__,__LINE__,temp->data);
    }

    return; 
}

void print_list (list_t *list_p) 
{
     list_t *temp;
     temp = list_p;
     printf("List Elements\n");
     while (temp != NULL) {
         printf("%d ",temp->data);
         temp = temp->next;
     }
     printf("\n");
     return;
}

void reverse_list (list_t **list_rev) 
{
    list_t *temp, *rev, *std;
    temp = *list_rev;
    rev = NULL;
    while(temp != NULL) {
        std = rev; 
        rev = temp;
        temp = temp->next;
        rev->next = std;  //save the node every iteration in rev
    }
    *list_rev = rev;
    return;
}

void main() 
{
    list_t *list; 
    int num, pos = 2;

    for(num=0; num<5; num++ ) {
        list_append(&list, num);
    }
    print_list(list);
    reverse_list(&list);
    print_list(list);
    return;
}

Output:
1
2
3
4
List Elements
0 1 2 3 4 
List Elements
4 3 2 1 0 


Find if the list is circular or not 

<to be updated>

Notice that we used a recursive data structure in which the data structure contains itself. 





Comments