数据结构严薇敏——队列的顺序存储(C语言)

缺乏、安全感 2022-05-18 01:52 277阅读 0赞

和栈相反,队列是一种先进先出的线性表。只能在表的一端进行插入,另一端进行删除。(类似与我们排队买东西,先到先得)

队列中,允许插入的一端叫做队尾,允许删除的一端叫队头。

代码实现 :

顺序数据结构定义为:

  1. typedef int ElemType;
  2. typedef struct SqQueue
  3. {
  4. ElemType data[MAX_SIZE];
  5. int size; //用来计数元素个数
  6. }SqQueue;

头文件:#include”SqQueueh.h”

  1. #ifndef SQQUEUEH_H_INCLUDED
  2. #define SQQUEUEH_H_INCLUDED
  3. #include<stdio.h>
  4. #include<stdlib.h>
  5. #define MAX_SIZE 20
  6. #define TRUE 1
  7. #define FALSE 0
  8. typedef int ElemType;
  9. typedef struct SqQueue
  10. {
  11. ElemType data[MAX_SIZE];
  12. int size;
  13. }SqQueue;
  14. //初始化一个空队
  15. SqQueue *Init_Queue();
  16. //销毁
  17. void Destory_Queue(SqQueue *S);
  18. //清空队列
  19. void Clear_Queue(SqQueue *S);
  20. //是否为空
  21. int IsEmpty_Queue(SqQueue *S);
  22. //队列的长度
  23. int Length_Queue(SqQueue *S);
  24. //返回队头元素
  25. ElemType GetHead_Queue(SqQueue *S);
  26. //进队
  27. void Enter_Queue(SqQueue *S,ElemType e);
  28. //出队
  29. void Delete_Queue(SqQueue *S);
  30. //遍历
  31. void Traverse_Queue(SqQueue *S);
  32. #endif // SQQUEUEH_H_INCLUDED

对头文件中函数的实现SqCQueue.c

  1. #include"SqQueueh.h"
  2. //以数组的起始位置作为最头,以结尾作为队尾
  3. //在队尾进行插入,在对头进行删除
  4. //初始化一个空队
  5. SqQueue *Init_Queue()
  6. {
  7. SqQueue *S = (SqQueue *)malloc(sizeof(SqQueue));
  8. int i;
  9. for(i = 0; i < MAX_SIZE; i++)
  10. {
  11. S->data[i] = 0;
  12. }
  13. S->size = 0;
  14. printf("队列初始化成功!\n");
  15. return S;
  16. }
  17. //销毁
  18. void Destory_Queue(SqQueue *S)
  19. {
  20. if(S != NULL)
  21. free(S);
  22. else
  23. return;
  24. printf("\n队列销毁成功!\n");
  25. }
  26. //清空队列
  27. void Clear_Queue(SqQueue *S)
  28. {
  29. if(IsEmpty_Queue(S))
  30. {
  31. printf("队列为空,不需要进行清空!\n");
  32. return;
  33. }
  34. int i;
  35. for( i = 0 ; i < S->size ; i++)
  36. {
  37. S->data[i] = 0;
  38. }
  39. S->size = 0;
  40. printf("\n清空队列成功!\n");
  41. }
  42. //是否为空
  43. int IsEmpty_Queue(SqQueue *S)
  44. {
  45. if(S->size == 0)
  46. return TRUE;
  47. else
  48. return FALSE;
  49. }
  50. //队列的长度
  51. int Length_Queue(SqQueue *S)
  52. {
  53. return S->size;
  54. }
  55. //返回队头元素
  56. ElemType GetHead_Queue(SqQueue *S)
  57. {
  58. return S->data[0];
  59. }
  60. //进队
  61. void Enter_Queue(SqQueue *S,ElemType e)
  62. {
  63. if( S == NULL)
  64. {
  65. printf("队列不存在,请创建一个队列!\n");
  66. return;
  67. }
  68. S->data[S->size] = e;
  69. S->size++;
  70. }
  71. //出队
  72. void Delete_Queue(SqQueue *S)
  73. {
  74. if(IsEmpty_Queue(S))
  75. {
  76. printf("队列为空,没有可以删除的元素!\n");
  77. return;
  78. }
  79. int i;
  80. for( i = 0; i < S->size; i++)
  81. {
  82. S->data[i] = S->data[i + 1];
  83. }
  84. S->data[i] = 0;
  85. S->size--;
  86. }
  87. //遍历
  88. void Traverse_Queue(SqQueue *S)
  89. {
  90. if(S == NULL)
  91. {
  92. printf("队列不存在!\n");
  93. return;
  94. }
  95. if(IsEmpty_Queue(S))
  96. {
  97. printf("队为空!\n");
  98. return;
  99. }
  100. int i;
  101. for(i = 0; i < S->size; i++)
  102. {
  103. printf("%d\t",S->data[i]);
  104. }
  105. printf("\n遍历队列成功!\n");
  106. }

主函数main.c

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include"SqQueueh.h"
  4. void test() //测试队列
  5. {
  6. SqQueue *S = Init_Queue();
  7. int i;
  8. for(i = 0; i < 5; i++)
  9. {
  10. Enter_Queue(S,i);
  11. }
  12. Traverse_Queue(S);
  13. printf("队列当前的长度为:%d\n",Length_Queue(S));
  14. Delete_Queue(S);
  15. Traverse_Queue(S);
  16. printf("队列当前的长度为:%d\n\n",Length_Queue(S));
  17. printf("当前的队头元素是:%d\n",GetHead_Queue(S));
  18. Clear_Queue(S);
  19. printf("队列当前的长度为:%d\n\n",Length_Queue(S));
  20. Destory_Queue(S);
  21. }
  22. int main()
  23. {
  24. test();
  25. system("pause");
  26. return 0;
  27. }

发表评论

表情:
评论列表 (有 0 条评论,277人围观)

还没有评论,来说两句吧...

相关阅读