📜  在C中使用双链表的员工管理系统(1)

📅  最后修改于: 2023-12-03 14:51:15.525000             🧑  作者: Mango

在C中使用双链表的员工管理系统

简介

本员工管理系统基于双链表数据结构实现,使用C语言编写。该系统可以执行添加员工、删除员工、修改员工信息和查询员工等操作,可以帮助管理者更好地管理员工信息。

双链表数据结构及其优劣

双链表是一种数据结构,由一个指向前驱元素和后继元素的指针组成,具有以下优势:

  • 双链表可以在O(1)时间内插入或删除节点,而单链表则需要O(n)时间。
  • 双链表可以同时进行正向和反向访问,而单链表只能进行正向访问。

但双链表也有一些劣势:

  • 双链表的每个节点需要存储两个指针,所以需要更多的内存。
  • 双链表提供了更多的灵活性,使得编写和维护代码更加复杂。
使用方法
安装

本员工管理系统不需要安装,用户只需要将源代码文件下载下来,通过编译器进行编译后运行即可。

使用

该系统功能较为完善,可以满足大多数的员工信息管理需求。用户可以根据提示输入相应的命令,即可进行相关操作。

添加员工

用户可以通过该系统添加新的员工信息。添加时需要输入员工的姓名、工号、职位以及工资等信息。

void addEmployee()
{
    char name[20];
    int empNumber;
    char job[20];
    float salary;
    
    printf("Enter employee name: ");
    scanf("%s", name);
    printf("Enter employee number: ");
    scanf("%d", &empNumber);
    printf("Enter employee job: ");
    scanf("%s", job);
    printf("Enter employee salary: ");
    scanf("%f", &salary);
    
    Employee* newEmployee = (Employee*)malloc(sizeof(Employee));
    newEmployee->empNumber = empNumber;
    newEmployee->salary = salary;
    strcpy(newEmployee->name, name);
    strcpy(newEmployee->job, job);
    
    if (isEmpty())
    {
        head = tail = newEmployee;
    }
    else
    {
        newEmployee->prev = tail;
        tail->next = newEmployee;
        tail = newEmployee;
    }
    
    printf("Employee added successfully!\n");
    printMenu();
}

查询员工

该系统可以根据员工的工号或姓名进行查询操作。查询时,用户需要输入相应的关键字。

void searchEmployee()
{
    int empNumber;
    char name[20];
    
    printf("Enter 1 to search by employee number or 2 to search by employee name: ");
    int option;
    scanf("%d", &option);
    
    if (option == 1)
    {
        printf("Enter employee number: ");
        scanf("%d", &empNumber);
        
        Employee* foundEmployee = findEmployeeByNumber(empNumber);
        if (foundEmployee != NULL)
        {
            printf("Employee found!\n");
            printEmployee(foundEmployee);
        }
        else
        {
            printf("Employee not found!\n");
        }
    }
    else if (option == 2)
    {
        printf("Enter employee name: ");
        scanf("%s", name);
        
        Employee* foundEmployee = findEmployeeByName(name);
        if (foundEmployee != NULL)
        {
            printf("Employee found!\n");
            printEmployee(foundEmployee);
        }
        else
        {
            printf("Employee not found!\n");
        }
    }
    else
    {
        printf("Invalid command!\n");
    }
    
    printMenu();
}

修改员工信息

该系统可以修改员工的职位和工资信息。

void modifyEmployee()
{
    int empNumber;
    
    printf("Enter employee number: ");
    scanf("%d", &empNumber);
    
    Employee* foundEmployee = findEmployeeByNumber(empNumber);
    if (foundEmployee != NULL)
    {
        printf("Enter new job: ");
        scanf("%s", foundEmployee->job);
        printf("Enter new salary: ");
        scanf("%f", &(foundEmployee->salary));
        
        printf("Employee information updated successfully!\n");
    }
    else
    {
        printf("Employee not found!\n");
    }
    
    printMenu();
}

删除员工

该系统可以根据员工的工号进行删除操作。

void removeEmployee()
{
    int empNumber;
    
    printf("Enter employee number: ");
    scanf("%d", &empNumber);
    
    Employee* foundEmployee = findEmployeeByNumber(empNumber);
    if (foundEmployee != NULL)
    {
        if (foundEmployee == head)
        {
            if (head->next == NULL)
            {
                head = tail = NULL;
            }
            else
            {
                head = head->next;
                head->prev = NULL;
            }
        }
        else if (foundEmployee == tail)
        {
            tail = tail->prev;
            tail->next = NULL;
        }
        else
        {
            foundEmployee->prev->next = foundEmployee->next;
            foundEmployee->next->prev = foundEmployee->prev;
        }
        
        free(foundEmployee);
        printf("Employee removed successfully!\n");
    }
    else
    {
        printf("Employee not found!\n");
    }
    
    printMenu();
}
总结

本员工管理系统基于双链表数据结构实现,具有较好的灵活性和性能。通过该系统,用户可以方便地添加、删除、修改和查询员工信息,实现更好的员工管理。