码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 使用链表实现栈操作


    源码如下:

    1. #include
    2. #include
    3. struct node
    4. {
    5. int info;
    6. struct node *link;
    7. };
    8. struct node *top = NULL, *temp;
    9. void push(struct node *);
    10. void pop(struct node *);
    11. void display(struct node *);
    12. int main()
    13. {
    14. int x = 0, item;
    15. printf("\t****stack using linked list****\n");
    16. while (x != 4)
    17. {
    18. printf("\n1. Push\n2. Pop\n3. Display\n4. Exit\n");
    19. printf("Enter your choice: ");
    20. scanf("%d", &x);
    21. switch (x)
    22. {
    23. case 1:
    24. push(top);
    25. break;
    26. case 2:
    27. pop(top);
    28. break;
    29. case 3:
    30. display(top);
    31. break;
    32. case 4:
    33. return 0;
    34. }
    35. }
    36. }
    37. void push(struct node *p)
    38. {
    39. int item;
    40. struct node *temp;
    41. temp = (struct node *)malloc(sizeof(struct node));
    42. printf("\nEnter element to be inserted: ");
    43. scanf("%d", &item);
    44. temp->info = item;
    45. temp->link = top;
    46. top = temp;
    47. printf("Inserted succesfully.\n");
    48. }
    49. void pop(struct node *p)
    50. {
    51. int item;
    52. struct node *temp;
    53. if (top == NULL)
    54. printf("\nStack is empty.\n");
    55. else
    56. {
    57. item = top->info;
    58. temp = top;
    59. top = top->link;
    60. free(temp);
    61. printf("\nElement popped is %d.\n", item);
    62. }
    63. }
    64. void display(struct node *p)
    65. {
    66. if (top == NULL)
    67. printf("\nStack is empty.\n");
    68. else
    69. {
    70. printf("\nElements in the stack are:\n");
    71. while (p != NULL)
    72. {
    73. printf("\t%d\n", p->info);
    74. p = p->link;
    75. }
    76. // printf("%d\n",p->info);
    77. }
    78. }

    测试运行:

        ****stack using linked list****

    1. Push
    2. Pop
    3. Display
    4. Exit
    Enter your choice: 1

    Enter element to be inserted: 88 //入栈
    Inserted succesfully.

    1. Push
    2. Pop
    3. Display
    4. Exit
    Enter your choice: 3

    Elements in the stack are:
        88

    1. Push
    2. Pop
    3. Display
    4. Exit
    Enter your choice: 1

    Enter element to be inserted: 22 //入栈
    Inserted succesfully.

    1. Push
    2. Pop
    3. Display
    4. Exit
    Enter your choice: 3

    Elements in the stack are:
        22
        88

    1. Push
    2. Pop
    3. Display
    4. Exit
    Enter your choice: 2

    Element popped is 22. //出栈

    1. Push
    2. Pop
    3. Display
    4. Exit
    Enter your choice: 3

    Elements in the stack are:
        88
     

  • 相关阅读:
    【Linux基础】基础I/O(一)
    [剑指Offer] 两种方法求解空格替换
    Linux中搜索查找类
    PO接口日志 RSXMB_SHOW_STATUS 统计消息状态概览
    1143 多少个Fibonacci数
    帷幄前沿茶话丨如何发起一场直播间运营变革?
    45-1 waf绕过 - 文件上传绕过WAF方法
    科大讯飞交卷,实测星火大模型
    【机器学习合集】优化目标与评估指标合集 ->(个人学习记录笔记)
    计算机毕业设计Java编程训练系统设计与实现(源码+系统+mysql数据库+lw文档)
  • 原文地址:https://blog.csdn.net/qq_20490175/article/details/134021305
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | 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号