Employee Data Using Doubly Linked List
Employee Data Using Doubly Linked List
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
#include<stdio.h>
#include<stdlib.h>
struct Employee
{
int SSN;
char Name[20];
char Dept[10];
char Designation[20];
float Sal;
long int PhNo;
struct Employee *prev;
struct Employee *next;
};
typedef struct Employee NODE;
NODE *head=NULL,*newEmp,*temp,*prvnode,*curr;
int count=0;
void create_node()
{
int ele;
newEmp=(NODE *)malloc(sizeof(NODE));
if (newEmp == NULL)
{
printf("Memory allocation failed!\n");
exit(0);
}
printf("Enter SSN: ");
scanf("%d", &newEmp->SSN);
printf("Enter Name: ");
scanf("%s", newEmp->Name);
printf("Enter Department: ");
scanf("%s", newEmp->Dept);
printf("Enter Designation: ");
scanf("%s", newEmp->Designation);
printf("Enter Salary: ");
scanf("%f", &newEmp->Sal);
printf("Enter Phone Number: ");
scanf("%ld", &newEmp->PhNo);
newEmp->prev = NULL;
newEmp->next = NULL;
}
void displayCount()
{
if(head==NULL)
{
printf("Doubly Linked List is Empty\n");
}
else
{
temp=head;
printf("\nEmployee Data:\n");
printf("SSN\tName\tDept\tDesignation\tSal\tPhNo\n");
while(temp!=NULL)
{
printf("%d\t%s\t%s\t%s\t%.2f\t%ld\n", temp->SSN, temp->Name, temp-
>Dept, temp->Designation, temp->Sal, temp->PhNo);
temp=temp->next;
}
}
printf("\nNumber of employees: %d\n", count);
}
void insert_front()
{
create_node();
if(head==NULL)
{
head=newEmp;
}
else
{
newEmp->next = head;
head->prev=newEmp;
head=newEmp;
}
count++;
displayCount();
}
void insert_end()
{
create_node();
if(head==NULL)
{
head=newEmp;
}
else
{
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newEmp;
newEmp->prev=temp;
}
count++;
displayCount();
}
void delete_front()
{
if(head == NULL)
{
printf("\nDoubly Linked List is EMPTY\n");
}
else if(head->next == NULL)
{
printf("\nDeleted SSN %d:\n ",head->SSN);
free(head);
head = NULL;
}
else
{
temp = head;
printf("\nDeleted SSN Node %d: \n",temp->SSN);
head = head -> next;
head -> prev = NULL;
free(temp);
}
count--;
displayCount();
}
void delete_end()
{
if(head == NULL)
{
printf("\nDoubly Linked List is EMPTY\n");
}
else if(head->next == NULL)
{
printf("\nDeleted SSN Node %d: \n",head->SSN);
head = NULL;
free(head);
}
else
{
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
printf("\nDeleted SSN Node %d: \n",temp->SSN);
temp->prev->next=NULL;
free(temp);
}
count--;
displayCount();
}
int main()
{
int ch,n,choice,i;
do
{
printf("\nDoubly Linked List Operations\n");
printf("******************************\n");
printf("1. Create DLL of N Employees\n");
printf("2. Display DLL and Count of Nodes\n");
printf("3. Insert at End\n");
printf("4. Insert at Front\n");
printf("5. Delete from End\n");
printf("6. Delete from Front\n");
printf("7. Demonstrate DLL as Double Ended Queue\n");
printf("8. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter the number of employees: ");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
insert_end();
}
break;
case 2:
displayCount();
break;
case 3:
insert_end();
break;
case 4:
insert_front();
break;
case 5:
delete_end();
break;
case 6:
delete_front();
break;
case 7:
doubleEndedQueueDemo();
break;
case 8:
printf("Exiting program.\n");
break;
default:
printf("Enter Valid Choice\n");
}
}while (choice != 8);
return 0;
}