【数据结构】 链式队列的一些操作

前端之家收集整理的这篇文章主要介绍了【数据结构】 链式队列的一些操作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

详细见代码

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct ElemType
  5. {
  6. int data;
  7. ElemType *next;
  8. }ElemType; // next 是 元素的指针 指向下一个元素的位置
  9.  
  10. typedef struct
  11. {
  12. ElemType *front;
  13. ElemType *rear;
  14. }Queue,*PsQueue; // 队列头指针 和 队列尾指针
  15.  
  16.  
  17. void initQueue(Queue * sq); // 初始化
  18. void DestoryQueue (Queue * sq); //销毁队列
  19. void EnQueue (Queue * sq,int e); //进队列
  20. void DeQueue (Queue * sq); //出队列
  21. void GetHead (Queue * sq); //取队头元素值
  22. bool QueueEmpty (Queue sq); //判队列空否
  23. void DisQueue(Queue * sq); // 遍历队列中元素
  24.  
  25. void main()
  26. {
  27. int size = 0;
  28. char choice = '\0';
  29. Queue sQueue = {0,};
  30. PsQueue PsQueue = &sQueue;
  31.  
  32. initQueue(PsQueue);
  33.  
  34. while (choice != 'q')
  35. {
  36. printf("******************************\n");
  37. printf("--- 1 元素进队 --- \n");
  38. printf("--- 2 队头出队 --- \n");
  39. printf("--- 3 遍历队中元素 --- \n");
  40. printf("--- 4 判队列空否 --- \n");
  41. printf("--- 5 取队头元素值 --- \n");
  42. printf("--- 6 销毁队列 --- \n");
  43. printf("--- q 退出 --- \n");
  44. printf("******************************\n");
  45.  
  46. scanf("\n%c",&choice);
  47.  
  48. switch (choice)
  49. {
  50. case '1':
  51. printf("输入进栈元素个数:");
  52. scanf("%d",&size);
  53.  
  54. for (int i = 0; i < size; i++)
  55. EnQueue (&sQueue,i+1) ; //进队列
  56. break;
  57. case '2':
  58. DeQueue(&sQueue);
  59. break;
  60. case '3':
  61. DisQueue(&sQueue);
  62. break;
  63. case '4':
  64. QueueEmpty(sQueue);
  65. break;
  66. case '5':
  67. GetHead(&sQueue);
  68. break;
  69. case '6':
  70. DestoryQueue(&sQueue);
  71. break;
  72. case 'q':
  73. return;
  74.  
  75. }
  76. }
  77. }
  78.  
  79. void initQueue(Queue * sq) // 初始化
  80. {
  81. sq->rear = sq->front = (ElemType *)malloc(sizeof(ElemType)); // 指向同一处内存空间
  82. }
  83.  
  84.  
  85. void DestoryQueue (Queue * sq) //销毁队列
  86. {
  87. while (sq->front->next != NULL)
  88. {
  89. ElemType *p = sq->front->next;
  90. sq->front->next = sq->front->next->next;
  91. free(p);
  92. }
  93.  
  94. free(sq->front);
  95. }
  96.  
  97.  
  98. void EnQueue (Queue * sq,int e) //进队列
  99. {
  100. sq->rear->next = (ElemType *)malloc(sizeof(ElemType));
  101. sq->rear = sq->rear->next;
  102.  
  103. sq->rear->data = e;
  104. sq->rear->next = NULL;
  105. }
  106.  
  107.  
  108. void DeQueue (Queue * sq) //出队列
  109. {
  110. // 队头出队
  111. if (sq->front == sq->rear)
  112. {
  113. printf("队列为空,不能出队~\n");
  114. return;
  115. }
  116. else
  117. {
  118. ElemType *p =sq->front->next;
  119. printf("出队元素为 %d\n",p->data);
  120.  
  121. sq->front->next = sq->front->next->next;
  122. free(p);
  123. }
  124. }
  125.  
  126.  
  127. void GetHead (Queue * sq) //取队头元素值
  128. {
  129. printf("队头元素为 %d\n",sq->front->next->data);
  130. }
  131.  
  132.  
  133. bool QueueEmpty (Queue sq) //判队列空否
  134. {
  135. if (sq.front == sq.rear)
  136. {
  137. printf("队列为空~\n");
  138. return true;
  139. }
  140. else
  141. {
  142. printf("队列不为空~\n");
  143. return false;
  144. }
  145. }
  146.  
  147.  
  148. void DisQueue(Queue * sq) // 遍历队列中元素
  149. {
  150. ElemType * p = sq->front;
  151.  
  152. while (p->next != NULL)
  153. {
  154. p = p->next;
  155. printf("%d\t",p->data);
  156. }
  157.  
  158. printf("\n");
  159. }

猜你在找的数据结构相关文章