Doubly Linked List in C


The following programs are covered as part of the doubly linked list.
  • Appending the linked list 
  • Add the node in the beginning of the list
  • Insert the node after nth position 
  • Delete the node in the linked list
  • Reverse the linked list 

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;
    struct list_n *prev;
}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;

    if (*list_head == NULL) {
        // If list is empty
        *list_head = temp;
        (*list_head)->next = NULL;
        (*list_head)->prev = NULL;
        printf("%s %d %d\n",__func__,__LINE__,(*list_head)->data);
    } else { 
        iterator = *list_head;
        while (iterator->next != NULL) {
             iterator = iterator->next;
        }
        iterator->next = temp;
        temp->prev = iterator;
        temp->next = NULL;
        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 main() 
{
    list_t *list; 
    int num;

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


Output:
1
2
3
4
5
6
7
list_append 23 0
list_append 32 1
list_append 32 2
list_append 32 3
list_append 32 4
List Elements
0 1 2 3 4 


Add a node at the beginning of the list


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

typedef struct list_n {
    int data;
    struct list_n *next;
    struct list_n *prev;
}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;

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

    return; 
}

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

    if (*list_head == NULL) {
        // If list is empty
        *list_head = temp;
        (*list_head)->next = NULL;
        (*list_head)->prev = NULL;
        printf("Since the list is empty, added node with data %d in the beginning of the list\n",(*list_head)->data);
    } else { 
        temp->next = *list_head;
        *list_head = temp;
        temp->prev = NULL;
        printf("Added node with data %d in the beginning of the list\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;
    }
    printf("\n");
    return;
}

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

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

Output:
1
2
3
4
5
6
7
8
9
10
list_append 23 0
list_append 32 1
list_append 32 2
list_append 32 3
list_append 32 4
List Elements
0 1 2 3 4 
Added node with data 5 in the beginning of the list
List Elements
5 0 1 2 3 4 

Insert the node after nth position 

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

typedef struct list_n {
    int data;
    struct list_n *next;
    struct list_n *prev;
}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;

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

    return; 
}

void list_addafter(list_t *list_head, int pos, int num)
{
    list_t *temp, *iterator;
    int count = 0;
    iterator = list_head;
    temp = (list_t *)malloc(sizeof(list_t));
    temp->data = num;

    while(iterator != NULL) {
        count++; 
        if (pos == count) {
            temp->next = iterator->next;
            iterator->next = temp;
            temp->prev = iterator; 
            iterator->next->prev = temp;
            printf("Added the node with data %d after the pos %d\n",temp->data,pos);

            break;
        }
        iterator = iterator->next;
    }
    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 main() 
{
    list_t *list; 
    int num, pos = 3;

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

Output:
1
2
3
4
5
6
7
8
9
10
list_append 23 0
list_append 32 1
list_append 32 2
list_append 32 3
list_append 32 4
List Elements
0 1 2 3 4 
Added the node with data 5 after the pos 3
List Elements
0 1 2 5 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;
    struct list_n *prev;
}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;

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

    return; 
}

void list_del(list_t **list_head, int num)
{
    list_t *temp, *iterator, *previous;
    int count = 0;
    iterator = *list_head;

    while(iterator != NULL) { 
        if (iterator->data == num) {
            if (*list_head == iterator) {
                (*list_head)->next = iterator->next;
                (*list_head)->prev = NULL;
            } else {
                previous->next = iterator->next;
                iterator->next->prev = previous;
            }
            printf("Deleted the node with data %d \n",iterator->data);
            free(iterator);
            break;
        } else {
            previous = iterator;
            iterator = iterator->next;
        }
    }
    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 main() 
{
    list_t *list; 
    int num, pos = 3;

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

Output:
1
2
3
4
5
6
7
8
9
10
list_append 23 0
list_append 32 1
list_append 32 2
list_append 32 3
list_append 32 4
List Elements
0 1 2 3 4 
Deleted the node with data 3 
List Elements
0 1 2 4 


Reverse the linked list

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

typedef struct list_n {
    int data;
    struct list_n *next;
    struct list_n *prev;
}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;

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

    return; 
}

void reverse_list (list_t **list_rev) 
{
    list_t *temp, *std;
    temp = *list_rev;

    while(temp != NULL) {
        std = temp->prev;
        temp->prev = temp->next;
        temp->next = std;
        temp = temp->prev; 
    }

    if (std != NULL) {
        *list_rev = std->prev;
    } 
    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 main() 
{
    list_t *list; 
    int num, pos = 3;

    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
5
6
7
8
9
list_append 23 0
list_append 32 1
list_append 32 2
list_append 32 3
list_append 32 4
List Elements
0 1 2 3 4 
List Elements
4 3 2 1 0 



Comments