twinkle_snow 2015-12-14 01:12 采纳率: 76.5%
浏览 2277

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

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

 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条回答

  • 微型蚂蚁 2015-12-14 01:31
    关注

    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);
    }

    
    
    评论

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样