2014-09-23 1 views
-2

다음 코드를 사용하여 노드를 추가하고 연결된 현재 모든 노드를 인쇄 할 수 있습니다. 쿼리 기능을 사용하여 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

아래에 지적 된 문제 (그리고 아마도 다른 사람) 때문에 목록을 작성하지 않은 것처럼 보입니다. 컴파일 문자열, 경고, 프로그램 입력 및 런타임 오류 또는 경고를 표시하십시오. 또한 디버거에서 실제로 연결된 목록에 데이터가 있는지 확인하십시오 (디버깅을 위해 목록에 성공적으로 추가 될 때마다 +1 증가하는 전역 int 카운터 유지) –

답변

0

-Wall와 GCC 4.9.1에 의해보고 된 오류입니다 :

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); 

라인에 대한 경우 두 번째 보고서 : scanf("%d", &TEMPID, &NewSAL);scanf 기능 때문에 두 가지 형식 지정자를 기대하고있다 두 개의 매개 변수는 TEMPIDNewSAL을 읽습니다.

에 의해 변경 : scanf("%d %d", &TEMPID, &NewSAL);

-Wall와 연타 3.5에 의해보고 된 오류입니다 :

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 루프를 입력하기 위해 변수를 초기화합니다 (예 : 값 : != 0).

권장 사항은 항상 최대 경고 수준으로 컴파일됩니다.