字符串存储和链接列表中的比较

时间:2014-01-28 17:10:34

标签: c string pointers linked-list

这是我的代码,但似乎将字符串存储到链表中的变量不起作用。请问问题是什么以及如何解决。谢谢。

typedef struct studList {
    char studName[3];
    struct studList *next;
} stud;

typedef struct courseList { 
    char courseName[3];
    stud *toStud;
    struct courseList *next;
} course;

int compareStuds(stud *toStud1, stud *toStud2) {
    int count = 0;
    stud *tempPtr;
    while(toStud1 != NULL) {
        tempPtr = toStud2;
        while(tempPtr != NULL) {
            if(strcmp(toStud1->studName, tempPtr->studName) == 0) {
                count = 1;
            }
            tempPtr = tempPtr->next;
        }
        toStud1 = toStud1->next;
    }
    return count;
}

int main (void)
{
    course *chead = NULL, *cnode, *hptr = NULL, *hptr2 = NULL;
    stud *shead = NULL, *snode = NULL;
    int noc,i,x,y,row,col,matrix[50][50],z,a = 0;
    printf("Enter the number of course(s): ");
    scanf("%d", &noc);
    printf("Enter 3 letter course name followed by 3 letter student names separated by spaces.\n");

    for(i = 0; i < noc; i++){
        x = 0;
        y = 0;
        z = 0;
        scanf(" %100[^\n]%*c",in);
        while(in[z] != NULL){
            if(x == 0){
                cnode = (course *) malloc(sizeof(course));
            }
            if(in[z] != ' ' && x < 3){
                cnode->courseName[x] = in[z];
                x++;
            }
            else if(in[z] != ' '){
                if(y == 0){
                    snode = (stud *) malloc(sizeof(stud));
                }
                if(y < 3){
                    snode->studName[y] = in[z];
                    y++;
                }
            }
            if(y == 3){
                cnode->toStud = snode;
                shead = snode->next;
                snode = shead;
                y = 0;
            }
            z++;
        }
        chead = cnode->next;
        cnode = chead;
    }
    hptr = chead;
    for(i = 0; i < noc; i++)
        matrix[i][i] = 0;
    for(row = 0; hptr != NULL; row++){
        hptr2 = hptr->next;
        col = row + 1;
        for(;hptr2 != NULL; col++){
            matrix[row][col] = compareStuds(hptr->toStud, hptr2->toStud);
            matrix[col][row] = compareStuds(hptr->toStud, hptr2->toStud);
            hptr2 = hptr2->next;
        }
        hptr = hptr->next;
    }
    for(row = 0; row < noc; row++){
        for(col = 0; col < noc; col++){
            printf("%d ", matrix[row][col]);
        }
        printf("\n");
    }
    return 0;
}

如果课程中有类似的学生,代码基本上会输出矩阵。如果同一个学生存在,则会被标记为1.我似乎无法将课程名称和学生存储在链接列表中,因为您需要为每个课程添加学生链接列表。并且根据用户有很多课程链表。有什么想法吗?

输入就像:

输入课程编号:5

c01 qwe wer ert rty tyu
c02 asd sdf dfg fgh ghj hjk
c03 aaa zzz xxx sss www
c04 qwe aaa dfg  poi lll
c05 asd ppp lll mmm

0 0 0 1 0
0 0 0 1 1
0 0 0 1 0
1 1 1 0 1
0 1 0 1 0

注意:矩阵由1到n个课程的课程表示。

如下所示,

 c01 c02 c03 c04 c05
c01 0   0   0   1   0
c02 0   0   0   1   1
c03 0   0   0   1   0
c04 1   1   1   0   1
c05 0   1   0   1   0

如果同一班级中有学生,则标记为1,如果不是则为0.因为c01和c04都有'qwe',然后行c04和col c01标记为1,与行c01相同col col 04也用1标记。

1 个答案:

答案 0 :(得分:0)

问题是您将三个字符的字符串存储在数组中,其中包含三个字符的空格。这个问题是在C字符串中没有长度,像strcmp这样的函数希望所有字符串都以特殊字符终止,即所谓的空字符(不要与{{1]混淆指针)。此特殊字符通常表示为NULL

您需要做的是增加所有字符串数组以为此终结符字符腾出空间,并将其添加到字符串的末尾。就像在'\0'中一样,例如

char studName[4];