c - 链表队列。坚持指针(我认为)

标签 c list linked-list

当我运行这些方法时,我的列表从未增长,我不知道为什么。头和尾总是相等的。我的指针或结构设置有问题吗?我花了 4 个小时来切换这些东西,但我似乎无法找到为什么事情不起作用,除非它是一个指针的东西(我正在学习但不擅长)..

typedef struct EmployeeStruct
{
    char LastName[10];
    char FirstName[10];
    struct EmployeeStruct *next;  
} Employee;

struct EmployeeStruct *head,*tail, *temp;

Employee* hireEmployee(Employee* head, char LastName[10], char FirstName[10])
{
    // exit if max number of employees is reached
    if(eNum > MAX_EMPLOYEES)
    {
        printf("Sorry, but you can't hire any more employees right now\n");
        return 0;
    }
    // insert at tail
    newHire = head;
    while(newHire->next != NULL)
    {
        newHire = newHire->next;
        printf("nope\n");
    }
    // allocate memory
    newHire->next = malloc(sizeof(Employee));

    if(newHire == NULL)
    {
        printf("Memory allocation failed");
        return 0;
    }

    newHire->next = NULL;
    // insert values into this node
    strcpy(newHire->LastName, LastName );
    strcpy(newHire->FirstName, FirstName );
    newHire->EmployeeNumber = eNum;
    tail = newHire;
    //printf("%d\n",newHire->EmployeeNumber);
    eNum+=1;

    //printf("%d\n",newHire->EmployeeNumber);
    return newHire;
}

int main()
{  
    char choice;
    char first[20];
    char last[20];
    int i = 0;
    // allocate memory
    head = malloc(sizeof(Employee));

    if(head == NULL)
    {
        printf("Memory allocation failed");
        return 0;
    }
    head->next = tail;

    while(TRUE)
    {
        // prompt user for choice
        printf("Please choose from the following options:\n");
        printf("a: Hire new employee\nb: Promote employee\nc: Delete employee\nd: Display roster\ne: Exit\n");
            scanf("\n%c", &choice);

        switch(choice)
        {
            case 'a':
                printf("New employees first name:\n");
                    scanf("%s", first);
                printf("New employees last name:\n");
                    scanf("%s", last);
                tail = hireEmployee(head,last,first);
                temp = head;
                while(temp->next != NULL)
                {
                    temp = temp->next;
                    printf("nope\n");
                }
                temp->next = tail;
                tail = temp->next;
                printf("A%d: %s %s\n", tail->EmployeeNumber,tail->FirstName,tail->LastName);
                tail = tail->next;
                printf("A%d: %s %s\n", tail->EmployeeNumber,tail->FirstName,tail->LastName);
                tail->next = NULL;
                //printEmployees(head);
                break;
            case 'b':
                //TBD
                break;
        }
    }
}

** 现在可以工作了,就是这四个错误:

(newHire == NULL)    
(head == NULL)
newHire =newHire->next;
temp->next = tail;

顺便说一句,我总是做作业和上课,而我只是被困在一个更大的程序中的一小部分。因此,感谢那些没有侮辱我并实际提供有用建议的人。我真的很感激。

最佳答案

这是你的错误

newHire->next = malloc(sizeof(Employee));

malloc的返回类型是void*所以改成

newHire->next = (EmployeeStruct *)malloc(sizeof(Employee));

if(newHire == NULL)

它永远不会为 NULL 将其更改为

if(newHire->next == NULL)

也改变

newHire->next = NULL;

你必须再遍历一次链接

newHire=newHire->next;
newHire->next = NULL;

关于c - 链表队列。坚持指针(我认为),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13062903/

相关文章:

python - 流媒体录制程序VLC

c - 如何检测汇编语言 X86 中的溢出情况

c - C语言如何将字符串插入到链表中?

algorithm - 用链表解决hash冲突,下一步怎么识别item

c - 将元素插入排序列表

c++ - 删除的 Windows 服务仍在运行

计算OpenSSL加密和解密的缓冲区大小?

r - 如何从嵌套列表中提取一个元素作为向量 - R twitteR 包

Python 列表到 Pandas 数据框的字典

java - Java 中的关联数组