• 数据结构2月22日


    题目:链表队列

    代码:

    main.c函数:

    1. #include
    2. #include "./03_linkQueue.h"
    3. int main(int argc, const char *argv[])
    4. {
    5. linkpos* pos = create_linkQueue();
    6. pushbyTail_linkQueue(pos,10);
    7. pushbyTail_linkQueue(pos,20);
    8. pushbyTail_linkQueue(pos,30);
    9. pushbyTail_linkQueue(pos,40);
    10. show_linkQueue(pos);
    11. return 0;
    12. }

    func.c函数:

    1. #include
    2. #include
    3. #include "./03_linkQueue.h"
    4. /*
    5. * function: 创建一个空队列
    6. * @param [ in]
    7. * @param [out]
    8. * @return
    9. */
    10. linkpos* create_linkQueue()
    11. {
    12. linkpos* pos=(linkpos*)malloc(sizeof(linkpos));
    13. pos->front=(linkQueue*)malloc(sizeof(linkQueue));
    14. if(pos->front==NULL)
    15. {
    16. printf("创建链队列失败\n");
    17. }
    18. pos->front->text.len=0;
    19. pos->front->next=NULL;
    20. pos->rear=pos->front;
    21. return pos;
    22. }
    23. /*
    24. * function: 尾插
    25. * @param [ in]
    26. * @param [out]
    27. * @return
    28. */
    29. void pushbyTail_linkQueue(linkpos* pos,dataType num)
    30. {
    31. linkQueue* temp =(linkQueue*)malloc(sizeof(linkQueue));
    32. if(temp==NULL)
    33. {
    34. printf("创建结点失败\n");
    35. }
    36. temp->text.data = num;
    37. temp->next=NULL;
    38. temp->next=NULL;
    39. pos->rear->next=temp;
    40. //更新rear的位置
    41. pos->rear=pos->rear->next;
    42. pos->front->text.len++;
    43. return;
    44. }
    45. /*
    46. * function: 头删
    47. * @param [ in]
    48. * @param [out]
    49. * @return
    50. */
    51. //判空
    52. int isEmpty(linkpos* pos)
    53. {
    54. return pos->rear==NULL ? 1:0;
    55. }
    56. dataType deletebyHead_linkQueue(linkpos* pos)
    57. {
    58. if(isEmpty(pos)==1)
    59. {
    60. printf("链队列为空,不可删除\n");
    61. }
    62. linkQueue* q=pos->front->next;
    63. pos->front->next=q->next;
    64. dataType num = q->text.data;
    65. free(q);
    66. q=NULL;
    67. return num;
    68. }
    69. /*
    70. * function: 遍历
    71. * @param [ in]
    72. * @param [out]
    73. * @return
    74. */
    75. void show_linkQueue(linkpos* pos)
    76. {
    77. while(pos->front->next!=NULL)
    78. {
    79. pos->front=pos->front->next;
    80. printf("%d ",pos->front->text.data);
    81. }
    82. printf("\n");
    83. return;
    84. }

    .h函数:

    1. #ifndef QUEUE_H_
    2. #define QUEUE_H_
    3. typedef int dataType;
    4. union msg
    5. {
    6. dataType data;
    7. int len;
    8. };
    9. typedef struct node
    10. {
    11. union msg text;
    12. struct node* next;
    13. }linkQueue;
    14. typedef struct
    15. {
    16. linkQueue* front;
    17. linkQueue* rear;
    18. }linkpos;
    19. linkpos* create_linkQueue();
    20. void pushbyTail_linkQueue(linkpos* pos,dataType num);
    21. dataType deletebyHead_linkQueue(linkpos* pos);
    22. void show_linkQueue(linkpos* pos);
    23. #endif

    运行结果:

  • 相关阅读:
    【开发篇】十、Spring缓存:手机验证码的生成与校验
    DO LARGE LANGUAGE MODELS KNOW ABOUT FACTS?
    基于JAVA的TCP网络QQ聊天工具系统
    【前端面试常见问题】防抖(Debounce)与节流(Throttle)
    【Flask 系统教程 7】数据库使用 SQLAlchemy
    【Linux】 man命令使用
    python 多线程
    Aspose.Email for Node.js via .NET
    【查找、排序~python】
    c#学习_第三弹
  • 原文地址:https://blog.csdn.net/m0_67459505/article/details/136243865