Lab program-8
8. Design, Develop and Implement 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
#include<stdio.h>
#include<stdlib.h>
int count=0;
struct node
{
struct node *prev, *next;
int ssn,phno;
float sal;
char name[20],dept[10],desg[20];
}*first,*last,*temp;
void create()
{
temp =(struct node *)malloc(sizeof(struct node));
temp->prev = NULL;
temp->next = NULL;
printf("\n Enter ssn,name,department, designation, salary and phno of employee : ");
scanf("%d %s %s %s %f %d", &temp->ssn, temp->name,temp->dept, temp->desg,
&temp->sal,&temp->phno);
count++;
}
void insertbeg()
{
if (first == NULL)
{
create();
first = temp;
last = first;
}
else
{
create();
temp->next = first;
first->prev = temp;
first = temp;
}
}
void insertend()
{
if(first==NULL)
{
create();
first = temp;
last = first;
}
else
{
create();
last->next = temp;
temp->prev = last;
last = temp;
}
}
void displaybeg()
{
temp =first;
if(temp == NULL)
{
printf("List empty to display \n");
return;
}
printf("\n Linked list elements from begining : \n");
while (temp!= NULL)
{
printf("%d %s %s %s %f %d\n", temp->ssn, temp->name,temp->dept,temp->desg,
temp->sal, temp->phno );
temp = temp->next;
}
printf(" No of employees = %d ", count);
}
int deleteend()
{
if(first==NULL)
{
printf("\nList is empty\n");
return;
}
temp=last;
if(temp->prev==NULL)
{
printf("\nDeleted node detail\n");
printf("%d %s %s %s %f %d", temp->ssn, temp->name,temp->dept, temp->desg,
temp->sal, temp->phno );
free(temp);
first=last=NULL;
return 0;
}
else
{
last=temp->prev;
last->next=NULL;
printf("\nDeleted node detail\n");
printf("%d %s %s %s %f %d\n", temp->ssn, temp->name,temp->dept, temp->desg,
temp->sal, temp->phno );
free(temp);
}
count--;
return 0;
}
int deletebeg()
{
if(first==NULL)
{
printf("\nList is empty\n");
return;
}
temp=first;
if(temp->next==NULL)
{
printf("\nDeleted node detail\n");
printf("%d %s %s %s %f %d", temp->ssn, temp->name,temp->dept, temp->desg,
temp->sal, temp->phno );
free(temp);
first=last=NULL;
}
else
{
first=first->next;
printf("\nDeleted node detail\n");
printf("%d %s %s %s %f %d", temp->ssn, temp->name,temp->dept, temp->desg,
temp->sal, temp->phno );
free(temp);
}
count--;
return 0;
}
void main()
{
int ch,n,i;
first=NULL;
temp = last = NULL;
printf("-----------------MENU--------------------\n");
printf("\n 1 - Create a DLL of n emp");
printf("\n 2 - Display from beginning");
printf("\n 3 - Insert at end");
printf("\n 4 - delete at end");
printf("\n 5 - Insert at beg");
printf("\n 6 - delete at beg");
printf("\n 7 - exit\n");
printf("------------------------------------------\n");
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("\n Enter no of employees : ");
scanf("%d", &n);
for(i=0;i<n;i++)
insertend();
break;
case 2:
displaybeg();
break;
case 3:
insertend();
break;
case 4:
deleteend();
break;
case 5:
insertbeg();
break;
case 6:
deletebeg();
break;
case 7:
exit(0);
default: printf("wrong choice\n");
}
}
}
#include<stdlib.h>
int count=0;
struct node
{
struct node *prev, *next;
int ssn,phno;
float sal;
char name[20],dept[10],desg[20];
}*first,*last,*temp;
void create()
{
temp =(struct node *)malloc(sizeof(struct node));
temp->prev = NULL;
temp->next = NULL;
printf("\n Enter ssn,name,department, designation, salary and phno of employee : ");
scanf("%d %s %s %s %f %d", &temp->ssn, temp->name,temp->dept, temp->desg,
&temp->sal,&temp->phno);
count++;
}
void insertbeg()
{
if (first == NULL)
{
create();
first = temp;
last = first;
}
else
{
create();
temp->next = first;
first->prev = temp;
first = temp;
}
}
void insertend()
{
if(first==NULL)
{
create();
first = temp;
last = first;
}
else
{
create();
last->next = temp;
temp->prev = last;
last = temp;
}
}
void displaybeg()
{
temp =first;
if(temp == NULL)
{
printf("List empty to display \n");
return;
}
printf("\n Linked list elements from begining : \n");
while (temp!= NULL)
{
printf("%d %s %s %s %f %d\n", temp->ssn, temp->name,temp->dept,temp->desg,
temp->sal, temp->phno );
temp = temp->next;
}
printf(" No of employees = %d ", count);
}
int deleteend()
{
if(first==NULL)
{
printf("\nList is empty\n");
return;
}
temp=last;
if(temp->prev==NULL)
{
printf("\nDeleted node detail\n");
printf("%d %s %s %s %f %d", temp->ssn, temp->name,temp->dept, temp->desg,
temp->sal, temp->phno );
free(temp);
first=last=NULL;
return 0;
}
else
{
last=temp->prev;
last->next=NULL;
printf("\nDeleted node detail\n");
printf("%d %s %s %s %f %d\n", temp->ssn, temp->name,temp->dept, temp->desg,
temp->sal, temp->phno );
free(temp);
}
count--;
return 0;
}
int deletebeg()
{
if(first==NULL)
{
printf("\nList is empty\n");
return;
}
temp=first;
if(temp->next==NULL)
{
printf("\nDeleted node detail\n");
printf("%d %s %s %s %f %d", temp->ssn, temp->name,temp->dept, temp->desg,
temp->sal, temp->phno );
free(temp);
first=last=NULL;
}
else
{
first=first->next;
printf("\nDeleted node detail\n");
printf("%d %s %s %s %f %d", temp->ssn, temp->name,temp->dept, temp->desg,
temp->sal, temp->phno );
free(temp);
}
count--;
return 0;
}
void main()
{
int ch,n,i;
first=NULL;
temp = last = NULL;
printf("-----------------MENU--------------------\n");
printf("\n 1 - Create a DLL of n emp");
printf("\n 2 - Display from beginning");
printf("\n 3 - Insert at end");
printf("\n 4 - delete at end");
printf("\n 5 - Insert at beg");
printf("\n 6 - delete at beg");
printf("\n 7 - exit\n");
printf("------------------------------------------\n");
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("\n Enter no of employees : ");
scanf("%d", &n);
for(i=0;i<n;i++)
insertend();
break;
case 2:
displaybeg();
break;
case 3:
insertend();
break;
case 4:
deleteend();
break;
case 5:
insertbeg();
break;
case 6:
deletebeg();
break;
case 7:
exit(0);
default: printf("wrong choice\n");
}
}
}