• 数据结构day07(栈和队列)


    今日任务

    链式队列:

    head.h

    1. #ifndef __HEAD_H__
    2. #define __HEAD_H__
    3. #include
    4. #include
    5. typedef int datatype;
    6. typedef struct link_list{
    7. datatype data;
    8. struct link_list* next;
    9. }link,*linkp;
    10. typedef struct circulate_line_top{
    11. linkp front;
    12. linkp rear;
    13. }link_top,*link_topp;
    14. link_topp create_top();
    15. int push(link_topp p,datatype data);
    16. int pull(link_topp p);
    17. int output(link_topp p);
    18. int del(link_topp p);
    19. #endif

    fun.c

    1. #include "head.h"
    2. /*
    3. * function: 创建top节点
    4. * @param [ in]
    5. * @param [out]
    6. * @return
    7. */
    8. link_topp create_top(){
    9. link_topp top=(link_topp)malloc(sizeof(link_top));
    10. if(NULL==top){
    11. puts("top point malloc failed");
    12. return NULL;
    13. }
    14. top->front=NULL;
    15. top->rear=NULL;
    16. puts("top point create success");
    17. return top;
    18. }
    19. /*
    20. * function: 创新链表节点
    21. * @param [ in]
    22. * @param [out]
    23. * @return
    24. */
    25. linkp create(datatype data){
    26. linkp p=(linkp)malloc(sizeof(link));
    27. if(NULL==p){
    28. puts("link list point malloc failed");
    29. return NULL;
    30. }
    31. p->data=data;
    32. p->next=NULL;
    33. puts("link list point create success");
    34. return p;
    35. }
    36. /*
    37. * function: 空指针判定
    38. * @param [ in]
    39. * @param [out]
    40. * @return
    41. */
    42. int void_point(link_topp p){
    43. if(NULL==p){
    44. puts("null point pass");
    45. return -1;
    46. }
    47. return 0;
    48. }
    49. /*
    50. * function: 判空
    51. * @param [ in]
    52. * @param [out]
    53. * @return
    54. */
    55. int is_empty(link_topp p){
    56. if(NULL==p){
    57. puts("null point pass");
    58. return -1;
    59. }
    60. if(p->front==NULL){
    61. puts("stack is NULL");
    62. return 1;
    63. }
    64. return 0;
    65. }
    66. /*
    67. * function: 入队
    68. * @param [ in]
    69. * @param [out]
    70. * @return
    71. */
    72. int push(link_topp p,datatype data){
    73. if(void_point(p))
    74. return -1;
    75. linkp new=create(data);
    76. //入队
    77. if(p->front==NULL)
    78. p->front=new;
    79. if(p->rear!=NULL)
    80. p->rear->next=new;
    81. p->rear=new;
    82. // printf("t->front%d\n",p->front->data);
    83. // printf("t->rear%d\n",p->rear->data);
    84. // printf("t->front%p\n",p->front);
    85. puts("push success");
    86. return 0;
    87. }
    88. /*
    89. * function: 出队
    90. * @param [ in]
    91. * @param [out]
    92. * @return
    93. */
    94. int pull(link_topp p){
    95. if(void_point(p))
    96. return -1;
    97. //判空
    98. if(p->front==NULL){
    99. puts("link stack is null ,it's no deed to pull");
    100. return 0;
    101. }
    102. //出队
    103. linkp del=p->front;//记录出队指针,free
    104. datatype data=del->data;
    105. p->front=p->front->next;
    106. free(del);
    107. del=NULL;
    108. puts("pull success");
    109. return 0;
    110. }
    111. /*
    112. * function: 循环输出
    113. * @param [ in]
    114. * @param [out]
    115. * @return
    116. */
    117. int output(link_topp p){
    118. linkp i=p->front;//不移动原指针
    119. if(void_point(p))
    120. return -1;
    121. if(is_empty(p))
    122. return 0;
    123. while(i!=NULL){
    124. printf("<-%d",i->data);
    125. i=i->next;
    126. }
    127. puts("output success");
    128. return 0;
    129. }
    130. /*
    131. * function: 销毁
    132. * @param [ in]
    133. * @param [out]
    134. * @return
    135. */
    136. int del(link_topp p){
    137. if(void_point(p))
    138. return -1;
    139. linkp del;
    140. while(p->front!=NULL){
    141. del=p->front;
    142. p->front=p->front->next;
    143. free(del);
    144. }
    145. free(p);
    146. puts("del success");
    147. return 0;
    148. }

    main.c

    1. #include "head.h"
    2. //链表实现循环队列---尾插头删
    3. int main(int argc, const char *argv[])
    4. {
    5. link_topp t=create_top();
    6. push(t,11);
    7. push(t,12);
    8. push(t,13);
    9. push(t,14);
    10. printf("t->front%p\n",t->front);
    11. output(t);
    12. printf("t->front%p\n",t->front);
    13. pull(t);
    14. output(t);
    15. pull(t);
    16. output(t);
    17. pull(t);
    18. output(t);
    19. pull(t);
    20. output(t);
    21. pull(t);
    22. output(t);
    23. del(t);
    24. t=NULL;
    25. return 0;
    26. }

    运行效果:

    递归

    1. #include
    2. #include
    3. #include
    4. int fun(int a){
    5. printf("%d\n",a%10);
    6. a/=10;
    7. if(a>0)
    8. fun(a);
    9. return 0;
    10. }
    11. int main(int argc, const char *argv[])
    12. {
    13. //使用递归实现程序,输入一个数,输出该数的每个数
    14. int a=123456;
    15. fun(a);
    16. return 0;
    17. }

    今日思维导图

  • 相关阅读:
    港联证券:上市公司三季报反映经济回暖向好态势
    搭建一个Vue3+Ts+Vite项目
    开源的网易云音乐API项目都是怎么实现的?
    蓝桥等考Python组别二级004
    8.3 矢量图层点要素单一符号使用二
    Linux服务器操作常用命令
    创新案例|香氛DTC品牌Scentbird从0到月收百万美元的4大增长运营策略
    《OnJava》——11内部类
    Linux之shell文本搜索工具grep
    【Python】matplotlib画图
  • 原文地址:https://blog.csdn.net/weixin_53762703/article/details/132591591