0% found this document useful (0 votes)
8 views8 pages

Employee Management with Doubly Linked List

The document contains a C program that implements a doubly linked list to manage employee records. It includes functions for inserting and deleting nodes at both ends of the list, displaying the list, and freeing the memory. The program allows user interaction through a menu-driven interface to perform these operations.

Uploaded by

shivanshucbhatt
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views8 pages

Employee Management with Doubly Linked List

The document contains a C program that implements a doubly linked list to manage employee records. It includes functions for inserting and deleting nodes at both ends of the list, displaying the list, and freeing the memory. The program allows user interaction through a menu-driven interface to perform these operations.

Uploaded by

shivanshucbhatt
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include <stdio.

h>

#include <stdlib.h>

#include <string.h>

// Employee structure

typedef struct {

int ssn;

char name[50], dept[30], desg[30], ph[15];

float sal;

} EMPL;

// Node of Doubly Linked List

struct node {

int ssn;

char name[50], dept[30], desg[30], ph[15];

float sal;

struct node *llink;

struct node *rlink;

};

typedef struct node* NODE;

// Function to create a new node

NODE getnode() {

NODE x = (NODE)malloc(sizeof(struct node));

if (x == NULL) {

printf("Out of memory\n");

exit(0);

}
return x;

// Insert at front

NODE insert_front(EMPL item, NODE first) {

NODE temp = getnode();

temp->ssn = [Link];

strcpy(temp->name, [Link]);

strcpy(temp->dept, [Link]);

strcpy(temp->desg, [Link]);

strcpy(temp->ph, [Link]);

temp->sal = [Link];

temp->llink = NULL;

temp->rlink = first;

if (first != NULL)

first->llink = temp;

return temp;

// Insert at rear

NODE insert_rear(EMPL item, NODE first) {

NODE temp = getnode();

temp->ssn = [Link];

strcpy(temp->name, [Link]);

strcpy(temp->dept, [Link]);

strcpy(temp->desg, [Link]);

strcpy(temp->ph, [Link]);
temp->sal = [Link];

temp->llink = temp->rlink = NULL;

if (first == NULL)

return temp;

NODE cur = first;

while (cur->rlink != NULL)

cur = cur->rlink;

cur->rlink = temp;

temp->llink = cur;

return first;

// Delete from front

NODE delete_front(NODE first) {

if (first == NULL) {

printf("Employee list is empty\n");

return NULL;

NODE second = first->rlink;

printf("Deleted Employee: SSN=%d, Name=%s\n", first->ssn, first->name);

free(first);

if (second != NULL)

second->llink = NULL;
return second;

// Delete from rear

NODE delete_rear(NODE first) {

if (first == NULL) {

printf("Employee list is empty\n");

return NULL;

if (first->rlink == NULL) {

printf("Deleted Employee: SSN=%d, Name=%s\n", first->ssn, first->name);

free(first);

return NULL;

NODE cur = first;

while (cur->rlink != NULL)

cur = cur->rlink;

printf("Deleted Employee: SSN=%d, Name=%s\n", cur->ssn, cur->name);

cur->llink->rlink = NULL;

free(cur);

return first;

// Display all nodes


void display(NODE first) {

if (first == NULL) {

printf("Employee list is empty\n");

return;

NODE cur = first;

int count = 0;

printf("\n--- Employee List ---\n");

while (cur != NULL) {

printf("SSN: %d\nName: %s\nDepartment: %s\nDesignation: %s\nSalary: %.2f\nPhone:


%s\n\n",

cur->ssn, cur->name, cur->dept, cur->desg, cur->sal, cur->ph);

cur = cur->rlink;

count++;

printf("Total Employees: %d\n", count);

// Free the entire list before exit

void free_list(NODE first) {

NODE cur = first;

while (cur != NULL) {

NODE temp = cur;

cur = cur->rlink;

free(temp);

}
int main() {

NODE first = NULL;

EMPL item;

int choice;

for (;;) {

printf("\n--- Menu ---\n");

printf("1. Insert at Front\n");

printf("2. Insert at Rear\n");

printf("3. Delete from Front\n");

printf("4. Delete from Rear\n");

printf("5. Display\n");

printf("6. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

getchar(); // consume newline left by scanf

switch (choice) {

case 1:

case 2:

printf("Enter Employee Details:\n");

printf("SSN: ");

scanf("%d", &[Link]);

getchar(); // consume newline

printf("Name: ");

fgets([Link], sizeof([Link]), stdin);

[Link][strcspn([Link], "\n")] = '\0'; // remove newline


printf("Department: ");

fgets([Link], sizeof([Link]), stdin);

[Link][strcspn([Link], "\n")] = '\0';

printf("Designation: ");

fgets([Link], sizeof([Link]), stdin);

[Link][strcspn([Link], "\n")] = '\0';

printf("Salary: ");

scanf("%f", &[Link]);

getchar();

printf("Phone: ");

fgets([Link], sizeof([Link]), stdin);

[Link][strcspn([Link], "\n")] = '\0';

if (choice == 1)

first = insert_front(item, first);

else

first = insert_rear(item, first);

break;

case 3:

first = delete_front(first);

break;

case 4:

first = delete_rear(first);

break;

case 5:
display(first);

break;

case 6:

free_list(first);

printf("Exiting program...\n");

exit(0);

default:

printf("Invalid choice! Please enter 1-6.\n");

return 0;

You might also like