C program to create and traverse Doubly Linked List

Write a C program to implement Doubly linked list data structure. Write a C program to create a doubly linked list and display all nodes of the created list. How to create and display a doubly linked list in C.
Doubly linked list representation

Required knowledge

Basic C programming, Dynamic memory allocation, Doubly linked list

Algorithm to create a Doubly linked list

Algorithm to create Doubly Linked list
Begin:
    alloc (head)
    If (head == NULL) then
        write ('Unable to allocate memory')
    End if
    Else then 
        read (data)
        head.datadata;
        head.prevNULL;
        head.nextNULL;
        lasthead;
        write ('List created successfully')
    End else
End

Algorithm to traverse or display Doubly linked list from beginning

Algorithm to traverse Doubly Linked list from beginning
%% Input :  head {Pointer to the first node of the list}
Begin:
    If (head == NULL) then
        write ('List is empty')
    End if
    Else then
        temphead;
        While (temp != NULL) do
            write ('Data = ', temp.data)
            temptemp.next;
        End while
    End else
End

Algorithm to traverse or display Doubly linked list from end

Algorithm to traverse Doubly Linked list from end
%% Input :  last {Pointer to the last node of the list}
Begin:
    If (last == NULL) then
        write ('List is empty')
    End if
    Else then
        templast;
        While (temp != NULL) do
            write ('Data = ', temp.data)
            temptemp.prev;
        End while
    End else
End

Steps to create Doubly linked list

Create a head node and assign some data to its data field.
Make sure that the previous and next address field of the head node must point to NULL.
Make the head node as last node.
Creation of doubly linked list step1


If you want to create more nodes then follow these steps:
Create a new node and assign some data to its data field.
Creation of doubly linked list step 2
Make sure that the next address field of new node must point to NULL.
Creation of doubly linked list step 3
Link the new node previous address field with last node.
Creation of doubly linked list step 4
Link the last node next address field with new node.
Creation of doubly linked list step 5
Move the last node to new node i.e. last node will now point to new node.
Creation of doubly linked list step 6
Repeat Steps 4-8 if you want to add more nodes to the list.

Program

/**
 * C program to create and display Doubly linked list
 */

#include <stdio.h>
#include <stdlib.h>


/* 
 * Basic structure of Node
 */
struct node {
    int data;
    struct node * prev;
    struct node * next;
}*head, *last;



/*
 * Functions used in this program 
 */
void createList(int n);
void displayListFromFirst();
void displayListFromEnd();



int main()
{
    int n, choice;

    head = NULL;
    last = NULL;
    
    printf("Enter the number of nodes you want to create: ");
    scanf("%d", &n);

    createList(n); //Creates list of n nodes

    printf("\nPress 1 to display list from First");
    printf("\nPress 2 to display list from End : ");
    scanf("%d", &choice);

    if(choice==1)
    {
        displayListFromFirst();
    }
    else if(choice == 2)
    {
        displayListFromEnd();
    }

    return 0;
}



/**
 * Creates a doubly linked list of n nodes.
 *
 * @n Number of nodes to be created
 */
void createList(int n)
{
    int i, data;
    struct node *newNode;

    if(n >= 1)
    {
        head = (struct node *)malloc(sizeof(struct node));

        if(head != NULL)
        {
            printf("Enter data of 1 node: ");
            scanf("%d", &data);

            head->data = data;
            head->prev = NULL;
            head->next = NULL;

            last = head;

            /*
             * Creates rest of the n-1 nodes
             */
            for(i=2; i<=n; i++)
            {
                newNode = (struct node *)malloc(sizeof(struct node));

                if(newNode != NULL)
                {
                    printf("Enter data of %d node: ", i);
                    scanf("%d", &data);

                    newNode->data = data;
                    newNode->prev = last; //Links new node with the previous node
                    newNode->next = NULL;

                    last->next = newNode; //Links previous node with the new node
                    last = newNode; //Makes new node as last/previous node
                }
                else
                {
                    printf("Unable to allocate memory.");
                    break;
                }
            }

            printf("\nDOUBLY LINKED LIST CREATED SUCCESSFULLY\n");
        }
        else
        {
            printf("Unable to allocate memory");
        }
    }
}



/**
 * Displays the content of the list from beginning to end
 */
void displayListFromFirst()
{
    struct node * temp;
    int n = 1;

    if(head == NULL)
    {
        printf("List is empty.");
    }
    else
    {
        temp = head;
        printf("\n\nDATA IN THE LIST:\n");

        while(temp != NULL)
        {
            printf("DATA of %d node = %d\n", n, temp->data);

            n++;
            
            /* Moves the current pointer to next node */
            temp = temp->next;
        }
    }
}



/**
 * Displays the content of the list from last to first
 */
void displayListFromEnd()
{
    struct node * temp;
    int n = 0;

    if(last == NULL)
    {
        printf("List is empty.");
    }
    else
    {
        temp = last;
        printf("\n\nDATA IN THE LIST:\n");

        while(temp != NULL)
        {
            printf("DATA of last-%d node = %d\n", n, temp->data);

            n++;
            
            /* Moves the current pointer to previous node */
            temp = temp->prev; 
        }
    }
} 

Output
Enter the number of nodes you want to create: 5
Enter data of 1 node: 10
Enter data of 2 node: 20
Enter data of 3 node: 30
Enter data of 4 node: 40
Enter data of 5 node: 50

DOUBLY LINKED LIST CREATED SUCCESSFULLY

Press 1 to display list from First
Press 2 to display list from End : 1


DATA IN THE LIST:
DATA of 1 node = 10
DATA of 2 node = 20
DATA of 3 node = 30
DATA of 4 node = 40
DATA of 5 node = 50

Happy coding ;)
Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment