请教c的小问题
#include <stdio.h>struct birthday{
int year;
int month;
int day;
};
struct student{
int id;
char * name;
struct birthday b;
struct student * next;
};
void outpt( struct student * p ) {
while (p->next != NULL){
printf("id=%dname:%s birthday:%d,%d,%d\n",
p->id, p->name, p->b.year, p->b.month, p->b.day);
p->next = p->next->next;
}
}
int main() {
struct student stu[] = {{1,"zhangsan", {1990, 1, 1}, NULL},
{2, "lisi", {2000, 2, 2}, NULL},
{3, "wangwu", {1992, 3, 3}, NULL},
{4, "zhaoliu", {1989, 4, 4}, NULL},
{5, "maqi", {1995, 5, 5}, NULL}};
stu.next = &stu;
stu.next = &stu;
stu.next = &stu;
stu.next = &stu;
struct student * pstu = stu;
outpt(pstu);
return 0;
}
不能遍历这个链表,好像是指针进不了stu,请问是哪的问题? p->next = p->next->next; 改成p = p->next; wslans 发表于 2024-9-6 15:50
p->next = p->next->next; 改成p = p->next;
谢谢,我先去试一下,然后再想为什么,不明白再回来请教。 xinkuzix 发表于 2024-9-6 15:52
谢谢,我先去试一下,然后再想为什么,不明白再回来请教。
stu,遍历不到 wslans 发表于 2024-9-6 15:50
p->next = p->next->next; 改成p = p->next;
stu显示不出来, while (p->next != NULL)改while(p!=NULL) wslans 发表于 2024-9-6 16:02
while (p->next != NULL)改while(p!=NULL)
好了,谢谢老师。我去消化一下 #include <stdio.h>
struct birthday {
int year;
int month;
int day;
};
struct student {
int id;
char *name;
struct birthday b;
struct student *next;
};
void outpt(struct student *p) {
while (p != NULL) {
printf("id=%dname:%s birthday:%d,%d,%d\n",
p->id, p->name, p->b.year, p->b.month, p->b.day);
p = p->next;
}
}
int main() {
struct student stu[] = {{1, "zhangsan", {1990, 1, 1}, NULL},
{2, "lisi", {2000, 2, 2}, NULL},
{3, "wangwu", {1992, 3, 3}, NULL},
{4, "zhaoliu", {1989, 4, 4}, NULL},
{5, "maqi", {1995, 5, 5}, NULL}};
stu.next = &stu;
stu.next = &stu;
stu.next = &stu;
stu.next = &stu;
struct student *pstu = stu;
outpt(pstu);
return 0;
}
页:
[1]