赞
踩
一、头文件
- #include <stdio.h>
- #include <malloc.h>
-
- #define MAXSIZE 5
二、循环结点
- typedef struct CircleIntQueue
- {
- int data[MAXSIZE];
- int head;
- int tail;
- }*CircleIntQueuePtr;
三、初始化
- //初始化
- CircleIntQueuePtr initQueue()
- {
- CircleIntQueuePtr Queue = (CircleIntQueuePtr)malloc(sizeof(struct CircleIntQueue));
-
- Queue->head = 0;
- Queue->tail = 0;
-
- return Queue;
- }
四、插入
- //插入元素
- void insertQueue(CircleIntQueuePtr Queue,int paraData)
- {
- if((Queue->tail +1)%MAXSIZE == Queue->head)
- {
- printf("The queue is full.\n");
- return ;
- }
-
- Queue->data[(Queue->tail)%MAXSIZE] = paraData;
- Queue->tail++;
- }
五、删除
- //删除元素
- int deleteQueue(CircleIntQueuePtr Queue)
- {
- int value;
- //判断队列是否为空
- if(Queue->head == Queue->tail )
- {
- printf("The queue is empty.\n");
- return ;
- }
- value = Queue->data[Queue->head % MAXSIZE];
- Queue->head++;
-
- return value;
- }
六、打印
- //打印循环队列
- void printQueue(CircleIntQueuePtr Queue)
- {
- int i;
- //判断队列是否为空
- if(Queue->head == Queue->tail )
- {
- printf("The queue is empty.\n");
- return;
- }
- for(i = Queue->head; i < Queue->tail; i++)
- {
- printf("%d ", Queue->data[i % MAXSIZE]);
- }
- printf("\r\n");
- }
七、测试代码
- void testLinkQueue(){
- int i = 10;
- CircleIntQueuePtr tempPtr = initQueue();
- for (; i < 16; i ++) {
- insertQueue(tempPtr, i);
- }
-
- printQueue(tempPtr);
-
- for (i = 0; i < 6; i ++) {
- printf("dequeue gets %d\r\n", deleteQueue(tempPtr));
- }
-
- insertQueue(tempPtr, 8);
- printQueue(tempPtr);
- }
八、主函数
- int main(){
- testLinkQueue();
- return 1;
- }
九、全部代码
- #include <stdio.h>
- #include <malloc.h>
-
- #define MAXSIZE 5
-
- typedef struct CircleIntQueue
- {
- int data[MAXSIZE];
- int head;
- int tail;
- }*CircleIntQueuePtr;
-
- //初始化
- CircleIntQueuePtr initQueue()
- {
- CircleIntQueuePtr Queue = (CircleIntQueuePtr)malloc(sizeof(struct CircleIntQueue));
-
- Queue->head = 0;
- Queue->tail = 0;
-
- return Queue;
- }
- //插入元素
- void insertQueue(CircleIntQueuePtr Queue,int paraData)
- {
- if((Queue->tail +1)%MAXSIZE == Queue->head)
- {
- printf("The queue is full.\n");
- return ;
- }
-
- Queue->data[(Queue->tail)%MAXSIZE] = paraData;
- Queue->tail++;
- }
-
- //删除元素
- int deleteQueue(CircleIntQueuePtr Queue)
- {
- int value;
- //判断队列是否为空
- if(Queue->head == Queue->tail )
- {
- printf("The queue is empty.\n");
- return ;
- }
- value = Queue->data[Queue->head % MAXSIZE];
- Queue->head++;
-
- return value;
- }
- //打印循环队列
- void printQueue(CircleIntQueuePtr Queue)
- {
- int i;
- //判断队列是否为空
- if(Queue->head == Queue->tail )
- {
- printf("The queue is empty.\n");
- return;
- }
- for(i = Queue->head; i < Queue->tail; i++)
- {
- printf("%d ", Queue->data[i % MAXSIZE]);
- }
- printf("\r\n");
- }
- void testLinkQueue(){
- int i = 10;
- CircleIntQueuePtr tempPtr = initQueue();
- for (; i < 16; i ++) {
- insertQueue(tempPtr, i);
- }
-
- printQueue(tempPtr);
-
- for (i = 0; i < 6; i ++) {
- printf("dequeue gets %d\r\n", deleteQueue(tempPtr));
- }
-
- insertQueue(tempPtr, 8);
- printQueue(tempPtr);
- }
- int main(){
- testLinkQueue();
- return 1;
- }
十、测试结果
十一、总结
1、随着代码的运行,Queue->front和Queue->tail会不断增加,但是Queue的最大空间是 MAXSIZE,所以必须要整除。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。