HOME

Sunday, February 19, 2017

#Program to insert data and display data using double linked list

#include<stdio.h>
#include<conio.h>
struct node
{
    int data;
    struct node *p;
    struct node *n;
};
struct node *first=NULL;
struct node *last=NULL;
void insert(void);
void displayB(void);
void displayL(void);
void main()
{
    while(1)
    {
    int ch;
    printf("1.To insert Data\n");
    printf("2.To Display Data From beginning\n");
    printf("3. To Display Data from End\n");
    printf("4. Exit\n");
    printf("Enter Choice\n");
    scanf("%d",&ch);
    switch(ch)
    {
    case 1:
        insert();
        break;
    case 2:
        displayB();
        break;
    case 3:
        displayL();
        break;
    case 4:
        exit(1);
        break;
    default:
        printf("Wrong input\n");
        break;
    }
    }
}
void insert(void)
{
    int v;
    printf("Enter value\n");
    scanf("%d",&v);
    struct node *ptr;
    ptr=(struct node *)malloc(sizeof(struct node));
    ptr->data=v;
    if(first==NULL)
    {
        ptr->p=NULL;
        ptr->n=NULL;
        first=last=ptr;
    }
    else{
               ptr->p=last;
        ptr->n=NULL;
        last->n=ptr;
         last=ptr;
    }
}
void displayB()
{
    struct node *ptr;
    ptr=first;
    printf("\nThe list of Data from Beginning are;");
    while(ptr->n!=NULL)
    {
        printf("\n%d",ptr->data);
    ptr=ptr->n;
    }
    printf("\n%d\n",ptr->data);

}
void displayL(void)
{
    struct node *ptr;
    ptr=last;
    printf("\nThe list of data from Last are:");
    while(ptr->p!=NULL)
    {
        printf("\n%d",ptr->data);
        ptr=ptr->p;
    }
    printf("\n%d\n",ptr->data);
}

No comments:

Post a Comment