搜索链接列表中的元素

时间:2016-11-22 08:46:31

标签: c linked-list structure

当我尝试打印courseCode时,会打印出null,但当我注释掉searchPerson()时,它会完美打印出来。请帮忙

typedef struct enrolledTag{ //list of students enrolled in a class
char givenName[100], lastName[100], studentNumber[11];
float unitsAllowed;
struct enrolledTag *next; 
}ENROLLED;

typedef struct studentTag{
    char givenName[100], lastName[100], studNum[11];
    int unitsAllowed, unitsEarned;
    struct acquiredClasses *subjectTaken;
    struct studentTag *next;
}STUDENT;
typedef struct courseTag{
char courseCode[10];
char section[10];
int units;
int maxClassSize;
int currentClassSize;
struct enrolledTag *studentsEnrolled;
struct courseTag *next;
}COURSE;

STUDENT* searchPerson(STUDENT *h){
    STUDENT *ptr=h;
    char lookFor[100];
    printf("Enter student number(XXXX-XXXXX): ");
    getchar();
    fgets(lookFor,100,stdin);
    lookFor[strlen(lookFor)-1]='\0';
    while(ptr!=NULL){
        if(strcmp(lookFor,ptr->studNum)==0){
            //printf("Student found!\n");
            return ptr;
        }
        ptr=ptr->next;
    }
    printf("No match found.\n");
    return ptr=NULL;  
}

void getCourseSection(char *inCourseCode, char *inSection){
    printf("Enter course code: ");
    getchar();
    fgets(inCourseCode, 10, stdin);
    inCourseCode[strlen(inCourseCode)-1]='\0'; //removes \n 
    printf("Enter section name: ");
    //getchar();
    fgets(inSection, 100, stdin);
    inSection[strlen(inSection)-1]='\0'; //removes \n 
}

COURSE *searchClass(COURSE *courseHead, char inCourseCode[10], char        inSection[10]){
    COURSE *travCourse=courseHead;
    while(travCourse!=NULL){
        if((strcmp(inCourseCode , travCourse->courseCode)==0) &&  (strcmp(inSection, travCourse->section)==0)){
            //printf("\nClass already in list\n");
            return travCourse;
        }
        travCourse=travCourse->next;
    }
    //printf("No match found.\n");
    return travCourse=NULL;
}

void addStudentToClass(STUDENT **studHead, COURSE **courseHead){
    STUDENT *studentNum=searchPerson(*studHead);
    char inCourseCode[10], inSection[10];

    getCourseSection(inCourseCode, inSection);
    COURSE *class=searchClass(*courseHead, inCourseCode, inSection);

    //this has a problem
    printf("Student: %s\n", (studentNum)->givenName); 
    printf("Class: %s\n", class->courseCode); 
    // why does class prints null?
}

0 个答案:

没有答案