• 2.27数据结构


    1.链队

    1. //link_que.c
    2. #include "link_que.h"
    3. //创建链队
    4. Q_p create_que()
    5. {
    6. Q_p q = (Q_p)malloc(sizeof(Q));
    7. if(q==NULL)
    8. {
    9. printf("空间申请失败\n");
    10. return NULL;
    11. }
    12. node_p L=(node_p)malloc(sizeof(node));
    13. if(L==NULL)
    14. {
    15. printf("申请空间失败\n");
    16. return NULL;
    17. }
    18. L->next=NULL;
    19. q->front = L;
    20. q->rear = L;
    21. return q;
    22. }
    23. //创建结点
    24. node_p create_node()
    25. {
    26. node_p new=(node_p)malloc(sizeof(node));
    27. if(new==NULL)
    28. {
    29. printf("申请空间失败\n");
    30. return NULL;
    31. }
    32. new->data=0;
    33. new->next=NULL;
    34. return new;
    35. }
    36. //判空
    37. int empty_que(Q_p q)
    38. {
    39. if(q==NULL)
    40. {
    41. printf("入参为空\n");
    42. return -1;
    43. }
    44. return q->front==NULL?1:0;
    45. }
    46. //入队
    47. void push_que(Q_p q,datatype e)
    48. {
    49. if(q==NULL)
    50. {
    51. printf("入参为空\n");
    52. return;
    53. }
    54. node_p s=create_node();
    55. s->data=e;
    56. q->rear->next=s;
    57. q->rear=s;
    58. }
    59. //出队
    60. void pop_que(Q_p q)
    61. {
    62. if(q==NULL)
    63. {
    64. printf("入参为空\n");
    65. return;
    66. }
    67. if(empty_que(q))
    68. {
    69. printf("队为空\n");
    70. return;
    71. }
    72. if(q->front->next==q->rear)
    73. {
    74. q->rear==q->front;
    75. }
    76. node_p p=q->front->next;
    77. printf("出队元素为:%d\n",p->data);
    78. q->front->next=p->next;
    79. free(p);
    80. p=NULL;
    81. }
    82. //打印
    83. void show(Q_p q)
    84. {
    85. if(q==NULL)
    86. {
    87. printf("入参为空\n");
    88. return;
    89. }
    90. if(empty_que(q))
    91. {
    92. printf("队为空\n");
    93. return;
    94. }
    95. node_p p=q->front;
    96. while(p->next!=NULL)
    97. {
    98. p=p->next;
    99. printf("%d\n",p->data);
    100. }
    101. }
    102. //销毁
    103. void free_que(Q_p *q)
    104. {
    105. if(q==NULL||*q==NULL)
    106. {
    107. printf("入参为空\n");
    108. return;
    109. }
    110. free(*q);
    111. *q=NULL;
    112. }
    1. //link_que.h
    2. #ifndef __LINK_QUE_H__
    3. #define __LINK_QUE_H__
    4. #include
    5. #include
    6. typedef int datatype;
    7. typedef struct node
    8. {
    9. datatype data;
    10. struct node *next;
    11. }node,*node_p;
    12. typedef struct Q
    13. {
    14. node_p front;
    15. node_p rear;
    16. }Q,*Q_p;
    17. //创建链队
    18. Q_p create_que();
    19. //创建结点
    20. node_p create_node();
    21. //判空
    22. int empty_que(Q_p q);
    23. //入队push_que
    24. void push_que(Q_p q,datatype e);
    25. //出队pop_que
    26. void pop_que(Q_p q);
    27. //打印
    28. void show(Q_p q);
    29. //销毁
    30. void free_que(Q_p *q);
    31. #endif
    1. //main.c
    2. #include "link_que.h"
    3. int main()
    4. {
    5. Q_p q = create_que();
    6. push_que(q,90);
    7. push_que(q,12);
    8. push_que(q,6);
    9. show(q);
    10. putchar(10);
    11. pop_que(q);
    12. putchar(10);
    13. show(q);
    14. putchar(10);
    15. push_que(q,1);
    16. push_que(q,7);
    17. push_que(q,5);
    18. push_que(q,4);
    19. push_que(q,3);
    20. push_que(q,2);
    21. show(q);
    22. putchar(10);
    23. free_que(&q);
    24. show(q);
    25. return 0;
    26. }

    2.二叉树的遍历

    1. //tree.c
    2. #include "tree.h"
    3. //创建结点
    4. tree_p create_node(char data)
    5. {
    6. tree_p new = (tree_p)malloc(sizeof(tree));
    7. if(new==NULL)
    8. {
    9. printf("空间申请失败\n");
    10. return NULL;
    11. }
    12. new->data=data;
    13. return new;
    14. }
    15. //创建二叉树
    16. tree_p create_tree()
    17. {
    18. char data = '\0';
    19. scanf("%c",&data);
    20. getchar();
    21. if(data=='#')
    22. return NULL;
    23. tree_p T = create_node(data);
    24. T->lch=create_tree();
    25. T->rch=create_tree();
    26. return T;
    27. }
    28. //先序遍历,根左右
    29. void pri(tree_p T)
    30. {
    31. if(T==NULL)
    32. return;
    33. printf("%c->",T->data);
    34. pri(T->lch);
    35. pri(T->rch);
    36. }
    37. //中序遍历,左根右
    38. void mid(tree_p T)
    39. {
    40. if(T==NULL)
    41. return;
    42. mid(T->lch);
    43. printf("%c->",T->data);
    44. mid(T->rch);
    45. }
    46. //后序遍历,左右根
    47. void last(tree_p T)
    48. {
    49. if(T==NULL)
    50. return;
    51. last(T->lch);
    52. last(T->rch);
    53. printf("%c->",T->data);
    54. }
    1. //tree.h
    2. #ifndef __TREE_H__
    3. #define __TREE_H__
    4. #include
    5. #include
    6. typedef struct tree_node
    7. {
    8. char data;
    9. struct tree_node *lch;
    10. struct tree_node *rch;
    11. }tree,*tree_p;
    12. //创建结点
    13. tree_p create_node(char data);
    14. //创建二叉树
    15. tree_p create_tree();
    16. //先序遍历,根左右
    17. void pri(tree_p T);
    18. //中序遍历,左根右
    19. void mid(tree_p T);
    20. //后序遍历,左右根
    21. void last(tree_p T);
    22. #endif
    1. //main.c
    2. #include "tree.h"
    3. int main()
    4. {
    5. tree_p T=create_tree();
    6. printf("先序遍历\n");
    7. pri(T);
    8. putchar(10);
    9. printf("中序遍历\n");
    10. mid(T);
    11. putchar(10);
    12. printf("后序遍历\n");
    13. last(T);
    14. putchar(10);
    15. return 0;
    16. }

    3.思维导图

  • 相关阅读:
    再肝3天,整理了90个 NumPy 例子,不能不收藏!
    STM32输出SPWM波,HAL库,cubeMX配置,滤波后输出1KHz正弦波
    Python|Pyppeteer自动获取二手车平台卖家联系方式(22)
    数据库的数据类型
    Linux常用命令
    π220N31兼容代替TI ISO1540DR 低功耗 3.0kVrms 双向I2C 隔离器
    【软件测试】--功能测试4-html介绍
    1113: 递归调用的次数统计(函数专题)
    Java创建对象的最佳方式:单例模式(Singleton)
    分享10套开源免费的高品质源码,免费源码下载平台
  • 原文地址:https://blog.csdn.net/m0_54964794/article/details/136326060