-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 증가하는 전역 int 카운터 유지) –