【发布时间】:2021-03-05 01:37:56
【问题描述】:
C 程序要求显示一个初始化的List。 get3rdYear 从 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