gpt4 book ai didi

c - 打印结构的所有 "members"

转载 作者:太空宇宙 更新时间:2023-11-04 03:22:51 25 4
gpt4 key购买 nike

我有嵌套结构(如下所示)

slist* add_student() 中,我将新学生添加到列表中

void print_student() 应该打印列表

void print_students(slist* students){
slist *tempS = students;
while(tempS){
printf("%d:%s\n", tempS->info->id, tempS->info->name);
tempS = tempS->next;
}
}


slist* add_student(slist *students, char *name, int id){

student* tempStudent;
tempStudent = (student *)malloc(sizeof(student));
tempStudent->id = id;
tempStudent->name = (char*)malloc(strlen(name)+1);
strcpy(tempStudent->name, name);

slist *news;
news=(slist *)malloc(sizeof(slist));

news->info = tempStudent;
news->next = students;
return news;
}

现在的问题是它只打印最后输入的“student”,我似乎无法分辨哪个函数做错了,所以问题是,它做错了吗因为我正在使用新的定义部分到结构 (slist* tempS = students in void print_students()) 例如?或者它是否与 next(在两个函数中)有关?

an example of an I/O would be

and another...

我用的struct和main,如果有人想看的话

static void getstring(char *buf, int length) {
int len;
buf = fgets(buf, length, stdin);
len = (int) strlen(buf);
if (buf[len-1] == '\n')
buf[len-1] = '\0';
}
int main() {
slist* students = 0;

char c;
char buf[100];
int id, num;

do {
printf("Choose:\n"
" add (s)tudent\n"
" (p)rint lists\n"
" (q)uit\n");

while ((c = (char) getchar()) == '\n');
getchar();

switch (c) {
case 's':
printf("Adding new student.\n");

printf("Student name: ");
getstring(buf, 100);

printf("Student ID: ");
scanf("%d", &id);

students = add_student(students, buf, id);

break;
case 'p':
printf("Printing Information.\n");
print_students(students);
break;
}

if (c != 'q')
printf("\n");
} while (c != 'q');
return 0;
}


// structures
typedef struct student {
char *name;
int id;
struct clist *courses;
} student;

typedef struct course {
char *title;
int number;
struct slist *students;
} course;

typedef struct slist {
student *info;
struct slist *next;
} slist;

typedef struct clist {
course *info;
struct clist *next;
} clist;

最佳答案

一般来说,拿起一张纸和一支铅笔,画出你的代码做了什么;这是在遇到麻烦时找出列表问题的专业提示!


您的代码现在没问题*,让我向您解释原因。

因此,如果您这样做,您会看到当第一个学生到达时,您会为他/她分配空间,然后正确填充该结构。 tempStudent 指向该结构。

然后,news 将创建一个新结构,它是处理列表的结构。它的 info 字段设置为新创建的学生,next 指向 students,我猜当第一个学生被添加时它是 NULL。

然后你返回 news 并且 students 现在指向它。


现在我们要添加第二个学生。我们创建了他的结构 - 到目前为止一切顺利!

我们再次创建处理列表的结构news。它会将 info 分配给新创建的学生,而 next 将指向 students,这就是我们想要的。

然后你返回 news 并且 students 现在指向它。

当然,新创建的学生将排在列表的第一位,尽管我们将他/她添加到第二位。


结果,你会得到这样的东西:

Georgioss-MacBook-Pro:~ gsamaras$ gcc -Wall main.c
Georgioss-MacBook-Pro:~ gsamaras$ ./a.out
Choose:
add (s)tudent
(p)rint lists
(q)uit
s
Adding new student.
Student name: Leon
Student ID: 1

Choose:
add (s)tudent
(p)rint lists
(q)uit
s
Adding new student.
Student name: kate
Student ID: 2

Choose:
add (s)tudent
(p)rint lists
(q)uit
p
Printing Information.
2:kate
1:Leon

没关系。


*当然你还得写其他功能,比如删除你的列表,取消分配你已经分配的空间。

Do I cast the result of malloc?不!

关于c - 打印结构的所有 "members",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43834501/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com