• 使用链表实现队列操作


    代码如下:

    1. #include <stdio.h>
    2. #include <stdlib.h>
    3. #define NULL 0
    4. struct node
    5. {
    6. int data;
    7. struct node *next;
    8. };
    9. struct queue
    10. {
    11. struct node *front, *rear;
    12. };
    13. struct queue q;
    14. /*初始化队列首尾节点均为NULL*/
    15. void createqueue() { q.front = q.rear = NULL; }
    16. int empty()
    17. {
    18. if (q.front == NULL)
    19. return 1;
    20. else
    21. return 0;
    22. }
    23. /*插入节点*/
    24. void insert(int x)
    25. {
    26. struct node *pnode;
    27. pnode = (struct node *)malloc(sizeof(struct node));
    28. if (pnode == NULL)
    29. {
    30. printf("Memory overflow. Unable to insert.\n");
    31. exit(1);
    32. }
    33. pnode->data = x;
    34. pnode->next = NULL; /*新节点是最后一个节点*/
    35. if (empty())
    36. q.front = q.rear = pnode;
    37. else
    38. {
    39. (q.rear)->next = pnode;
    40. q.rear = pnode;
    41. }
    42. }
    43. /*删除节点*/
    44. int removes()
    45. {
    46. int x;
    47. struct node *p;
    48. if (empty())
    49. {
    50. printf("Queue Underflow. Unable to remove.\n");
    51. exit(1);
    52. }
    53. p = q.front;
    54. x = (q.front)->data;
    55. q.front = (q.front)->next;
    56. if (q.front == NULL)
    57. q.rear = NULL;
    58. free(p);
    59. return x;
    60. }
    61. /*显示节点*/
    62. void show()
    63. {
    64. struct node *p;
    65. if (empty())
    66. printf("Queue empty. No data to display \n");
    67. else
    68. {
    69. printf("Queue from front to rear is as shown: \n");
    70. p = q.front;
    71. while (p != NULL)
    72. {
    73. printf("%d ", p->data);
    74. p = p->next;
    75. }
    76. printf("\n");
    77. }
    78. }
    79. /*销毁队列*/
    80. void destroyqueue() { q.front = q.rear = NULL; }
    81. int main()
    82. {
    83. int x, ch;
    84. createqueue();
    85. do
    86. {
    87. printf("\n\n Menu: \n");
    88. printf("1:Insert \n");
    89. printf("2:Remove \n");
    90. printf("3:exit \n");
    91. printf("Enter your choice: ");
    92. scanf("%d", &ch);
    93. switch (ch)
    94. {
    95. case 1:
    96. printf("Enter element to be inserted: ");
    97. scanf("%d", &x);
    98. insert(x);
    99. show();
    100. break;
    101. case 2:
    102. x = removes();
    103. printf("Element removed is: %d\n", x);
    104. show();
    105. break;
    106. case 3:
    107. break;
    108. }
    109. } while (ch != 3);
    110. destroyqueue();
    111. return 0;
    112. }

    运行结果:

     Menu: 
    1:Insert 
    2:Remove 
    3:exit 
    Enter your choice: 1
    Enter element to be inserted: 99
    Queue from front to rear is as shown: 
    99 


      Menu: 
    1:Insert 
    2:Remove 
    3:exit 
    Enter your choice: 1
    Enter element to be inserted: 88
    Queue from front to rear is as shown: 
    99 88 


      Menu: 
    1:Insert 
    2:Remove 
    3:exit 
    Enter your choice: 1
    Enter element to be inserted: 11
    Queue from front to rear is as shown: 
    99 88 11 


      Menu: 
    1:Insert 
    2:Remove 
    3:exit 
    Enter your choice: 2
    Element removed is: 99
    Queue from front to rear is as shown: 
    88 11 
     

  • 相关阅读:
    HTTP&Tomcat
    GPPT阅读笔记
    用JQuery操作元素的style属性
    mescroll 在uni-app 运行的下拉刷新和上拉加载的组件
    辉芒微IO单片机FT60F022-RB
    如何看待unity新的收费模式
    【ES6】-- js类与类的继承
    神经网络 07(正则化)
    Rust的Slice切片
    springboot中的线程池
  • 原文地址:https://blog.csdn.net/qq_20490175/article/details/134021791