Write a C program to perform all insertion, Deletion and search operation in an employee database using doubly linked list.

Leave a Comment
#include<stdio.h>
#include<conio.h>


struct node

{
struct node *prev;
int id;
char name[20];
struct node *next;
};
struct node *first=NULL,*last=NULL;
int n;
void append(int n)
{
struct node *t;

int i;

first=(struct node*)malloc(sizeof(struct node));
first->prev=NULL;
printf("\nEnter employee id:");
scanf("%d",&first->id);
printf("\nEnter the employee name:");
scanf("%s",first->name);
first->next=NULL;
first=last;

for(i=1;i<n;i++)

 {
t=(struct node*)malloc(sizeof(struct node));
t->prev=last;
printf("\nEnter employee id:");
scanf("%d",&t->id);
printf("\nEnter the employee name:");
scanf("%s",t->name);
t->next=NULL;
last->next=t;



}
}



void display()
{
struct node *t;
t=first;
do
{
printf("\nEmployee id:%d",t->id);
printf("\nEmployee name:%s",t->name);
t=t->next;
printf("\n\n");
}while(t->next!=NULL);
}

void del()
{
struct node *t;
int n;
printf("\nEnter the employee id to be deleted:");
scanf("%d",&n);
while(t!=NULL)
{
if(t->id==n)
{
t=t->next;
t->prev=NULL;
}
else
{
if(t->next==NULL)
t->prev->next=NULL;
else{
t->prev->next=t->next;
t->next->prev=t->prev;
}
}
return;

t=t->next;
}

}


void search()
{

struct node *t;
int n,flag=0;
printf("\nEnter the employee id to be searched:");
scanf("%d",&n);
while(t!=NULL)
{
if(t->id==n)
{
printf("\nThe id found\nNAME:%s",t->name);
flag=1;
break;
}
}
if(flag==0)
printf("\nThe id not found!!!");
}


int main()
{
int o;
while(o!=0)
{
printf("\nMENU");
printf("\nEnter 1 for making the list");
printf("\nEnter 2 for printing the list");
printf("\nEnter 3 for searching in the list");
printf("\nEnter 4 for deleting the element from the list");
printf("\nEnter 0 for exit");
printf("\nEnter ur option:");
scanf("%d",&o);
switch(o)
{
case 1:printf("\nEnter the max limit:");
scanf("%d",&n);
append(n);
break;
case 2:display();break;
case 3:search();break;
case 4:del();break;
case 0:exit(0);
default:printf("\nU have entered a wrong choice!!");
}
}
getche();
}

0 comments: