程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 循環隊列的實現

循環隊列的實現

編輯:關於C語言

 

  1. #ifndef SQQUEUE_H_INCLUDED 
  2. #define SQQUEUE_H_INCLUDED /* 防止重復包含 */ 
  3.  
  4. ////////////////////////////////////////// 
  5. //包含頭文件  
  6. #include <stdlib.h> 
  7. #include "ds.h" // OK, Status 等定義  
  8.  
  9. //數據元素的類型(缺省使用int型) 
  10. #ifndef ElemType 
  11. #define ElemType int 
  12. #define USE_DEFAULT_ELEMTYPE /* 使用缺省類型的標志 */ 
  13. #endif //ElemType 
  14.  
  15. ////////////////////////////////////////// 
  16. //循環隊列的存儲結構 
  17.  
  18. #define MAXQSIZE 500/* 循環隊列的最大容量 */ 
  19. typedef struct { 
  20.     /* TODO (#1#): 這裡完成循環隊列的類型定義 */ 
  21.     ElemType *base; 
  22.     int front; 
  23.     int rear; 
  24.     //.................................... 
  25. } SqQueue; 
  26.  
  27.  
  28. ////////////////////////////////////////// 
  29. //循環隊列的基本操作  
  30.  
  31. //構造一個空隊列Q 
  32. Status InitQueue(SqQueue &Q) 
  33. { 
  34.     /* TODO (#2#): 構造空隊列 */ 
  35.     Q.base=(ElemType*)malloc(MAXQSIZE *sizeof(ElemType)); 
  36.     if(!Q.base)exit(OVERFLOW); 
  37.     QQ.front=Q.rear =0; 
  38.     return OK; //TODO: 替換這行代碼,以下同 
  39.     //.................................... 
  40. } 
  41.  
  42. //銷毀隊列Q  
  43. //  前提:隊列Q已存在  
  44. Status DestroyQueue(SqQueue &Q) 
  45. { 
  46.     /* TODO (#3#): 銷毀隊列 */ 
  47.     free(Q.base); 
  48.     Q.base=NULL; 
  49.     Q.front=0; 
  50.     Q.rear=0; 
  51.     return OK; 
  52.     //.................................... 
  53. } 
  54.  
  55. //將隊列Q清為空隊列 
  56. //  前提:隊列Q已存在 
  57. Status ClearQueue(SqQueue &Q) 
  58. { 
  59.     /* TODO (#4#): 清空隊列 */ 
  60.     Q.base=0; 
  61.     Q.rear=0; 
  62.     return OK; 
  63.     //.................................... 
  64. } 
  65.  
  66. //若隊列Q為空,則返回TRUE,否則FALSE  
  67. //  前提:隊列Q已存在 
  68. Status QueueEmpty(SqQueue Q) 
  69. { 
  70.     /* TODO (#5#): 判斷隊列是否為空 */ 
  71.     if(Q.front==Q.rear) 
  72.         return OK; 
  73.     else 
  74.         return ERROR; 
  75.     //.................................... 
  76. } 
  77.  
  78. //返回隊列Q的元素個數,即隊列長度 
  79. //  前提:隊列Q已存在  
  80. int QueueLength(SqQueue Q) 
  81. { 
  82.     /* TODO (#6#): 返回隊列長度 */ 
  83.     return (Q.rear-Q.front+MAXQSIZE)%MAXQSIZE; 
  84.     //.................................... 
  85. } 
  86.  
  87. //取隊列Q頭元素用e返回 
  88. //  前提:隊列Q存在且非空 
  89. Status GetHead(SqQueue Q,ElemType &e) 
  90. { 
  91.     /* TODO (#7#): 取隊頭元素存入e */ 
  92.     if(Q.rear==Q.front) 
  93.         return ERROR; 
  94.     e=Q.base[Q.front]; 
  95.     //e=*(Q.base+Q.front); 
  96.     return OK;//返回操作狀態(成功:OK,失敗:ERROR) 
  97.     //.................................... 
  98. } 
  99.  
  100. //插入元素e作為隊列Q的新的隊尾元素 
  101. //  前提:隊列Q存在且未滿 
  102. Status EnQueue(SqQueue &Q, ElemType e) 
  103. { 
  104.     /* TODO (#8#): 元素e入隊列 */ 
  105.     if((Q.rear+1)%MAXQSIZE==Q.front) 
  106.         return ERROR; 
  107.     //e=*(Q.base +Q.rear); 
  108.     Q.base[Q.rear]=e; 
  109.     Q.rear=(Q.rear+1)%MAXQSIZE; 
  110.     return OK;//返回操作狀態(成功:OK,失敗:ERROR) 
  111.     //.................................... 
  112. } 
  113.  
  114. //刪除隊列Q的隊頭元素,並用e返回  
  115. //  前提:隊列Q存在且非空  
  116. Status DeQueue(SqQueue &Q, ElemType e) 
  117. { 
  118.     /* TODO (#9#): 出隊列存入e */ 
  119.     if(Q.front==Q.rear) 
  120.         return ERROR; 
  121.     //e=*(Q.base+Q.front); 
  122.     e=Q.base[Q.front]; 
  123.     Q.front=(Q.front+1)%MAXQSIZE; 
  124.     return OK;//返回操作狀態(成功:OK,失敗:ERROR) 
  125.     //.................................... 
  126. } 
  127.  
  128. ////////////////////////////////////////// 
  129.  
  130.  
  131. //TODO: 定義好 SqQueue 類型後使用 QueueView 函數  
  132. /****** //TODO: 刪除此行以便使用QueueView() 
  133. #include <stdio.h> 
  134. //查看隊列狀態(調試用) 
  135. void QueueView(SqQueue Q) 
  136. { 
  137.    extern void PrintElem(ElemType e);//打印數據用  
  138.    int i=0; 
  139.    if(Q.front<0||Q.front>=MAXQSIZE||Q.rear<0||Q.rear>=MAXQSIZE){ 
  140.        printf("隊列未初始化\n");  
  141.        return ; 
  142.    } 
  143.    printf("---Queue View---\n"); 
  144.    printf("front=%d , rear=%d\n", Q.front, Q.rear); 
  145.    if(Q.rear>=Q.front) { 
  146.        printf(".....   ......\n"); 
  147.        for(i=Q.front; i<Q.rear; i++) { 
  148.            printf("%5d\t", i); 
  149.            PrintElem(Q.base[i]); 
  150.            printf("\n"); 
  151.        } 
  152.        if(i<MAXQSIZE) printf(".....   ......\n"); 
  153.    } else {        
  154.        for(i=0; i<Q.rear; i++) { 
  155.            printf("%5d\t", i); 
  156.            PrintElem(Q.base[i]); 
  157.            printf("\n"); 
  158.        } 
  159.        printf(".....   ......\n"); 
  160.        for(i=Q.front; i<MAXQSIZE; i++) { 
  161.            printf("%5d\t", i); 
  162.            PrintElem(Q.base[i]); 
  163.            printf("\n"); 
  164.        } 
  165.    } 
  166.    printf("--- view end ---\n"); 
  167. } 
  168. ******/ //TODO: 刪除此行以便使用QueueView() 
  169.  
  170. //取消ElemType的默認定義,以免影響其它部分  
  171. #ifdef USE_DEFAULT_ELEMTYPE 
  172. #undef ElemType 
  173. #undef USE_EFAULT_ELEMTYPE 
  174. #endif 
  175.  
  176. #endif //SQQUEUE_H_INCLUDED 
  1. #include <stdio.h> 
  2. #include <stdlib.h> 
  3. #include "sqqueue.h" 
  4.  
  5. //初始化系統  
  6.  
  7.  
  8. void Finalize(SqQueue &q);    
  9.  
  10. //////////////////////////////////////////// 
  11. //主程序  
  12. int main() 
  13. { 
  14.     SqQueue q; //循環隊列  
  15.     int x;  
  16.      
  17.     //系統初始化 
  18.     InitQueue(q); 
  19.    printf("數據元素進隊列,以0結束"); 
  20.     scanf("%d",&x); 
  21.    while(x!=0){ 
  22.       EnQueue(q,x); 
  23.       scanf("%d",&x); 
  24.    } 
  25.    printf("\n隊列元素的個數"); 
  26.  
  27.    printf("%d",QueueLength(q)); 
  28.  
  29.  
  30.    printf("\n頭元素是:"); 
  31.    if(!QueueEmpty(q)){ 
  32.      if(GetHead(q,x)==OK) 
  33.       printf("%d",x); 
  34.    } 
  35.  
  36.  
  37.    printf("\n出隊列,先進先出"); 
  38.       if( DeQueue(q,x)==OK) 
  39.          printf("%d",x); 
  40.    printf("\n此時的對頭是:"); 
  41.    if(!QueueEmpty(q)){ 
  42.      if(GetHead(q,x)==OK) 
  43.       printf("%d\n",x); 
  44.    } 
  45.   
  46. } 

本文出自 “趙玉強的博客” 博客,請務必保留此出處http://zhaoyuqiang.blog.51cto.com/6328846/1179584

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved