QUESTION
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
Create a DLL of N Employees Data by using end insertion.
Display the status of DLL and count the number of nodes in it
Perform Insertion and Deletion at End of DLL
Perform Insertion and Deletion at Front of DLL
Demonstrate how this DLL can be used as Double Ended Queue.
Exit
Here's a menu-driven C program that performs operations on a Doubly Linked List (DLL) for storing employee data.
Employee Data Fields:
Each node will contain the following fields:
-
SSN
(string) -
Name
(string) -
Dept
(string) -
Designation
(string) -
Sal
(float) -
PhNo
(string)
Supported Operations:
-
Create DLL by End Insertion
-
Display DLL and Count Nodes
-
Insert at End
-
Delete from End
-
Insert at Front
-
Delete from Front
-
Double Ended Queue Demonstration (DLL can be used as Deque)
-
Exit
Complete C Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Structure to hold employee data
struct Employee {
char ssn[20];
char name[50];
char dept[30];
char designation[30];
float sal;
char phno[15];
struct Employee* prev;
struct Employee* next;
};
typedef struct Employee* NODE;
NODE head = NULL;
// Function to create a new node with employee data
NODE createNode() {
NODE newNode = (NODE)malloc(sizeof(struct Employee));
if (newNode == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
printf("Enter SSN: ");
scanf("%s", newNode->ssn);
printf("Enter Name: ");
scanf("%s", newNode->name);
printf("Enter Dept: ");
scanf("%s", newNode->dept);
printf("Enter Designation: ");
scanf("%s", newNode->designation);
printf("Enter Salary: ");
scanf("%f", &newNode->sal);
printf("Enter Phone Number: ");
scanf("%s", newNode->phno);
newNode->prev = newNode->next = NULL;
return newNode;
}
// End insertion
void insertEnd() {
NODE newNode = createNode();
if (head == NULL) {
head = newNode;
} else {
NODE temp = head;
while (temp->next != NULL)
temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
}
printf("Employee inserted at the end.\n");
}
// Insert at front
void insertFront() {
NODE newNode = createNode();
if (head == NULL) {
head = newNode;
} else {
newNode->next = head;
head->prev = newNode;
head = newNode;
}
printf("Employee inserted at the front.\n");
}
// Delete from end
void deleteEnd() {
if (head == NULL) {
printf("List is empty.\n");
return;
}
if (head->next == NULL) {
printf("Deleted employee with SSN: %s\n", head->ssn);
free(head);
head = NULL;
return;
}
NODE temp = head;
while (temp->next != NULL)
temp = temp->next;
printf("Deleted employee with SSN: %s\n", temp->ssn);
temp->prev->next = NULL;
free(temp);
}
// Delete from front
void deleteFront() {
if (head == NULL) {
printf("List is empty.\n");
return;
}
NODE temp = head;
if (head->next == NULL) {
printf("Deleted employee with SSN: %s\n", head->ssn);
free(head);
head = NULL;
return;
}
head = head->next;
head->prev = NULL;
printf("Deleted employee with SSN: %s\n", temp->ssn);
free(temp);
}
// Display DLL and count nodes
void displayAndCount() {
if (head == NULL) {
printf("List is empty.\n");
return;
}
int count = 0;
NODE temp = head;
printf("\n--- Employee List ---\n");
while (temp != NULL) {
printf("SSN: %s | Name: %s | Dept: %s | Designation: %s | Salary: %.2f | Phone: %s\n",
temp->ssn, temp->name, temp->dept, temp->designation, temp->sal, temp->phno);
temp = temp->next;
count++;
}
printf("Total employees: %d\n", count);
}
// Double Ended Queue demonstration
void doubleEndedQueueDemo() {
int choice;
while (1) {
printf("\nDouble Ended Queue Operations:\n");
printf("1. Insert at Front (Push)\n");
printf("2. Insert at End (Enqueue)\n");
printf("3. Delete from Front (Pop)\n");
printf("4. Delete from End (Dequeue)\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
insertFront();
break;
case 2:
insertEnd();
break;
case 3:
deleteFront();
break;
case 4:
deleteEnd();
break;
case 5:
return;
default:
printf("Invalid choice. Try again.\n");
}
}
}
// Main function with menu
int main() {
int choice;
while (1) {
printf("\n--- Employee DLL Menu ---\n");
printf("1. Create DLL by End Insertion\n");
printf("2. Display and Count Nodes\n");
printf("3. Insert at End\n");
printf("4. Delete from End\n");
printf("5. Insert at Front\n");
printf("6. Delete from Front\n");
printf("7. Double Ended Queue Demo\n");
printf("8. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
insertEnd();
break;
case 2:
displayAndCount();
break;
case 3:
insertEnd();
break;
case 4:
deleteEnd();
break;
case 5:
insertFront();
break;
case 6:
deleteFront();
break;
case 7:
doubleEndedQueueDemo();
break;
case 8:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice. Try again.\n");
}
}
return 0;
}
How the Program Works:
Doubly Linked List Operations:
-
End Insertion (
insertEnd()
): Inserts a new employee at the end of the list. -
Front Insertion (
insertFront()
): Inserts a new employee at the front of the list. -
Delete from End (
deleteEnd()
): Deletes an employee from the end of the list. -
Delete from Front (
deleteFront()
): Deletes an employee from the front of the list. -
Display List and Count Nodes (
displayAndCount()
): Displays all employees in the list and counts the number of nodes.
Double-Ended Queue (Deque) Operations:
-
Insert at Front (Push): Works like a stack, adding employees to the front.
-
Insert at End (Enqueue): Adds employees at the end, like a queue.
-
Delete from Front (Pop): Removes employees from the front, like stack pop.
-
Delete from End (Dequeue): Removes employees from the end, like queue dequeue
No comments:
Post a Comment