You are on page 1of 3

#include<stdio.

h>
#include<stdlib.h>
struct node{
int data;
struct node *next,*prev;
};
struct node *temp,*head,*start=NULL,*tail;
void inserthead(){
int x;
printf("enter the enter to insert at head");
scanf("%d",&x);
temp=(struct node *)malloc(sizeof(struct node));
temp->data=x;
temp->next=NULL;
temp->prev=NULL;
if(start==NULL)
{
start=temp;
head=temp;
tail=temp;
}
else{
temp->next=head;
head->prev=temp;
temp=head;
}
}
void inserttail(){
int y;
printf("enter the enter to insert at tail");
scanf("%d",&y);
temp=(struct node *)malloc(sizeof(struct node));
temp->data=y;
temp->next=NULL;
temp->prev=NULL;
if(start==NULL)
{
start=temp;
head=temp;
tail=temp;
}
else{
temp->prev=tail;
tail->next=temp;
temp=tail;
}
}
void print(){
struct node *prnt;
prnt=head;
printf("the double linked list elements are:\n");
while(prnt!=NULL){
printf("%d->",prnt->data);
prnt=prnt->next;
}
}

void delnode(){
struct node *del,*r;
del=head;
int d,k=1;
printf("enter the position to delete element in that posit
ion ");
scanf("%d",&d);
while(k<d-1)
{
del=del->next;
k++;
}
r=del->next;
del->next=r->next;
r->next->prev=del;
free(r);
}
int main(){
int i;
do{
printf("menu:\n");
printf("1 insert naode at head\n");
printf("2 insert node at tail\n");
printf("3 delete node at nth position\n");
printf("4 print all double linked list elemens\n");
printf("5 exit\n");
printf("enter your choice to proceed");
scanf("%d",&i);
switch(i){
case 1:
inserthead();
break;
case 2:
inserttail();
break;
case 3:
delnode();
break;
case 4:
print();
break;
}
}while(i!=5);
}

You might also like