在列表中添加节点无法正常工作

时间:2021-06-01 18:19:31

标签: c list pointers

我知道我的非正常功能已经完成,但我真的需要帮助,
当我在这里输入列表头时,我希望它的地址更改为尾地址,但它没有
这是我的功能
在它下面你会找到我调用函数的主要部分
我真的很感谢你的帮助。非常感谢


void add_employee(list *node)
{
    list *tail = (list *)malloc(sizeof(list));

    printf("adress of node %p\n", tail);
    printf("Adding employee\n");

    printf("\t- First name :\t");
    scanf("%10s", tail->e.firstname);
    fflush(stdin);

    printf("\t- Last name  :\t");
    scanf("%10s", tail->e.lastname);
    fflush(stdin);

    printf("\t- Salary     :\t");
    scanf("%lf", &tail->e.salary);
    fflush(stdin);

    tail->next = NULL;
    node = tail;

    printf("continue ? (Y/n) : ");

    if (toupper(getchar()) == 'Y')
        add_employee(tail->next);
}

这里是 main.c 文件\

ps:我知道 display 应该占据头部,如果 add_employee 正常工作,myList 将是尾部,但现在要修改,我只需要知道为什么 add_employee 不能正常工作

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "myFunctionsLists.h"

void main()
{
    list *myList = NULL;

    char choice = 'Q';
    do
    {
        choice = menu();
        empty_buffer();

        switch (choice)
        {
        case 'a':
        case 'A':
            printf("adress %p\n", myList);
            add_employee(myList);
            printf("adress %p\n", myList);
            break;
        case 'd':
        case 'D':
            display_list(myList);
            break;
        case 'q':
        case 'Q':
            printf("quitting...");
            free_list(myList);
            exit(EXIT_SUCCESS);
            break;
        default:
            printf("Unknown choice\n");
            break;
        }
        empty_buffer();
    } while (1);
}

0 个答案:

没有答案
相关问题