功能无法从链表中读取

时间:2014-09-23 21:07:47

标签: c

我有以下代码,我可以在其中添加节点并打印链接列表中的所有当前节点。当我尝试使用查询功能插入ID排名并仅打印那些我得到的内容和修改相同的内容时,我输入一个ID和金额进行更改而不做任何操作。

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

struct employeeData {
    int EMP_ID;
    char name[20];
    int dept;
    int rank;
    int salary;

    struct employeeData* next;
};

void initializeList(struct employeeData** List);
void add(struct employeeData** List);
void Delete(struct employeeData** List);
void modify(struct employeeData** List);
void query(struct employeeData** List);
void print(struct employeeData** List);

int main() {
    struct employeeData* myList = NULL;
    int inputNUM, myListInput;
    // initializeList(myList);
    while (inputNUM != 0) {
        printf("Please select an option from the following menu\n");
        printf("1) To add a new employee \n");
        printf("2) To delete an employee \n");
        printf("3) To modify an employee record \n");
        printf("4) To query employees by rank \n");
        printf("5) To print all employee information \n\n");
        printf("Enter 0 to stop \n\n");

        printf("Input: ");
        scanf("%d", &inputNUM);

        printf("\n");

        if (inputNUM == 1) {
            add(&myList);
        }
        if (inputNUM == 2) {
            Delete(&myList);
        }
        if (inputNUM == 3) {
            modify(&myList);
        }
        if (inputNUM == 4) {
            query(&myList);
        }
        if (inputNUM == 5) {
            print(&myList);
        }
    }

    return 0;
}

/*
void initializeList(struct employeeData **List)

{

FILE *ifp;
ifp = fopen("empInfo.txt","r");

struct employeeData *Temp = NULL;
Temp = (struct employeeData*)malloc(sizeof(struct employeeData));

while (ifp != NULL)
{
    fscanf(ifp, "%d %s %d %d %d", &Temp->EMP_ID, Temp->name, &Temp->dept,
&Temp->rank, &Temp->salary);

    Temp->next;
}

while (*List->next != NULL)
{
    *List = *List->next;
}

*List-next = Temp;

fclose(ifp);

}

*/

void add(struct employeeData** List) {
    struct employeeData* Temp = NULL;
    Temp = (struct employeeData*)malloc(sizeof(struct employeeData));

    printf("Please enter the information of the employee: ");
    scanf("%d %s %d %d %d", &Temp->EMP_ID, Temp->name, &Temp->dept, &Temp->rank,
          &Temp->salary);

    printf("\n");
    Temp->next = NULL;
    if (*List == NULL) {
        *List = Temp;
    }

    else {
        struct employeeData* last = *List;

        while (last->next != NULL) {
            last = last->next;
        }

        last->next = Temp;
    }
}

void Delete(struct employeeData** List) {
    int TEMPID = NULL;

    struct employeeData* Temp = *List;

    printf("Please enter the EMP_ID for the employee that you would like to be "
           "deleted: ");
    scanf("%d", &TEMPID);

    while (Temp->EMP_ID != TEMPID) {
        if (Temp->next->EMP_ID == TEMPID) {
            break;
        } else {
            Temp = Temp->next;
        }
    }

    if (Temp->next->next == NULL) {
        free(Temp->next);
        *List = Temp;
    } else {
        struct employeeData* connect = Temp;
        struct employeeData* del = Temp->next;
        connect = Temp->next->next;
        Temp->next = connect;
        free(del);
        *List = Temp;
    }
}

void modify(struct employeeData** List) {
    int TEMPID = 0, NewSAL = 0;

    struct employeeData* Temp = *List;

    printf("Please enter the EMP_ID and new salary for the employee that you "
           "would like to modify: ");
    scanf("%d", &TEMPID, &NewSAL);

    while (Temp->EMP_ID != TEMPID) {
        Temp = Temp->next;
    }

    Temp->salary = NewSAL;
    *List = Temp;
}

void query(struct employeeData** List) {
    int TEMPID = NULL;

    printf("Please provide the rank that you would like to query: ");
    scanf("%d", &TEMPID);

    struct employeeData* Temp = *List;
    while (Temp != NULL) {
        if (Temp->EMP_ID == TEMPID) {
            printf("%s \n", Temp->name);
            Temp = Temp->next;
        } else {
            Temp = Temp->next;
        }
    }

    printf("\n");
}

void print(struct employeeData** List) {
    struct employeeData* Temp = *List;
    while (Temp != NULL) {
        printf("%d %s %d %d %d \n", Temp->EMP_ID, Temp->name, Temp->dept,
               Temp->rank, Temp->salary);

        Temp = Temp->next;
    }

    printf("\n");
}

1 个答案:

答案 0 :(得分:0)

这是GCC 4.9.1使用 -Wall

报告的错误
E:\test.cpp: In function 'int main()':
E:\test.cpp:23:19: warning: unused variable 'myListInput' [-Wunused-variable]
     int inputNUM, myListInput;
                   ^
E:\test.cpp: In function 'void modify(employeeData**)':
E:\test.cpp:153:33: warning: too many arguments for format [-Wformat-extra-args]
     scanf("%d", &TEMPID, &NewSAL);

关于第{:1}}行的第二个报告,scanf("%d", &TEMPID, &NewSAL);函数需要两个格式说明符,因为要读取scanfTEMPID两个参数。

更改者:NewSAL

这是Clang 3.5使用 -Wall

报告的错误
scanf("%d %d", &TEMPID, &NewSAL);

你在while条件中使用了一个未初始化的变量(这是未定义的行为,任何事情都可能发生):E:\test.cpp:23:19: warning: unused variable 'myListInput' [-Wunused-variable] int inputNUM, myListInput; ^ E:\test.cpp:23:9: warning: variable 'inputNUM' is used uninitialized whenever function 'main' is called [-Wsometimes-uninitialized] int inputNUM, myListInput; ~~~~^~~~~~~~ E:\test.cpp:25:12: note: uninitialized use occurs here while (inputNUM != 0) { ^~~~~~~~ E:\test.cpp:23:17: note: initialize the variable 'inputNUM' to silence this warning int inputNUM, myListInput; ^ = 0 ,在这种情况下,解决方案是初始化变量(例如:使用值while (inputNUM != 0) { ),用于进入while循环。

推荐始终使用可用的最高警告级别进行编译。