Program 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
for macro back slash (\) is considered as macro not completed
#include<stdio.h>
#include<stdlib.h>
# define print(t) \
printf("%s\t%s\t%s\t%s\t%f\t%ld\n",t->SSN,t->name,t->dept,t->desig,t->sal,t->ph);
# define read(t) \
printf("Enter SSN, Name, Dept, Desig, Salary, Phone of the Employee:\n"); \
scanf("%s%s%s%s%f%ld",t->SSN,t->name,t->dept, t->desig,&(t->sal),&(t->ph));
# define check_empty(t) \
if (f==NULL) \
{ printf("DLL is empty\n"); return NULL; }
# define check_empty1(t) \
if (f==NULL) \
{ printf("DLL is empty\n"); return; }
struct node
{
char SSN[10], name[20], dept[30],desig[30];
float sal;
long int ph;
struct node *llink, *rlink;
};
typedef struct node nd;
nd* create(nd *);
1
void status(nd *);
nd* ins_front(nd *);
nd* ins_rear(nd *);
nd* del_front(nd *);
nd* del_rear(nd *);
void display(nd *);
int main()
{
nd * first = NULL;
int ch;
for(;;)
{
printf("1. Create N students\n2. Status of DLL\n");
printf("3. Insert front\n4. Insert rear\n5. Delete
front\n");
printf("6. Delete rear\n7. Display\n8. Exit\nChoice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: first = create(first);break;
case 2: status(first); break;
case 3: first = ins_front(first); break;
case 4: first = ins_rear(first); break;
case 5: first = del_front(first); break;
case 6: first = del_rear(first); break;
case 7: display(first); break;
case 8: exit(0);
}
}
}
2
nd * del_front(nd *f)
{
nd *t;
check_empty(t) f
printf("Information to be deleted is...\n");
print(f);
t = f->rlink;
if (t)
t->llink = NULL;
free(f);
return t;
}
nd * del_rear(nd *f)
{
nd *t,*p;
check_empty(f);
for(p=NULL,t=f;t->rlink!=NULL;p=t,t=t->rlink);
printf("Information to be deleted is...\n");
print(t);
free(t);
if (p!=NULL)
{
p->rlink=NULL;
return f;
}
return NULL;
}
3
nd * ins_rear(nd * f)
{
nd *p=f;
nd *t=(nd*)malloc(sizeof(nd));
t->rlink=t->llink=NULL;
read(t);
if (f==NULL)
return t;
for(;p->rlink!=NULL; p=p->rlink);
p->rlink=t;
t->llink=p;
return f;
}
void status(nd *f)
{
int cnt=0;
check_empty1(f);
for(;f!=NULL;f=f->rlink,cnt++);
printf("Number of nodes in SLL is %d\n",cnt);
}
nd* create(nd *f)
{
int n,i;
printf("Enter value for n\n"); scanf("%d",&n);
for(i=0;i<n;i++)
f = ins_rear(f);
return f;
}
4
nd* ins_front(nd *f)
{
nd *t=(nd*)malloc(sizeof(nd));
t->rlink = t->llink = NULL;
read(t);
t->rlink = f;
if (f!=NULL)
f->llink=t;
return t;
}
void display(nd *f)
{
check_empty1(f);
printf("Contents of the list from FIRST -> LAST\n");
while(f->rlink!=NULL)
{
print(f);
f = f->rlink;
}
print(f);
printf("Contents of the list from LAST -> FIRST \n");
while(f != NULL)
{
print(f);
f = f->llink;
}
}