第四篇:基本数据结构——队列的顺序表示

以下为操作队列的算法,该队列为静态队列,用循环数组实现。
给该队列分配的内存长度为len+1,但实际只用了len个内存空间来保存数据,这样做是为了更方便判断队列的满与空。队列中front位置中存放的是队首的数据,rear位置的前一个位置中存放队尾的数据,而rear位置中则没有数据存放,这样做的目的是为了在入队和出队时方便对队列的操作,而不用考虑特殊情况

操作系统:ubuntu
编译软件:gcc
结果截图:
源代码:
[cpp]  view plain  copy
  1. #include  
  2. #include  
  3. #include  
  4.   
  5.   
  6. int len;//全局变量,静态队列的实际有效长度,即实际存放了len个数  
  7. typedef struct Queue  
  8. {  
  9. int *pBase;  
  10. int front;  
  11. int rear;  
  12. } QUEUE,*PQUEUE;  
  13.   
  14.   
  15. PQUEUE creat_queue();  
  16. bool enter_queue(PQUEUE,int);  
  17. bool full_queue(PQUEUE);  
  18. bool empty_queue(PQUEUE);  
  19. void traverse_queue(PQUEUE);  
  20. bool out_queue(PQUEUE,int *);  
  21.   
  22.   
  23. int main()  
  24. //创建静态队列,并定义出队数据的保存在变量data_save中  
  25. int data_save;  
  26. PQUEUE pQueue = creat_queue();  
  27. //将数据入队,并遍历输出队列中的数据  
  28. enter_queue(pQueue,1);  
  29. enter_queue(pQueue,2);  
  30. enter_queue(pQueue,3);  
  31. enter_queue(pQueue,4);  
  32.         traverse_queue(pQueue);  
  33. //将数据出队,并遍历输出队列中的数据  
  34. out_queue(pQueue,&data_save);  
  35.         traverse_queue(pQueue);  
  36.   
  37.   
  38. return 0;  
  39. }  
  40.   
  41.   
  42. //创建并初始化一个空的静态队列,返回指向该队列的指针,此时首尾在队列同一位置处  
  43.   
  44.   
  45. PQUEUE creat_queue()  
  46. {  
  47. printf("Input the length of the queue:\n");  
  48. scanf("%d",&len);  
  49. PQUEUE pQueue = (PQUEUE)malloc(sizeof(QUEUE));  
  50. pQueue->pBase = (int *)malloc(sizeof(int)*(len+1));  
  51. if(NULL==pQueue || NULL==pQueue->pBase)  
  52. {  
  53.   printf("malloc failed!");  
  54.   exit(-1);  
  55. }  
  56. else  
  57. {  
  58.   pQueue->front = 0;  
  59.   pQueue->rear = 0;  
  60. }  
  61. return pQueue;  
  62. }  
  63.   
  64.   
  65. //判断该静态队列是否满  
  66. bool full_queue(PQUEUE pQueue)  
  67. {  
  68. if(pQueue->front == (pQueue->rear+1)%(len+1))  
  69.   return true;  
  70. else  
  71.   return false;  
  72. }  
  73.   
  74.   
  75. //判断该静态队列是否空  
  76. bool empty_queue(PQUEUE pQueue)  
  77. {  
  78. if(pQueue->front == pQueue->rear)  
  79.   return true;  
  80. else  
  81.   return false;  
  82. }  
  83.   
  84.   
  85. //将变量val从队尾入队  
  86. bool enter_queue(PQUEUE pQueue,int val)  
  87. {  
  88. if(full_queue(pQueue))  
  89.   return false;  
  90. else  
  91. {  
  92.   pQueue->pBase[pQueue->rear] = val;  
  93.   pQueue->rear = (pQueue->rear+1)%(len+1);  
  94.   return true;  
  95. }  
  96. }  
  97.   
  98.   
  99. //将数据出队,并将其保存在out_data指针指向的位置  
  100. bool out_queue(PQUEUE pQueue,int *out_data)  
  101. {  
  102. if(empty_queue(pQueue))  
  103.   return false;  
  104. else  
  105. {  
  106.   *out_data = pQueue->front;  
  107.       pQueue->front = (pQueue->front+1)%(len+1);  
  108.   return true;  
  109. }  
  110. }  
  111.   
  112.   
  113. //遍历该静态队列,并自队首向队尾输出队列中的数据  
  114.   
  115.   
  116. void traverse_queue(PQUEUE pQueue)  
  117. {  
  118. int i = pQueue->front;  
  119. printf("now datas in the queue are:\n");  
  120. while(i != pQueue->rear)  
  121. {  
  122.   printf("%d ",pQueue->pBase[i]);  
  123.   i = (i+1)%(len+1);  
  124. }  
  125. printf("\n");  
  126. return ;  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值