0

これらのメソッドを実行すると、リストが増えず、理由がわかりません。頭と尻尾は常に同じです。ポインターまたは構造体のセットアップに何か問題がありますか? 私はこの切り替えに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;
        }
    }
}

** 現在は機能しています。次の 4 つのエラーでした。

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

ところで、私はいつも宿題をして授業に出席していますが、ずっと大きなプログラムの小さな部分にとらわれていました。だから、私を侮辱せず、実際に有益なアドバイスをくれた人たちに感謝します. 本当に感謝しています。

4

2 に答える 2

2

ここにあなたの間違いがあります

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;
于 2012-10-25T06:48:38.067 に答える
1

問題はここにあると思います

temp->next = tail;
tail = temp->next;

あなたの中にcase 'b'

temp->nextそれはそのために作るのでhead

于 2012-10-25T06:53:04.850 に答える