Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

#include<stdio.

h>
#include<stdlib.h>
struct node{
int data;
struct node *next,*prev;
};
int a,b,c;
struct node *head=NULL,*temp,*new;
struct node *getnode(int n)
{
struct node *new;
new=(struct node *)malloc(sizeof(struct node*));
new->data=n;
new->next=NULL;
new->prev=NULL;
return new;
}
void insert_beg()
{
printf("Enter a number to insert at begining : ");
scanf("%d",&c);
new=getnode(c);
if(head==NULL)
{
head=new;
}
else
{
new->next=head;
head->prev=new;
head=new;
}
}
void display(struct node *head)
{
struct node *temp;
temp=head;
if(temp==NULL)
{
printf("List is empty");
}
else
{
while(temp!=NULL)
{
printf("%d<-->",temp->data);
temp=temp->next;
}
}
}
void main()
{
printf("Enter 0 to add number (or) 1 to exit : ");
scanf("%d",&b);
while(b==0)
{
printf("Enter a number to add in DLL : ");
scanf("%d",&a);
new=getnode(a);
if(head==NULL)
{
head=temp=new;
}
else
{
temp->next=new;
new->prev=temp;
temp=new;
}
printf("Enter 0 to add another number (or) 1 to exit : ");
scanf("%d",&b);
}
display(head);
printf("\n");
insert_beg();
display(head);
}

You might also like