Lab PGM 8
Lab PGM 8
Develop a menu driven Program in C for the following operations on Doubly Linked List
(DLL) of Employee Data with the fields: SSN, Name, Dept, Designation, Sal, PhNo.
a. Create a DLL of N Employees Data by using end insertion.
b. Display the status of DLL and count the number of nodes in it
c. Perform Insertion and Deletion at End of DLL
d. Perform Insertion and Deletion at Front of DLL
e. Demonstrate how this DLL can be used as Double Ended Queue
f. Exit
PROGRAM CODE:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
char SSN[10],Name[10],Dept[10],Designation[10];
int sal,phno;
struct node *llink;
struct node *rlink;
};
typedef struct node *NODE;
NODE first=NULL;
int count=0;
NODE CreateEmployeeNode()
{
char SSN[10],Name[10],Dept[10],Designation[10];
int sal,phno;
NODE temp;
printf("\nEnter SSN,Name,Dept,Designation,Sal,Phno of the
employee: ");
scanf("%s%s%s%s%d%d",SSN,Name,Dept,Designation,&sal,&phno);
temp=(NODE)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\nInsufficient Memory");
exit(0);
}
temp->llink=temp->rlink=NULL;
strcpy(temp->SSN,SSN);
strcpy(temp->Name,Name);
strcpy(temp->Dept,Dept);
strcpy(temp->Designation,Designation);
temp->sal=sal;
temp->phno=phno;
count++;
return temp;
}
NODE insert_front()
{
NODE temp;
temp=CreateEmployeeNode();
if(first==NULL)
return temp;
temp->rlink=first;
first->llink=temp;
return temp;
}
NODE delete_front()
{
NODE temp;
if(first==NULL)
{
printf("\nDLL is empty");
return NULL;
}
if(first->rlink==NULL)
{
printf("\nThe employee node with SSN = %s is
deleted",first->SSN);
free(first);
count--;
return NULL;
}
temp=first->rlink;
printf("\nThe employee node with SSN =%s id deleted",first-
>SSN);
free(first);
count--;
return temp;
}
NODE insert_rear()
{
NODE cur,temp;
temp=CreateEmployeeNode();
if(first==NULL)
return temp;
cur=first;
while(cur->rlink!=NULL)
cur=cur->rlink;
cur->rlink=temp;
temp->llink=cur;
return first;
}
NODE delete_rear()
{
NODE prev,cur;
if(first==NULL)
{
printf("\nDLL is empty");
return NULL;
}
if(first->rlink==NULL)
{
printf("\nThe employee node with SSN =%s id
deleted",first->SSN);
free(first);
count--;
return NULL;
}
prev=NULL;
cur=first;
while(cur->rlink!=NULL)
{
prev=cur;
cur=cur->rlink;
}
prev->rlink=NULL;
printf("\nThe employee node with SSN =%s id deleted",cur-
>SSN);
free(cur);
return first;
}
void display()
{
NODE cur;
int no=0;
cur=first;
if(first==NULL)
printf("\nDLL is empty");\
else
{
printf("\nNode\tSSN\tName\tDept\tDesign.\tSal\tPhoneno\n");
while(cur!=NULL)
{
printf("%d\t%s\t%s\t%s\t%s\t%d\t%d\n",++no,cur->SSN,cur-
>Name,cur->Dept,cur->Designation,cur->sal,cur->phno);
cur=cur->rlink;
}
}
printf("\nNo of employee node is %d",no);
}
void DoubleEndedQueueDemo()
{
int ch;
do
{
printf("\nDouble Ended Queue Operation");
printf("\n1:Insert at front\n2:Delete at front\n3:Insert
at rear\n4:Delete at rear\n5:Display\n6:Return");
printf("\nEnter the choice :");
scanf("%d",&ch);
switch(ch)
{
case 1:first=insert_front();
break;
case 2:first=delete_front();
break;
case 3:first=insert_rear();
break;
case 4:first=delete_rear();
break;
case 5:display();
break;
default:return;
}
}while(ch!=5);
}
int main()
{
int ch,i,n;
do
{
printf("\n\n1:Create DLL of employee nodes\n");
printf("2:Insert at front\n3:Delete at front\n4:Insert at
rear\n5:Delete at rear\n6:Display\n");
printf("7:Double Ended Queue Demo\n8:Exit");
printf("\nEnter the choice :");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\nEnter the no of Employee nodes: ");
scanf("%d",&n);
for(i=0;i<n;i++)
first=insert_front();
break;
case 2:first=insert_front();
break;
case 3:first=delete_front();
break;
case 4:first=insert_rear();
break;
case 5:first=delete_rear();
break;
case 6:display();
break;
case 7:DoubleEndedQueueDemo();
break;
case 8:exit(0);
default:printf("\nInvalid Cholce");
}
}while(ch!=8);
}