码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 数据结构day7栈-链式栈原理及实现


     

     

     全部代码:

    main.c

    1. #include
    2. #include
    3. #include
    4. #include "linkstack.h"
    5. int main(int argc, char *argv[])
    6. {
    7. linkstack s;
    8. s = stack_create();
    9. if(s == NULL)
    10. {
    11. return -1;
    12. }
    13. stack_push(s, 10);
    14. stack_push(s, 20);
    15. stack_push(s, 30);
    16. stack_push(s, 40);
    17. stack_push(s, 50);
    18. #if 0
    19. while(!stack_empty(s))//栈不空,出栈
    20. {
    21. printf("pop : %d\n",stack_pop(s));
    22. }
    23. #endif
    24. s = stack_free(s);
    25. return 0;
    26. }

    linkstack.h

    1. typedef int data_t;
    2. typedef struct node{
    3. data_t data;
    4. struct node *next;
    5. }listnode, *linkstack;
    6. linkstack stack_create();
    7. int stack_push(linkstack s, data_t value);
    8. data_t stack_pop(linkstack s);
    9. int stack_empty(linkstack s);
    10. data_t stack_top(linkstack s);
    11. linkstack stack_free(linkstack s);

    linkstack.c

    1. #include
    2. #include
    3. #include
    4. #include "linkstack.h"
    5. linkstack stack_create(){
    6. linkstack s;
    7. s = (linkstack)malloc(sizeof(listnode));
    8. if(s == NULL)
    9. {
    10. printf("malloc failed\n");
    11. return NULL;
    12. }
    13. s->data = 0;
    14. s->next = NULL;
    15. }
    16. int stack_push(linkstack s, data_t value){
    17. linkstack p;
    18. if(s == NULL)
    19. {
    20. printf("s is NULL\n");
    21. return -1;
    22. }
    23. p = (linkstack)malloc(sizeof(listnode));
    24. if(p == NULL)
    25. {
    26. printf("malloc failed\n");
    27. return -1;
    28. }
    29. p->data = value;
    30. //p->next = NULL;
    31. p->next = s->next;
    32. s->next = p;
    33. return 0;
    34. }
    35. data_t stack_pop(linkstack s){
    36. linkstack p;
    37. data_t t;
    38. p = s->next;
    39. s->next = p->next;
    40. t = p->data;
    41. free(p);
    42. p=NULL;
    43. return t;
    44. }
    45. //1-empty
    46. int stack_empty(linkstack s){
    47. if(s == NULL)
    48. {
    49. printf("s is NULL\n");
    50. return -1;
    51. }
    52. return (s->next == NULL ? 1 : 0);
    53. }
    54. data_t stack_top(linkstack s){
    55. return (s->next->data);//栈顶的元素
    56. }
    57. linkstack stack_free(linkstack s){
    58. linkstack p;
    59. if(s == NULL)
    60. {
    61. printf("s is NULL\n");
    62. return NULL;
    63. }
    64. while(s != NULL)
    65. {
    66. p = s;
    67. s = s->next;
    68. printf("free:%d\n",p->data);
    69. free(p);
    70. }
    71. return NULL;
    72. }

     运行结果:

  • 相关阅读:
    Spring中value的#和$区别
    计算机毕业设计Java瀚绅睿茨二人二轮车租赁管理(源码+系统+mysql数据库+lw文档)
    文心一言 VS 讯飞星火 VS chatgpt (112)-- 算法导论10.2 3题
    [硬件基础]-双稳态多谐振荡器配置
    Element UI之Checkbox 多选框
    会议管理系统SSM记录(二)
    cocos入门7:cocos creator 中的ui系统
    浅谈对接海康SDK语音对讲功能
    萌新看过来,你还学不懂VScode插件吗?
    【C++】C++ 引用详解 ⑦ ( 指针的引用 )
  • 原文地址:https://blog.csdn.net/AuroraSmith/article/details/132700753
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | 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号