WRITE A PROGRAM TO CREATES A SIMPLE DOUBLY LINKED LIST


#include<stdio.h>
#include<malloc.h>

struct dlist
{
    int info;
    struct dlist *link;
    struct dlist *prev;
};

int num ;
struct dlist start;
void create_dlist (struct dlist *);
void display (struct dlist *);

/* Function to creates doubly linked list */

void create_dlist(struct dlist *ptr)
{
    int n,i=0;
    start.link = NULL;  /* Empty list */
    start.prev = NULL;
    ptr = &start;      /* Ptr points to the start pointer */
    printf("\n How many nodes you want to create: ");
    scanf("%d",&n);
    printf("\n\n =======================================================\n");
    printf("\n Note That:\n -----------\n Enter numeric data in node\n");
    printf("\n =======================================================\n");
    while( i!=n)
    {
        ptr->link = (struct dlist *) malloc(sizeof(struct dlist));
        ptr->link->prev = ptr;
        ptr = ptr->link;
        printf("\n Enter the values of the node : %d: ", (i+1));
        scanf("%d", &ptr->info);
        ptr->link = NULL;
        fflush(stdin);
        i++;
    }
    printf("\n\n--------------------");
    printf("\n Total nodes = %d",i);

}

/* Function to display the list */

void display (struct dlist *ptr)
{
    ptr = start.link;
    printf("\n\n\t\t-----------------");
    printf("\n\t\tForwad Traversal:\n\t\t-----------------\n\n");
    while (ptr->link)
    {

        printf("\n\n\t\t  %d", ptr->info);
        ptr = ptr->link;
    }
    printf("\n\n\t\t  %d", ptr->info);

    printf("\n\n\t\t-------------------");
    printf("\n\t\tBackward Traversal:\n\t\t-------------------\n\n");
    do {

        printf("\n\n\t\t  %d", ptr->info);
        ptr = ptr->prev;
    } while (ptr->prev);
}

void main()
{
    struct dlist *ptr = (struct dlist *) malloc(sizeof(struct dlist));
    clrscr();
    create_dlist(ptr);
    printf("\n\n--------------------");
    printf("\n\n\n Doubly linked list is: \n");
    printf("\n========================");
    display(ptr);
    getch();
}


Comments

Popular posts from this blog

Write a program to add two number using inline function in C++?

Traversing of elements program with algorithm and Flowchart