Insert a node in Doubly Linked List, from left to right - C Program

Write a C program to insert a node in a doubly linked list, from left to right so that nodes of that doubly linked list will be in ascending order (using double pointers).

Solution:

In this example, we insert the node in double linked list and then arrange it on ascending order. We will use structure and double pointer in below example.

Program

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct node
{
    int info;
    struct node *next;
    struct node *previous;
}s;
void insertion(s **,s**,int);
void display(s*);
void arrange(s*);
void main()
{
    int ch,n;
    s *front=NULL;
    s*rear=NULL;
    do
    {
        printf("\n1.insertion\n2.display\n3.arrange\n");
        printf("Enter your choice: ");
        scanf("%d",&ch);
        switch(ch)
        {
            case 1:
            printf("\nEnter the no to be inserted: ");
            scanf("%d",&n);
            //call the insertion function
            insertion(&front,&rear,n);
            break;
           
            case 2:
            // calling the display function
            display(front);
            break;
           
            case 3:
            // calling the arrange function for sort the element.
            arrange(front);
            break;
         }
     }while(ch!=4);
     getch();
}
void insertion(s**front,s**rear,int n)
{
    s*p=NULL;
    p=(s*)malloc(sizeof(s));
    p->info=n;
    p->next=NULL;
    if((*rear)==NULL)
    {
        *rear=p;
        *front=p;
        p->previous=NULL;
    }
    else
    {
        (*rear)->next=p;
        p->previous=(*rear);
        (*rear)=p;
        (*rear)->next=NULL;
    }
}
void arrange(s*tp)
{
    s*p=NULL;
    s*q=NULL;
    int t;
    for(p=tp;p!=NULL;p=p->next)
    {
        for(q=p->next;q!=NULL;q=q->next)
        {
            if(p->info>q->info)
            {
                t=(p->info);
                (p->info)=(q->info);
                (q->info)=t;
            }
        }
    }
}
void display(s*front)
{
    if(front==NULL)
    {
        printf("no node to display");
    }
    else
    {
        while(front!=NULL)
        {
             printf("%d\t",front->info);
             front=front->next;
        }
    }
}


Output:

doubly linked list