| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 908 人关注过本帖
标题:求大神看一看我这个动态链表的函数怎么不能用
只看楼主 加入收藏
a83342358
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2018-4-9
结帖率:66.67%
收藏
 问题点数:0 回复次数:2 
求大神看一看我这个动态链表的函数怎么不能用
代码如下:
#include <stdio.h>
#include <malloc.h>
struct student
{
    char name[10];
    struct student *next;
};

struct student *Input(int n)
{
    int i;n=0;
    struct student *s,*r,*head;
    r=s=(struct student*)malloc(sizeof(struct student));
    for(i=0;i<n;i++)
    {
        printf("请输入第%d个学生的姓名:",n+1);
        scanf("%s",&s->name);
        if(i==0)
            r=head;
        if(i>0)
            r->next=s;
        r=s;
        s=(struct student*)malloc(sizeof(struct student));
    }
    r->next=NULL;
    return head;
}
int main()
{
    int n;struct student *s;
    scanf("%d",&n);
    s=Input(n);
    printf("%s",s->name);
    return 0;
}

运行之后输入n的值之后就崩溃了。。
搜索更多相关主题的帖子: 动态 struct student name int 
2018-04-10 15:00
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10539
专家分:42927
注 册:2014-5-20
收藏
得分:0 
#include <stdio.h>
#include <malloc.h>
struct student
{
    char name[10];
    struct student *next;
};

struct student *Input(int n)
{
    struct student *head, *s;
    head = (struct student*)malloc(sizeof(struct student));
    s = head;
    printf("请输入第1个学生的姓名:");
    scanf("%s", s->name);
    int i;
    for(i=1; i<n; i++)
    {
        s->next = (struct student*)malloc(sizeof(struct student));
        s = s->next;
        printf("请输入第%d个学生的姓名:",i+1);
        scanf("%s", s->name);
    }
    s->next = NULL;
    return head;
}

int main()
{
    int n;
    struct student *s;
    scanf("%d",&n);
    s=Input(n);
    for (; s; s=s->next)
       printf("%s\n",s->name);
    return 0;
}
2018-04-10 15:52
a83342358
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2018-4-9
收藏
得分:0 
非常感谢
2018-04-10 19:38
快速回复:求大神看一看我这个动态链表的函数怎么不能用
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016767 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved