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

4.单链表(list)

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

4.单链表(list)

数据结构之单链表(list)

        案例:定义三个学生信息,通过第一个学生能够找到第二个学生,通过第二个学生能够找到第三学生

        通过第一个学生能够找到第三学生

        

        参考代码:student.c

//student.c:单链表演示
#include 
typedef struct student {
    char name[32];
    int age;
    struct student *next; //保存下一个学生的地址
}stu_t;
int main(void) {
    stu_t stu1 = {"刘备", 20, NULL};
    stu_t stu2 = {"关羽", 19, NULL};
    stu_t stu3 = {"张飞", 18, NULL};
    stu_t head;
    stu_t tail;
    //连接他们
    head.next = &stu1;
    stu1.next = &stu2;
    stu2.next = &stu3;
    stu3.next = &tail;
    tail.next = NULL;
    //通过刘备打印名字  可维护性非常差
    printf("%s, %s, %sn", stu1.name, stu1.next->name, stu1.next->next->name);
    //通过head,tail访问
    for(stu_t *pnode = &head; pnode != &tail; pnode=pnode->next) {
        stu_t *pmid = pnode->next;
        if(pmid != &tail)
            printf("%s ", pmid->name);
    }
    printf("n");
    return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/913678.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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