【问题标题】:String storage and Comparison in Linked List链表中的字符串存储和比较
【发布时间】:2014-02-20 03:23:38
【问题描述】:

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

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 c04 也标有 1。

【问题讨论】:

  • 变量in是什么?
  • 另外,您必须记住字符串包含一个额外的特殊字符 ('\0'),它告诉系统字符串在哪里结束。所以对于一个三字符的字符串,你实际上需要 四个 个字符。
  • in 只是一个临时变量,用于存储整个输入字符串以将每个字符分配为课程或学生。 in 的前 3 个字符被添加到 course 结构中,后面的 3 个字符被添加到 student 结构中

标签: c string pointers linked-list


【解决方案1】:

问题是您将三个字符的字符串存储在数组中,其中三个字符的空间。问题在于 C 中的字符串没有长度,像 strcmp 这样的函数期望所有字符串都以特殊字符终止,即所谓的空字符(不要与 NULL 指针混淆) .这个特殊字符通常表示为'\0'

你需要做的是增加所有字符串数组为这个终止符腾出空间,并将它添加到字符串的末尾。就像在char studName[4]; 和例如

if(y < 3) {
    snode->studName[y++] = in[z];
} else if (y == 3) {
    snode->studName[y] = '\0';
}

【讨论】:

  • 如果你使用 studName[3],这意味着你有 4 个插槽可以放置一个角色。这还不够吗?即使我增加了字符串数组,我得到的输出也是错误的。我认为问题在于链表的组织方式
  • @Marquack 不,那是三个插槽,编号从零到二。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-05
  • 2020-05-19
  • 2013-02-25
  • 1970-01-01
  • 2018-04-10
相关资源
最近更新 更多