Question description saran, subash, and Yasir alias Pari are three first-year engineering students of the State Technical Institution (STI), India. While saran and subash are average students who come from a Middle class, Yasir is from a rich family. saran studies, engineering as per his father's wishes, while subash, whose family is poor, studies engineering to improve his family's financial situation. Yasir, however, studies engineering of his simple passion for developing android applications. Yasir is participating in a hackathon for android application development. the task is Insertion in a Doubly Linked list at beginig. Functional Description: In the doubly linked list, we would use the following steps to insert a new node at the beginning of the doubly linked list. Create a new node Assign its data value Assign newly created node’s next ptr to current head reference. So, it points to the previous start node of the linked list address Change the head reference to the new node’s address. Change the next node’s previous pointer to new node’s address (head reference)
C Program


#include<stdio.h>

#include<stdlib.h>

struct Node

{

    int data;

    struct Node *next;

    struct Node *prev;

};

void insertStart(struct Node** head,int data)

{

    struct Node* newNode=(struct Node*)malloc(sizeof(struct Node));

    newNode->data=data;

    newNode->prev=NULL;

    newNode->next=*head;

    if(*head!=NULL)

        (*head)->prev=newNode;

    *head=newNode;

}

int main()

{

    int n,i,x;

    scanf("%d",&n);

    struct Node* head=NULL;

    for(i=0;i<n;i++)

    {

        scanf("%d",&x);

        insertStart(&head,x);

    }

    struct Node* temp=head;

    struct Node* last;

    while(temp!=NULL)

    {

        printf("%d ",temp->data);

        if(temp->next==NULL)

            last=temp;

        temp=temp->next;

    }

    printf("\n");

    while(last!=NULL)

    {

        printf("%d ",last->data);

        last=last->prev;

    }

    return 0;

}