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>
#include <string.h>
struct Employee {
char ssn[20], name[30], dept[20], desg[20], phno[15];
float sal;
struct Employee *prev, *next;
};
typedef struct Employee* NODE;
NODE head = NULL;
// Create new node
NODE createNode() {
NODE temp = (NODE)malloc(sizeof(struct Employee));
printf("Enter SSN, Name, Dept, Designation, Salary, Phone:\n");
scanf("%s %s %s %s %f %s", temp->ssn, temp->name, temp->dept, temp->desg, &temp->sal, temp->phno);
temp->prev = temp->next = NULL;
return temp;
}
// a. Insert at end
void insertEnd() {
NODE newNode = createNode();
if (!head) head = newNode;
else {
NODE temp = head;
while (temp->next) temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
}
printf("Inserted at end.\n");
}
// d. Insert at front
void insertFront() {
NODE newNode = createNode();
if (!head) head = newNode;
else {
newNode->next = head;
head->prev = newNode;
head = newNode;
}
printf("Inserted at front.\n");
}
// c. Delete from end
void deleteEnd() {
if (!head) {
printf("List is empty.\n");
return;
}
if (!head->next) {
free(head);
head = NULL;
} else {
NODE temp = head;
while (temp->next) temp = temp->next;
temp->prev->next = NULL;
free(temp);
}
printf("Deleted from end.\n");
}
// d. Delete from front
void deleteFront() {
if (!head) {
printf("List is empty.\n");
return;
}
NODE temp = head;
head = head->next;
if (head) head->prev = NULL;
free(temp);
printf("Deleted from front.\n");
}
// b. Display and count
void display() {
if (!head) {
printf("List is empty.\n");
return;
}
NODE temp = head;
int count = 0;
printf("\nEmployee List:\n");
printf("SSN\tName\tDept\tDesg\tSal\tPhone\n");
while (temp) {
printf("%s\t%s\t%s\t%s\t%.2f\t%s\n", temp->ssn, temp->name, temp->dept, temp->desg, temp->sal, temp->phno);
count++;
temp = temp->next;
}
printf("Total employees: %d\n", count);
}
// e. Demonstrate Double-Ended Queue
void dequeueDemo() {
printf("\nDouble Ended Queue Operations:\n");
printf("1. Insert Front\n2. Insert End\n3. Delete Front\n4. Delete End\n5. Display\n6. Back to Main Menu\n");
int ch;
do {
printf("Enter choice: ");
scanf("%d", &ch);
switch (ch) {
case 1: insertFront(); break;
case 2: insertEnd(); break;
case 3: deleteFront(); break;
case 4: deleteEnd(); break;
case 5: display(); break;
case 6: return;
default: printf("Invalid!\n");
}
} while (ch != 6);
}
int main() {
int choice;
do {
printf("\n--- MENU ---\n");
printf("1. Create DLL (Insert End)\n2. Display and Count\n");
printf("3. Insert Front\n4. Insert End\n");
printf("5. Delete Front\n6. Delete End\n");
printf("7. Double-Ended Queue Demo\n8. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: insertEnd(); break;
case 2: display(); break;
case 3: insertFront(); break;
case 4: insertEnd(); break;
case 5: deleteFront(); break;
case 6: deleteEnd(); break;
case 7: dequeueDemo(); break;
case 8: printf("Exiting...\n"); break;
default: printf("Invalid choice!\n");
}
} while (choice != 8);
return 0;
}
--- MENU ---
1. Create DLL (Insert End)
2. Display and Count
3. Insert Front
4. Insert End
5. Delete Front
6. Delete End
7. Double-Ended Queue Demo
8. Exit
Enter choice: 1
Enter SSN, Name, Dept, Designation, Salary, Phone:
1001
Abhishek
CSE
Developer
58385
0123456789
Inserted at end.
--- MENU ---
1. Create DLL (Insert End)
2. Display and Count
3. Insert Front
4. Insert End
5. Delete Front
6. Delete End
7. Double-Ended Queue Demo
8. Exit
Enter choice: 1
Enter SSN, Name, Dept, Designation, Salary, Phone:
1002
Virat
CSE
Engineer
50000
1234567890
Inserted at end.
--- MENU ---
1. Create DLL (Insert End)
2. Display and Count
3. Insert Front
4. Insert End
5. Delete Front
6. Delete End
7. Double-Ended Queue Demo
8. Exit
Enter choice: 2
Employee List:
SSN Name Dept Desg Sal Phone
1001 Abhishek CSE Developer 58385.00 0123456789
1002 Virat CSE Engineer 50000.00 1234567890
Total employees: 2
--- MENU ---
1. Create DLL (Insert End)
2. Display and Count
3. Insert Front
4. Insert End
5. Delete Front
6. Delete End
7. Double-Ended Queue Demo
8. Exit
Enter choice: 3
Enter SSN, Name, Dept, Designation, Salary, Phone:
1000
Anushka
CSE
Manager
70000
2134567890
Inserted at front.
--- MENU ---
1. Create DLL (Insert End)
2. Display and Count
3. Insert Front
4. Insert End
5. Delete Front
6. Delete End
7. Double-Ended Queue Demo
8. Exit
Enter choice: 5
Deleted from front.
--- MENU ---
1. Create DLL (Insert End)
2. Display and Count
3. Insert Front
4. Insert End
5. Delete Front
6. Delete End
7. Double-Ended Queue Demo
8. Exit
Enter choice: 6
Deleted from end.
--- MENU ---
1. Create DLL (Insert End)
2. Display and Count
3. Insert Front
4. Insert End
5. Delete Front
6. Delete End
7. Double-Ended Queue Demo
8. Exit
Enter choice: 7
Double Ended Queue Operations:
1. Insert Front
2. Insert End
3. Delete Front
4. Delete End
5. Display
6. Back to Main Menu
Enter choice: 1
Enter SSN, Name, Dept, Designation, Salary, Phone:
2001
Samrat
ME
Analyts
51000
1234567890
Inserted at front.
Enter choice: 5
Employee List:
SSN Name Dept Desg Sal Phone
2001 Samrat ME Analyts 51000.00 1234567890
1001 Abhishek CSE Developer 58385.00 0123456789
Total employees: 2
Enter choice: 6
--- MENU ---
1. Create DLL (Insert End)
2. Display and Count
3. Insert Front
4. Insert End
5. Delete Front
6. Delete End
7. Double-Ended Queue Demo
8. Exit
Enter choice:
8
Exiting...
Replay !
Share Your Thoughts