#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
int iSSN;
char cName[30],cDept[10],cDesignation[30],cPhNo[11];
int iSalary;
struct node *plink;
struct node *nlink;
};
typedef struct node *NODEPTR;
NODEPTR fnGetNode(void);
void fnFreeNode(NODEPTR);
NODEPTR fnInsRear(NODEPTR);
NODEPTR fnDelFront(NODEPTR);
NODEPTR fnInsFront(NODEPTR);
NODEPTR fnDelRear(NODEPTR);
void fnDisplay(NODEPTR);
int main()
{
NODEPTR first=NULL;
int iChoice,iNum,i;
printf("\n Enter the number of Employees N:");
scanf("%d",&iNum);
for(i=0;i<iNum;i++)
{
printf("\n Enter Data for node %d:\n",i+1);
first=fnInsRear(first);
}
for(;;)
{
printf("\n DLL OPERATIONS\n");
printf("=========");
printf("\n 1. Insert Rear \n 2. Delete Front\n 3. Insert Front \n 4. Delete Rear \n 5. Display \n 6. Exit \n");
printf("\n Enter your choice\n");
printf("\n Enter the number of Employees N:");
scanf("%d",&iNum);
for(i=0;i<iNum;i++)
{
printf("\n Enter Data for node %d:scanf("%d",&iChoice);
switch(iChoice)
{
case 1:
first=fnInsRear(first);
break;
case 2:
first=fnDelFront(first);
break;
case 3:
first=fnInsFront(first);
break;
case 4:
first=fnDelRear(first);
break;
case 5:
fnDisplay(first);
break;
case 6:
exit(0);
}
}
return 0;
}
NODEPTR fnGetNode()
{
NODEPTR newborn;
newborn=(NODEPTR)malloc(sizeof(struct node));
if(newborn==NULL)
{
printf("n Memory overflow");
exit(0);
}
printf("\n Enter SSN:");
scanf("%d",&newborn->iSSN);
printf("\n Enter name:");
scanf("%s",newborn->cName);
printf("\n enter department:");
scanf("%s",newborn->cDept);
printf("\n Enter designation:");
scanf("%s",newborn->cDesignation);
printf("\n Enter salary:");
scanf("%d",&newborn->iSalary);
printf("\n Enter Phone No:");
scanf("%s",newborn->cPhNo);
return newborn;
}
void fnFreeNode(NODEPTR x)
{
free(x);
}
NODEPTR fnInsRear(NODEPTR firprintf("\n Enter SSN:");
scanf("%d",&newborn->iSSN);
printf("\n Enter name:");
scanf("%s",newborn->cName);
printf("\n enter department:");
scanf("%s",newborn->cDept);
printf("\n Enter designation:");
scanf("%s",newborn->cDesignation);
printf("\n Enter salary:");
scanf("%d",&newborn->iSalary);
printf("\n Enter Phone No:");
scanf("%s",newborn->cPhNo);st)
{
NODEPTR temp,cur;
temp=fnGetNode();
temp->plink=temp->nlink=NULL;
if(first==NULL)
return temp;
cur=first;
while(cur->nlink!=NULL)
{
cur=cur->nlink;
}
cur->nlink=temp;
temp->plink=cur;
return first;
}
NODEPTR fnInsFront(NODEPTR first)
{
NODEPTR temp;
temp=fnGetNode();
temp->plink=temp->nlink=NULL;
temp->nlink=first;
first=temp;
return first;
}
NODEPTR fnDelRear(NODEPTR first)
{
NODEPTR cur,prev;
if(first==NULL)
{
printf("\nDLL is empty\n");
return first;
}
cur=first;
if(cur->nlink==NULL)
{
printf("\n Node deleted for %s\n",cur->cName);
fnFreeNode(cur);
return NULL;
}
while(cur->nlink!=NULL)
{
cur=cur->nlink;
}
prev=cur->plink;
prev->nlink=NULL;
printf("\n Node deleted for %s\n",cur->cName);
fnFreeNode(cur);
return first;
}
NODEPTR fnDelFront(NODEPTR first)
{
NODEPTR temp;
if(first==NULL)
{
printf("\n DLL is empty\n");
return first;
}
if(first->nlink==NULL)
{
printf("\n Node deleted for %s\n", first->cName);
fnFreeNode(first);
return NULL;
}
temp=first;
first=first->nlink;
first->plink=NULL;
printf("\n Node deleted for %s\n",temp->cName);
fnFreeNode(temp);
return first;
}
void fnDisplay(NODEPTR first)
{
NODEPTR curr;
int count=0;
if(first==NULL)
{
printf("\n DLL is empty\n");
return;
}
printf("\n The contents of DLL are:\n");
currprintf("\n Enter the number of Employees N:");
scanf("%d",&iNum);
for(i=0;i<iNum;i++)
{
printf("\n Enter Data for node %d:=first;
printf("\n");
printf("\n SSN \t Name\tDept\tDesignation\tSalary\tPhone No");
while(curr!=NULL)
{
printf("\n%-5d\t%s\t%s\t%s\t%-7d\t%-11s",curr->iSSN,curr->cName,curr->cDept,curr->cDesignation,curr->iSalary,curr->cPhNo);
curr=curr->nlink;
count++;
}
printf("\n\nDLL has %d nodes\n",count);
}