码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 宁波财经学院程序设计补修单链表


    2024年6月2日 

    1. #include
    2. #include
    3. // 定义链表节点结构体
    4. typedef struct Node {
    5. int data;
    6. struct Node* next;
    7. } Node;
    8. // 尾插函数
    9. void append(Node** head_ref, int new_data) {
    10. // 1. 分配新节点,并设置其数据
    11. Node* new_node = (Node*)malloc(sizeof(Node));
    12. new_node->data = new_data;
    13. new_node->next = NULL;
    14. // 2. 如果链表为空,则新节点是头节点
    15. if (*head_ref == NULL) {
    16. *head_ref = new_node;
    17. return;
    18. }
    19. // 3. 否则,找到尾节点
    20. Node* last = *head_ref;
    21. while (last->next != NULL) {
    22. last = last->next;
    23. }
    24. // 4. 更改尾节点的next,使其指向新节点
    25. last->next = new_node;
    26. return;
    27. }
    28. // ... 其他链表操作函数和主函数(main) ...

    尾插法是上面的

    下面是头插法

    1. #include
    2. #include
    3. typedef struct Node {
    4. int data;
    5. struct Node* next;
    6. } Node;
    7. void prepend(Node** head_ref, int new_data) {
    8. Node* new_node = (Node*)malloc(sizeof(Node));
    9. new_node->data = new_data;
    10. new_node->next = NULL;
    11. if (*head_ref == NULL) {
    12. *head_ref = new_node;
    13. return;
    14. }
    15. new_node->next = *head_ref;
    16. *head_ref = new_node;
    17. return;
    18. }

    只用知道上传代码即可

  • 相关阅读:
    校园安防监控系统升级改造方案:如何实现设备利旧上云与AI视频识别感知?
    大话设计模式——2.简单工厂模式(Simple Factory Pattern)
    【js】获取easyui datagrid的数据
    HDU_7149
    随想录一刷Day59——单调栈
    Linux华硕笔记本安装ROG Asusctl
    关于group by 后,想要查询多余列的问题
    PCB入门介绍与电阻电容电感类元件的创建
    【WebRTC---源码篇】(十:零)WEBRTC/StreamStatisticianImpl持续更新中)
    QT专栏1 -Qt安装教程
  • 原文地址:https://blog.csdn.net/leke2003/article/details/139396040
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号