You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言如何将头指针传入函数?链表员工系统打印搜索运行报错如何修复

C语言链表员工管理系统代码修复说明

核心错误点

  • 结构体定义错误:name成员仅定义为char类型,无法存储字符串,使用%s输入会导致内存越界,是运行时错误的核心诱因之一,需要修改为足够长度的字符数组。
  • 头指针未正确传递:createlist函数返回新创建的链表头指针,但原代码selectmenu中调用该函数时没有接住返回值,后续打印、搜索操作访问的仍是空/野指针。
  • 全局变量滥用:全局temp指针在多个函数中被反复修改,逻辑混乱,应该在遍历链表的函数中使用局部临时指针。
  • 链表构建逻辑错误:createlist中用于判断是否为头节点的变量i从未被修改,始终为0,导致每个节点都被当作头节点处理,链表根本没有正确拼接。
  • main函数定义错误:C语言标准main函数不支持自定义struct node*类型的参数,初始传入的头指针本身就是野指针。
  • 递归逻辑冗余:selectmenu、searchemp中不必要的递归调用会导致栈溢出风险,应该改用循环实现菜单逻辑。
  • 搜索逻辑缺陷:搜索到目标员工后没有终止遍历,会进入死循环。

修复后完整代码

#include <stdio.h>
#include <stdlib.h>

struct node {
    int number;
    char name[50]; // 改成长度足够的字符数组存储姓名
    int salary;
    struct node *next;
};

// 创建链表,返回头指针
struct node* createlist(){
    int num, j;
    printf("\nEnter the number of employees : ");
    scanf("%d", &num);
    
    struct node *head = NULL, *temp = NULL;
    for(j=0; j<num; j++){
        struct node *new_node = (struct node*)malloc(sizeof(struct node));
        new_node->next = NULL; // 新节点next默认置空
        
        printf("\nEnter the employee number      : ");
        scanf("%d", &new_node->number);
        printf("Enter the name           : ");
        scanf("%s", new_node->name);
        printf("Enter the salary         : ");
        scanf("%d", &new_node->salary);
        printf("\n");
        
        if(head == NULL){ // 头节点为空时赋值给头
            head = new_node;
            temp = head;
        } else { // 否则拼到链表尾部
            temp->next = new_node;
            temp = temp->next;
        }
    }
    return head;
}

// 打印链表
void printlist(struct node *head){
    if(head == NULL){
        printf("\n\nENTER THE DATA BEFORE PRINT!!!!\n\n");
        return;
    }
    struct node *temp = head; // 用局部临时变量遍历
    printf("员工编号\t姓名\t工资\n");
    while(temp != NULL){
        printf("%d\t\t%s\t\t%d\n", temp->number, temp->name, temp->salary);
        temp = temp->next;
    }
}

// 搜索员工
void searchemp(struct node *head) {
    if(head == NULL){
        printf("\n\nENTER THE DATA BEFORE SEARCH!!!!\n\n");
        return;
    }
    printf("Enter the employee number : ");
    int nm;
    scanf("%d", &nm);
    
    struct node *temp = head;
    while(temp != NULL){
        if(temp->number == nm){
            printf("找到员工:%d\t\t%s\t\t%d\n", temp->number, temp->name, temp->salary);
            break; // 找到就退出循环
        }
        temp = temp->next;
    }
    if(temp == NULL){
        printf("未找到编号为%d的员工\n", nm);
    }
}

// 菜单逻辑用循环实现
void selectmenu(){
    struct node *head = NULL; // 头指针作为局部变量
    int op;
    do {
        printf("\n=========EMPLOYEE REGISTRATION SYSTEM=========\n");
        printf("1.Enter Employee Details\n2.Output Employee Details\n3.Search Employee\n4.Exit\n");
        printf("Enter the option : ");
        scanf("%d", &op);
        
        switch (op){
            case 1:
                head = createlist(); // 接住创建后返回的头指针
                break;
            case 2:
                printlist(head);
                break;
            case 3:
                searchemp(head);
                break;
            case 4:
                printf("退出系统\n");
                break;
            default:
                printf("输入选项无效,请重新输入\n");
        }
    } while(op != 4); // 选4才退出循环
}

int main(){
    selectmenu();
    return 0;
}

内容的提问来源于stack exchange,提问作者Maheshi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 22:15:06