链表中的节点的数据域可以直接插入结构体吗?打印时要怎么写呢?

链表中的节点的数据域可以直接插入结构体吗?如下:

 struct student
{
    int num;
    char name[10];
    int grade;
};
struct stunode
{
    struct student;
    struct stunode * next;
};

如果这样可以的话,打印时要怎样写呢?(见最后几行,事实证明这样写是不可能成功的。。。)求指点

 #include<stdio.h>
#include<stdlib.h>
struct student
{
    int num;
    char name[10];
    int grade;
};
struct stunode
{
    struct student;
    struct stunode * next;
};
main()
{
    int i;
    struct stunode *p,*head;
    struct student h[13]=
    {
        {2,"Lin",92},
        {3,"Zhang",87},
        {4,"Zhao",72},
        {5,"Ma",91},
        {9,"Zhen",85},
        {11,"Wang",100},
        {12,"Li",86},
        {13,"Xu",83},
        {16,"Mao",78},
        {17,"Hao",95},      
        {20,"Lu",82},
        {21,"Song",76},
        {22,"Wu",88}
    };
    head=NULL;
    for(i=0;i<13;i++)
    {
        p=(struct stunode *)malloc(sizeof(struct stunode));
        p->next=head;
        p->stu=h[i];
        head=p;
    }
    for(i=1;i<=12;i++)
    {
        printf("%d  %s  %f\n",head->stu);
        head++;
    }
    printf("%d  %s  %f\n",head->stu);
}

还有一个问题:数组节点是什么意思?

3个回答

struct student
{
int num;
char name[10];
int grade;
};
struct stunode
{
struct student stu;
struct stunode * next;
};
int main()
{
int i;
struct stunode *p,*head;
struct student h[13]=
{
{2,"Lin",92},
{3,"Zhang",87},
{4,"Zhao",72},
{5,"Ma",91},
{9,"Zhen",85},
{11,"Wang",100},
{12,"Li",86},
{13,"Xu",83},
{16,"Mao",78},
{17,"Hao",95},
{20,"Lu",82},
{21,"Song",76},
{22,"Wu",88}
};
head=NULL;
for(i=0;i {
p=(struct stunode *)malloc(sizeof(struct stunode));
p->next=head;
p->stu=h[i];
head=p;
}
for(i=1;i<=12;i++)
{
printf("%d %s %d\n",head->stu.num, head->stu.name, head->stu.grade);
head = head->next;
}
printf("%d %s %d\n",head->stu.num, head->stu.name, head->stu.grade);
}


可以直接插入结构体。数组节点就是节点里面含有数组。
不过你的程序错误太多。

struct stunode
{
struct student;
struct stunode * next;
};
你这里错了啊!当然不行了。struct student; 这两个联合起来是一个定义,但是你的变量声明呢?
麻烦改成:
struct stunode
{
struct student myStudent;
struct stunode * next;
};

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问