C PROGRAM : DOUBLY LINKED LIST

 

//To insert an element at the beginning of the doubly linked list

 

#include<stdlib.h>

#include<stdio.h>

#include<conio.h>

 

struct node

{

            int data;

            struct node *next;

            struct node *prev;

 

}*start = NULL;

 

void insert_begin()

{

            struct node *temp,*ptr;

            temp = malloc(sizeof(struct node));

 

            scanf("%d",&temp->data );

            temp->prev =NULL;

temp->next =NULL;

 

            if(start==NULL)

                        start=temp;

            else

            {

                        temp->next =start;

                        start=temp;

            }

}

 

void display()

{

            struct node *ptr;

                        ptr=start;

                        printf("\n\nElements in the Doubly Linked List are: ");

                        while(ptr!=NULL)

                        {

                                    printf("%d\t",ptr->data );

                                    ptr=ptr->next ;

                        }

 

}

 

 

void main()

{

            char ch;

            clrscr();

 

            do

            {

                        printf("\nEnter the data value for the node: ");

                        insert_begin();

                        printf("Continue to add element at end of list(y/n):");

                        ch=getch();

 

            } while (ch=='y');

 

            display();

            getch();

}

 

 

OUTPUT:

 

 

Enter the data value for the node: 6

Continue to add element at end of list(y/n): y

Enter the data value for the node: 7

Continue to add element at end of list(y/n): y

Enter the data value for the node: 8

Continue to add element at end of list(y/n): y

Enter the data value for the node: 3

Continue to add element at end of list(y/n): y

Enter the data value for the node: 2

Continue to add element at end of list(y/n): y

Enter the data value for the node: 1

Continue to add element at end of list(y/n): n

 

Elements in the Doubly Linked List are: 1       2       3       8       7        6

Comments

Popular posts from this blog

Backtracking - N-Queens Problem, Sum of Subsets, Graph Colouring, Hamiltonian Cycle

Divide and Conquer Technique - Binary Search, Quick Sort, Merge Sort

GRAPH THEORY