C Program to create and display a Doubly Linked List (DLL) | DLL | C Program

C Program to create and display a Doubly Linked List (DLL)
DOUBLY LINKED LIST


  Create and Display a Doubly Linked List | C Program

#include<stdio.h>

#include<stdlib.h>

struct node
{
    int data;
    struct node *prev;
    struct node *next;
};
struct node *start=NULL;

main()
{
    int i,value,ne;  // ne=Number of element in list
    struct node *n,*temp;

    n=malloc(sizeof(struct node));

    printf("\n\n\tHow many value do you want to Enter in List: ");
    scanf("%d",&ne);

    printf("\nEnter first data for list: ");
    scanf("%d",&value);

    n->data=value;
    n->prev=NULL;
    n->next=NULL;
    start=n;

    for(i=2;i<=ne;i++)
    {
        n=malloc(sizeof(struct node));

        printf("Enter next data: ");
        scanf("%d",&value);

        n->data=value;
        n->prev=NULL;
        n->next=NULL;
        temp=start;

        while(temp->next!=NULL)
            temp=temp->next;

        temp->next=n;
        n->prev=temp;
    }

    //for display
    if(start==NULL)
        printf("\n\t** List is empty **");

    else
    {
        temp=start;

        printf("\n\tData in the List: ");

        while(temp!=NULL)
        {
            printf("%d\t",temp->data);

            temp=temp->next;
        }
    }
    getch();
}

OUTPUT:


Post a Comment

0 Comments