C語言實現循環隊列


今日在處理數據存儲的問題中,數據占用的空間較大,在詢問之下,提及循環隊列。

沒有學習過的我,想想就是頭大,只能慢慢從網上找資料,一個字母一個字母的敲,最后,還是慢慢的對隊列有了一些理解

對於循環隊列有幾個操作:

1、初始化

2、入隊

3、出隊

4、遍歷隊列

5、判隊列空,判隊列滿

 

具體如何實現,我會在下面通過代碼實現

在對循環隊列操作之前,先要建立隊列結構體元素,

1 typedef struct Queue
2 {
3     int * BUF;
4     int front;
5     int rear;
6 }QUEUE;

 

1、初始化

  初始化,需要完成的工作是,為新建的隊列分配內存空間,然后在將頭尾指針置零

1 void initQueue(QUEUE *queue_q)
2 {
3     queue_q->BUF = (int *)malloc(sizeof(int)*BUF_SIZE);
4     if(queue_q->BUF != NULL)     //隊列內存分配成功
5     {
6         queue_q->front = queue_q->rear = 0; //初始化頭尾指針 
7     }
8    
9 }

2、入隊

入隊主要是將數據放到內存中,但是應該放到那一段內存,這就是一個問題了,

在此,在入隊的時候,循環隊列的頭指針不做動作,尾指針向后偏移

實現代碼如下:

其中的 BUF_SIZE 為循環隊列的空間大小嗎,但是實際能存儲的數據字節數是(BUF_SIZE - 1)

#define BUF_SIZE 8

 

1 void In_Queue(QUEUE *queue_q , int value)
2 {
3     if(is_fullQueue(queue_q) != true)        //隊列未滿
4     {
5         queue_q->BUF[queue_q->rear] = value;
6         queue_q->rear = (queue_q->rear + 1)%BUF_SIZE ;    //尾指針偏移 
7     }
8 }

細心的人會注意到函數 is_fullQueue(queue_q) ,這是對循環隊列進行判斷,看是不是滿了,應該隊列的空間是有限的,對於滿的隊列,無法進行數據入隊操作

具體函數如下:

1 unsigned char is_fullQueue(QUEUE *queue_q)
2 {
3     if((queue_q->rear +1)%BUF_SIZE == queue_q->front)
4     {
5         return true;
6     }else
7         return false;
8 }

 

同樣,存在一個判空函數,函數的原理是:頭指針 = 尾指針

實現代碼如下:

1 unsigned char isemptyQueue(QUEUE *queue_q)
2 {
3     if(queue_q->front == queue_q->rear)
4     {
5         return true;
6     }
7     else
8         return false;
9 }

 

3、出隊

出隊是將頭指針位置下的數據取出來,然后頭指針偏移到被取數據的位置

代碼實現如下:

1  void out_Queue(QUEUE *queue_q , int *value)
2  {
3      if(isemptyQueue(queue_q) != true)        //隊列未空
4      {
5         *value = queue_q->BUF[queue_q->front];
6         queue_q->front = (queue_q->front + 1)%BUF_SIZE ;
7      }
8 }

入隊要判滿,出隊則要判空。

因為空的隊列,沒辦法取數據

4、遍歷隊列

這就是一個簡單的打印函數,沒什么好說的

唯一需要注意的就是,遍歷是出隊操作,操作的是頭指針,若頭指針 = 尾指針,遍歷完畢,循環隊列為空

 1 void bianli_a(QUEUE *queue_q)
 2 {
 3     if(isemptyQueue(queue_q) != true)
 4     {
 5         int ret=queue_q->front;
 6         while(ret != queue_q->rear)
 7         { 
 8             printf("%d  ",queue_q->BUF[ret]);
 9             ret=(ret+1)%BUF_SIZE;
10         }
11     }
12 }

 

下面是我學習循環隊列的時候,寫的代碼,若有指教,請評論:

 1 #include <stdio.h>
 2 #include <malloc.h>
 3 #include <stdlib.h>
 4 
 5 #define true 1
 6 #define false 0
 7 #define BUF_SIZE 8
 8 typedef struct Queue
 9 {
10     int * BUF;
11     int front;
12     int rear;
13 }QUEUE;
14 
15 void initQueue(QUEUE *queue_q)
16 {
17     queue_q->BUF = (int *)malloc(sizeof(int)*BUF_SIZE);
18     if(queue_q->BUF != NULL)     //隊列內存分配成功
19     {
20         queue_q->front = queue_q->rear = 0; //初始化頭尾指針 
21     }
22    
23 }
24 
25 //判空
26 unsigned char isemptyQueue(QUEUE *queue_q)
27 {
28     if(queue_q->front == queue_q->rear)
29     {
30         return true;
31     }
32     else
33         return false;
34 }
35  
36 //判滿
37 unsigned char is_fullQueue(QUEUE *queue_q)
38 {
39     if((queue_q->rear +1)%BUF_SIZE == queue_q->front)
40     {
41         return true;
42     }else
43         return false;
44 }
45 
46 //入隊
47  
48 void In_Queue(QUEUE *queue_q , int value)
49 {
50     if(is_fullQueue(queue_q) != true)        //隊列未滿
51     {
52         queue_q->BUF[queue_q->rear] = value;
53         queue_q->rear = (queue_q->rear + 1)%BUF_SIZE ;    //尾指針偏移 
54     }
55 }
56  
57 
58 //出隊 
59  void out_Queue(QUEUE *queue_q , int *value)
60  {
61      if(isemptyQueue(queue_q) != true)        //隊列未空
62      {
63         *value = queue_q->BUF[queue_q->front];
64         queue_q->front = (queue_q->front + 1)%BUF_SIZE ;
65      }
66 }
67 
68 void bianli_a(QUEUE *queue_q)
69 {
70     if(isemptyQueue(queue_q) != true)
71     {
72         int ret=queue_q->front;
73         while(ret != queue_q->rear)
74         { 
75             printf("%d  ",queue_q->BUF[ret]);
76             ret=(ret+1)%BUF_SIZE;
77         }
78     }
79 }
80 
81 int  main()
82 {
83     int val;
84     QUEUE m;
85     initQueue(&m);
86     In_Queue(&m,1);
87     In_Queue(&m,2);
88     In_Queue(&m,3);
89     bianli_a(&m);
90     printf("\n");
91     out_Queue(&m,&val);
92     bianli_a(&m); 
93     return 0;
94 }

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM