DOUBLE MENU DRIVEN LI.LIST

 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

  1. Create a DLL of N Employees Data by using end insertion.

  2. Display the status of DLL and count the number of nodes in it

  3. Perform Insertion and Deletion at End of DLL

  4. Perform Insertion and Deletion at Front of DLL

  5. Demonstrate how this DLL can be used as Double Ended Queue.

  6. 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:

  1. Create DLL by End Insertion

  2. Display DLL and Count Nodes

  3. Insert at End

  4. Delete from End

  5. Insert at Front

  6. Delete from Front

  7. Double Ended Queue Demonstration (DLL can be used as Deque)

  8. 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:

  1. End Insertion (insertEnd()): Inserts a new employee at the end of the list.

  2. Front Insertion (insertFront()): Inserts a new employee at the front of the list.

  3. Delete from End (deleteEnd()): Deletes an employee from the end of the list.

  4. Delete from Front (deleteFront()): Deletes an employee from the front of the list.

  5. Display List and Count Nodes (displayAndCount()): Displays all employees in the list and counts the number of nodes.

Double-Ended Queue (Deque) Operations:

  1. Insert at Front (Push): Works like a stack, adding employees to the front.

  2. Insert at End (Enqueue): Adds employees at the end, like a queue.

  3. Delete from Front (Pop): Removes employees from the front, like stack pop.

  4. Delete from End (Dequeue): Removes employees from the end, like queue dequeue

Output:

ceciot@cec-IOT:~$ cd Desktop/
ceciot@cec-IOT:~/Desktop$ gcc doublelilist.c -o abcd
ceciot@cec-IOT:~/Desktop$ ./abcd


--- Employee DLL Menu ---
1. Create DLL by End Insertion
2. Display and Count Nodes
3. Insert at End
4. Delete from End
5. Insert at Front
6. Delete from Front
7. Double Ended Queue Demo
8. Exit

Enter your choice: 1
Enter SSN: 1233
Enter Name: JODA
Enter Dept: IOT
Enter Designation: UI DESIGNER
Enter Salary: Enter Phone Number: Employee inserted at the end.


--- Employee DLL Menu ---
1. Create DLL by End Insertion
2. Display and Count Nodes
3. Insert at End
4. Delete from End
5. Insert at Front
6. Delete from Front
7. Double Ended Queue Demo
8. Exit

Enter your choice: 1
Enter SSN: 21354
Enter Name: RAJ
Enter Dept: IOT
Enter Designation: DEVELOPER
Enter Salary: 23000
Enter Phone Number: 9836352823
Employee inserted at the end.


--- Employee DLL Menu ---
1. Create DLL by End Insertion
2. Display and Count Nodes
3. Insert at End
4. Delete from End
5. Insert at Front
6. Delete from Front
7. Double Ended Queue Demo
8. Exit

Enter your choice: 1
Enter SSN: 2344
Enter Name: KUMAR
Enter Dept: IOT
Enter Designation: TESTER
Enter Salary: 30988
Enter Phone Number: 9472548392
Employee inserted at the end.


--- Employee DLL Menu ---
1. Create DLL by End Insertion
2. Display and Count Nodes
3. Insert at End
4. Delete from End
5. Insert at Front
6. Delete from Front
7. Double Ended Queue Demo
8. Exit

Enter your choice: 1
Enter SSN: 2504
Enter Name: JOSEPH
Enter Dept: IOT
Enter Designation: CLIENT SIDE
Enter Salary: Enter Phone Number: Employee inserted at the end.


--- Employee DLL Menu ---
1. Create DLL by End Insertion
2. Display and Count Nodes
3. Insert at End
4. Delete from End
5. Insert at Front
6. Delete from Front
7. Double Ended Queue Demo
8. Exit

Enter your choice: 2

--- Employee List ---
SSN: 1233 | Name: JODA | Dept: IOT | Designation: UI | Salary: 0.00 | Phone: DESIGNER
SSN: 21354 | Name: RAJ | Dept: IOT | Designation: DEVELOPER | Salary: 23000.00 | Phone: 9836352823
SSN: 2344 | Name: KUMAR | Dept: IOT | Designation: TESTER | Salary: 30988.00 | Phone: 9472548392
SSN: 2504 | Name: JOSEPH | Dept: IOT | Designation: CLIENT | Salary: 0.00 | Phone: SIDE
Total employees: 4


--- Employee DLL Menu ---
1. Create DLL by End Insertion
2. Display and Count Nodes
3. Insert at End
4. Delete from End
5. Insert at Front
6. Delete from Front
7. Double Ended Queue Demo
8. Exit

Enter your choice: 3
Enter SSN: 2356
Enter Name: JOSHI
Enter Dept: IOT
Enter Designation: TESTER
Enter Salary: 30000
Enter Phone Number: 8472538492
Employee inserted at the end.


--- Employee DLL Menu ---
1. Create DLL by End Insertion
2. Display and Count Nodes
3. Insert at End
4. Delete from End
5. Insert at Front
6. Delete from Front
7. Double Ended Queue Demo
8. Exit

Enter your choice: 2

--- Employee List ---
SSN: 1233 | Name: JODA | Dept: IOT | Designation: UI | Salary: 0.00 | Phone: DESIGNER
SSN: 21354 | Name: RAJ | Dept: IOT | Designation: DEVELOPER | Salary: 23000.00 | Phone: 9836352823
SSN: 2344 | Name: KUMAR | Dept: IOT | Designation: TESTER | Salary: 30988.00 | Phone: 9472548392
SSN: 2504 | Name: JOSEPH | Dept: IOT | Designation: CLIENT | Salary: 0.00 | Phone: SIDE
SSN: 2356 | Name: JOSHI | Dept: IOT | Designation: TESTER | Salary: 30000.00 | Phone: 8472538492
Total employees: 5


--- Employee DLL Menu ---
1. Create DLL by End Insertion
2. Display and Count Nodes
3. Insert at End
4. Delete from End
5. Insert at Front
6. Delete from Front
7. Double Ended Queue Demo
8. Exit

Enter your choice: 5
Enter SSN: 2345
Enter Name: JEE
Enter Dept: IOT
Enter Designation: CODER
Enter Salary: 42090
Enter Phone Number: 837549302
Employee inserted at the front.


--- Employee DLL Menu ---
1. Create DLL by End Insertion
2. Display and Count Nodes
3. Insert at End
4. Delete from End
5. Insert at Front
6. Delete from Front
7. Double Ended Queue Demo
8. Exit

Enter your choice: 2

--- Employee List ---
SSN: 2345 | Name: JEE | Dept: IOT | Designation: CODER | Salary: 42090.00 | Phone: 837549302
SSN: 1233 | Name: JODA | Dept: IOT | Designation: UI | Salary: 0.00 | Phone: DESIGNER
SSN: 21354 | Name: RAJ | Dept: IOT | Designation: DEVELOPER | Salary: 23000.00 | Phone: 9836352823
SSN: 2344 | Name: KUMAR | Dept: IOT | Designation: TESTER | Salary: 30988.00 | Phone: 9472548392
SSN: 2504 | Name: JOSEPH | Dept: IOT | Designation: CLIENT | Salary: 0.00 | Phone: SIDE
SSN: 2356 | Name: JOSHI | Dept: IOT | Designation: TESTER | Salary: 30000.00 | Phone: 8472538492
Total employees: 6

--- Employee DLL Menu ---
1. Create DLL by End Insertion
2. Display and Count Nodes
3. Insert at End
4. Delete from End

--- Employee DLL Menu ---
1. Create DLL by End Insertion
2. Display and Count Nodes
3. Insert at End
4. Delete from End
5. Insert at Front
6. Delete from Front
7. Double Ended Queue Demo
8. Exit
Enter your choice:

5. Insert at Front
6. Delete from Front
7. Double Ended Queue Demo
8. Exit

Enter your choice: 4
Deleted employee with SSN: 2356

--- Employee DLL Menu ---
1. Create DLL by End Insertion
2. Display and Count Nodes
3. Insert at End
4. Delete from End
5. Insert at Front
6. Delete from Front
7. Double Ended Queue Demo
8. Exit

Enter your choice: 6
Deleted employee with SSN: 2345

--- Employee DLL Menu ---
1. Create DLL by End Insertion
2. Display and Count Nodes
3. Insert at End
4. Delete from End
5. Insert at Front
6. Delete from Front
7. Double Ended Queue Demo
8. Exit

Enter your choice: 7

Double Ended Queue Operations:
1. Insert at Front (Push)
2. Insert at End (Enqueue)
3. Delete from Front (Pop)
4. Delete from End (Dequeue)
5. Exit

Enter your choice: 1
Enter SSN: 3456
Enter Name: GANZ
Enter Dept: IOT
Enter Designation: DEVELOPER
Enter Salary: 30000
Enter Phone Number: 246547683
Employee inserted at the front.


Double Ended Queue Operations:
1. Insert at Front (Push)
2. Insert at End (Enqueue)
3. Delete from Front (Pop)
4. Delete from End (Dequeue)
5. Exit

Enter your choice: 5

--- Employee DLL Menu ---
1. Create DLL by End Insertion
2. Display and Count Nodes
3. Insert at End
4. Delete from End
5. Insert at Front
6. Delete from Front
7. Double Ended Queue Demo
8. Exit

Enter your choice: 2

--- Employee List ---
SSN: 3456 | Name: GANZ | Dept: IOT | Designation: DEVELOPER | Salary: 30000.00 | Phone: 246547683
SSN: 1233 | Name: JODA | Dept: IOT | Designation: UI | Salary: 0.00 | Phone: DESIGNER
SSN: 21354 | Name: RAJ | Dept: IOT | Designation: DEVELOPER | Salary: 23000.00 | Phone: 9836352823
SSN: 2344 | Name: KUMAR | Dept: IOT | Designation: TESTER | Salary: 30988.00 | Phone: 9472548392
SSN: 2504 | Name: JOSEPH | Dept: IOT | Designation: CLIENT | Salary: 0.00 | Phone: SIDE
Total employees: 5


 

No comments:

Post a Comment