【问题标题】:Why can't the list display?为什么列表不能显示?
【发布时间】:2021-03-05 01:37:56
【问题描述】:

C 程序要求显示一个初始化的Listget3rdYear 从 List P 中删除所有年级 3 的学生。所有 3 年级的学生将被移动到一个新的 List 并返回到调用函数。如果课程是 BSIT,函数 displayBSIT 会显示列表中的学生姓名。函数displayList 显示列表中学生的全名。这个程序的问题是运行时没有初始化数据显示,只有黑屏。

#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>

typedef struct {
    char fName[16];
    char lName[16];
    char mI;
}Name;
typedef struct{
    int id;
    Name fullName;
    char course[20];
    int yrlvl;
}Record;

typedef struct node{
    Record student;
    struct node *link;
}*List;

void initList(List *P);

//create the following functions:
List get3rdYear(List *P);
void displayBSIT(List P);
void displayList(List P);
/*
    get3rdYear-> removes all the students with the year level 3 from List P.
        All the 3 year students will be moved to a new List and returned to the 
        calling function.
    displayBSIT-> displays the student name from the list if the course is BSIT
    displayList-> display the full name of the students in the list
*/

int main(void){
    List L, thirdYear;
    initList(&L);
    displayBSIT(L);
    //thirdYear = get3rdYear(&L);
    displayList(L);
    //displayList(thirdYear);
    //initalize L by calling initList()
    //call displayBSIT()
    //call get3rdYear(), thirdYear will recieve the list returned by get3rdYear()
    //display L and thirdYear
    return 0;
}

void initList(List *P){
    int ndx;
    List temp;
    Record arr[] = {{12, {"Chris", "Vergara", 'S'}, "BSIT", 3}, {32, {"Dione", "Cabigas", 'A'}, "BSCS", 4}, {85, {"Jomer", "Barcenilla", 'G'}, "BSIS", 1}, {98, {"Danise", "Hinoguin", 'B'}, "BSIT", 1}, {456, {"Francis", "Aliser", 'C'}, "BSCS", 3},{888, {"Cody", "Ng", 'A'}, "BSIS", 3},{32,{"Jason", "Carreos", 'S'}, "BSIS", 3}};
    *P = NULL;
    for(ndx = 0; ndx!=7; ndx++){
        temp = (List)malloc(sizeof(struct node));
        if(temp != NULL){
            temp->student = arr[ndx];
            temp->link = *P;
            *P = temp;
        }
    }
}

List get3rdYear(List *P){
    List temp;
    temp = *P;
    while(temp != NULL){
        if(temp->student.yrlvl == 3){
            printf("%d\n",temp->student.id);
            printf("%s %s %c\n",temp->student.fullName.fName,temp->student.fullName.lName,temp->student.fullName.mI);
            printf("%s\n",temp->student.course);
            printf("%d\n",temp->student.yrlvl);
            temp = temp->link;
        }
    }
    return temp;
}

void displayBSIT(List P){
    List trav;
    trav = P;
    while(trav != NULL){
        if(trav->student.course == "BSIT"){
            printf("%d\n",trav->student.id);
            printf("%s %s %c\n",trav->student.fullName.fName,trav->student.fullName.lName,trav->student.fullName.mI);
            printf("%s\n",trav->student.course);
            printf("%d\n",trav->student.yrlvl);
            trav = trav->link;
        }
    }
}

void displayList(List P){
    List trav;
    trav = P;
    while(trav != NULL){
        printf("%d\n",trav->student.id);
        printf("%s %s %c\n",trav->student.fullName.fName,trav->student.fullName.lName,trav->student.fullName.mI);
        printf("%s\n",trav->student.course);
        printf("%d\n",trav->student.yrlvl);
        trav = trav->link;
    }
}```

【问题讨论】:

  • 对于函数displayBSIT,是的
  • 没有警告,编译过程中没有错误

标签: c data-structures linked-list structure


【解决方案1】:

你的代码在这个函数中有两个问题:

void displayBSIT(List P){
    List trav;
    trav = P;
    while(trav != NULL){
        if(trav->student.course == "BSIT"){
            printf("%d\n",trav->student.id);
            printf("%s %s %c\n",trav->student.fullName.fName,trav->student.fullName.lName,trav->student.fullName.mI);
            printf("%s\n",trav->student.course);
            printf("%d\n",trav->student.yrlvl);
            trav = trav->link;
        }
    }
}

首先,== 不比较 C 中的字符串。由于字符串实际上是内存中第一个字符的地址,所以进行这种比较是没有意义的。由于我猜你正在做作业,我将让你了解如何比较 C 中的字符串。

这里更重要的建议是:如果您的编译器还没有告诉您,那么您做错了什么。当我通过https://repl.it 运行它时(我必须将conio 更改为stdio,因为conio 是专有的),我收到以下警告:

> clang-7 -pthread -lm -o main main.c
main.c:84:33: warning: result of comparison against a string literal is unspecified (use strncmp instead) [-Wstring-compare]
        if(trav->student.course == "BSIT"){
                                ^  ~~~~~~
1 warning generated.

您可以看到它在clang 下运行,这是一个受人尊敬的高质量编译器。你现在要知道的最重要的事情是,你的编译器也应该警告你你犯了一个错误。

好的,第二个问题。让我澄清一下displayBSIT代码:

void displayBSIT(List P){
    List trav;
    trav = P;
    while(trav != NULL){
        if(trav->student.course == "BSIT"){
            printStudent(trav->student);
            trav = trav->link;
        }
    }
}

想一想。在当前节点有效的情况下,如果当前节点的学生课程为BSIT,则打印并转到下一个节点。如果当前节点的学生的课程不是 BSIT,会发生什么?我们会继续下一个节点吗?

这两个问题共同作用。您的比较不起作用,您的迭代也不起作用,因此您的程序没有提供任何有趣的输出。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多