#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node* next;
struct node* prev;
}node;
node* start;
void insert()
{
node *ptr, *temp;
int val, loc, i;
ptr=(node *)malloc(sizeof(node));
if(ptr==NULL)
{
printf("Overflow!\n");
return;
}
temp=start;
printf("Enter location\n");
scanf("%d", &loc);
for(i=0; i<loc; i++)
{
temp=temp->next;
if(temp==NULL)
{
printf("There are less than %d elements in list\n", loc);
return;
}
}
printf("Enter value to be entered\n");
scanf("%d", &val);
ptr->data=val;
ptr->next=temp->next;
ptr->prev=temp;
temp->next=ptr;
temp->next->prev=ptr;
printf("Element %d added successfully at %d position", val, loc);
}
void delete()
{
node *ptr, *temp;
ptr=start;
if(ptr==NULL)
{
printf("Underflow\n");
return;
}
int val;
printf("Enter the value after which element has to be deleted\n");
scanf("%d", &val);
while(ptr->data!=val)
ptr=ptr->next;
if(ptr->next=NULL)
printf("Can't delete\n");
else if(ptr->next->next==NULL)
{
ptr->next=NULL;
printf("Node deleted\n");
}
else
{
temp=ptr->next;
ptr->next=temp->next;
temp->next->prev=ptr;
free(temp);
printf("Node deleted\n");
}
}
void display()
{
node *ptr;
ptr=start;
while(ptr!=NULL)
{
printf("%d ", ptr->data);
ptr=ptr->next;
}
if(ptr==NULL)
{
printf("List is empty\n");
}
}
void main()
{
int a;
do
{
printf("\nEnter 1 for insert, 2 for delete, 3 for display and 4 for exit\n");
scanf("%d", &a);
switch(a)
{
case 1 : insert();
break;
case 2 : delete();
break;
case 3 : display();
break;
case 4 : printf("Program terminated successfully\n");
break;
default : printf("Enter valid inputs\n");
break;
}
}
while(a!=4);
}
To embed this program on your website, copy the following code and paste it into your website's HTML: