栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C语言单链表练习

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C语言单链表练习

定义一个结构体管理士兵的信息(编号,姓名,兵种,年龄)。输入若干士兵的信息,当输入的士兵编号为 0 时输入结束。建立一个单向链表存放士兵信息并输出。(链表的建立和输出分别定义函数实现

#include 
#include 
struct s{
    int N;
    char name[20];
    char kind [20];
    int age;
    struct s *next;
};
struct s* create(){
    struct s *tmp=(struct s *)malloc(sizeof (struct s));
    return tmp;
}
void ps(struct s *head){
    struct s *tmp=head->next;
    while(head->next){
        printf("No:%d Name:%s Kind:%s Age:%dn",tmp->N,tmp->name,tmp->kind,tmp->age);
        head=tmp;
        tmp=tmp->next;
    }
}
int main()
{
    struct s *tmp,*head,*index;
    head=create();
    index=head;
    int n;
    scanf("%d",&n);
    while (1){
        if(n!=0){
            tmp=(struct s *)malloc(sizeof (struct s));
            tmp->N=n;
            scanf(" %s %s %dn",&tmp->name,&tmp->kind,&tmp->age);
            index->next=tmp;
            index=tmp;
        }else{
            index->next=NULL;
            break;
        }
        scanf("%d",&n);
    }
    ps(head);
    return 0;
}

测试数据

1 yi 海军 23
2 er 陆军 35
3 san 空军 29
0
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/935314.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号