C程序要求显示初始化的List
。get3rdYear
从列表P中删除所有三年级的学生。所有三年级学生将被移动到一个新的列表并返回到调用函数。如果课程是BSIT,则函数CCD_。函数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("%dn",temp->student.id);
printf("%s %s %cn",temp->student.fullName.fName,temp->student.fullName.lName,temp->student.fullName.mI);
printf("%sn",temp->student.course);
printf("%dn",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("%dn",trav->student.id);
printf("%s %s %cn",trav->student.fullName.fName,trav->student.fullName.lName,trav->student.fullName.mI);
printf("%sn",trav->student.course);
printf("%dn",trav->student.yrlvl);
trav = trav->link;
}
}
}
void displayList(List P){
List trav;
trav = P;
while(trav != NULL){
printf("%dn",trav->student.id);
printf("%s %s %cn",trav->student.fullName.fName,trav->student.fullName.lName,trav->student.fullName.mI);
printf("%sn",trav->student.course);
printf("%dn",trav->student.yrlvl);
trav = trav->link;
}
}```
您的代码在这个函数中都有两个问题:
void displayBSIT(List P){
List trav;
trav = P;
while(trav != NULL){
if(trav->student.course == "BSIT"){
printf("%dn",trav->student.id);
printf("%s %s %cn",trav->student.fullName.fName,trav->student.fullName.lName,trav->student.fullName.mI);
printf("%sn",trav->student.course);
printf("%dn",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下运行,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,会发生什么?我们会继续到下一个节点吗?
这两个问题共同作用。您的比较不起作用,迭代也不起作用。因此,您的程序不会提供任何有趣的输出。