赞
踩
循环队列是把顺序队列首尾相连,把存储队列元素的表从逻辑上看成一个环,成为循环队列。循环队列就是将队列存储空间的最后一个位置绕到第一个位置,形成逻辑上的环状空间,供队列循环使用。
- typedef struct CircleIntQueue
- {
- int data[TOTAL_SPACE];
-
- int head;
-
- int tail;
- }*CircleIntQueuePtr;
- CircleIntQueuePtr initQueue()
- {
- CircleIntQueuePtr resultPtr = (CircleIntQueuePtr)malloc(sizeof(struct CircleIntQueue));
- resultPtr->head = 0;
- resultPtr->tail = 0;
-
- return resultPtr;
- }
- void enqueue(CircleIntQueuePtr paraPtr, int paraValue)
- {
- if ((paraPtr->tail + 1) % TOTAL_SPACE == paraPtr->head) {
- printf("Queue full.\r\n");
- return;
- } // Of if
-
- paraPtr->data[paraPtr->tail % TOTAL_SPACE] = paraValue;
- paraPtr->tail++;
- }
- int dequeue(CircleIntQueuePtr paraPtr)
- {
- int resultValue;
- if (paraPtr->head == paraPtr->tail) {
- printf("No element in the queue.\r\n");
- return -1;
- } // Of if
-
- resultValue = paraPtr->data[paraPtr->head % TOTAL_SPACE];
- paraPtr->head++;
-
- return resultValue;
- }
- void outputLinkQueue(CircleIntQueuePtr paraPtr)
- {
- int i;
- if (paraPtr->head == paraPtr->tail)
- {
- printf("Empty queue.");
- return;
- } // Of if
-
- printf("Elements in the queue: ");
- for (i = paraPtr->head; i < paraPtr->tail; i++)
- {
- printf("%d, ", paraPtr->data[i % TOTAL_SPACE]);
- } // Of for i
-
- printf("\r\n");
- }
- void testLinkQueue()
- {
- int i = 10;
- CircleIntQueuePtr tempPtr = initQueue();
- for (; i < 16; i ++)
- {
- enqueue(tempPtr, i);
- }//Of for i
-
- outputLinkQueue(tempPtr);
-
- for (i = 0; i < 6; i ++)
- {
- printf("dequeue gets %d\r\n", dequeue(tempPtr));
- }//Of for i
-
- enqueue(tempPtr, 8);
- outputLinkQueue(tempPtr);
- }
- int main()
- {
- testLinkQueue();
- return 1;
- }
- Queue full.
- Queue full.
- Elements in the queue: 10, 11, 12, 13,
- dequeue gets 10
- dequeue gets 11
- dequeue gets 12
- dequeue gets 13
- No element in the queue.
- dequeue gets -1
- No element in the queue.
- dequeue gets -1
- Elements in the queue: 8,
- #include <stdio.h>
- #include <malloc.h>
-
- #define TOTAL_SPACE 5
-
- /**
- * Circle int queue.
- */
- typedef struct CircleIntQueue
- {
- int data[TOTAL_SPACE];
-
- int head;
-
- int tail;
- }*CircleIntQueuePtr;
-
- /**
- * Initialize the queue.
- */
- CircleIntQueuePtr initQueue()
- {
- CircleIntQueuePtr resultPtr = (CircleIntQueuePtr)malloc(sizeof(struct CircleIntQueue));
- resultPtr->head = 0;
- resultPtr->tail = 0;
-
- return resultPtr;
- }// Of the first constructor
-
- /**
- * Enqueue.
- *
- * @param paraValue The value of the new node.
- */
- void enqueue(CircleIntQueuePtr paraPtr, int paraValue)
- {
- if ((paraPtr->tail + 1) % TOTAL_SPACE == paraPtr->head) {
- printf("Queue full.\r\n");
- return;
- } // Of if
-
- paraPtr->data[paraPtr->tail % TOTAL_SPACE] = paraValue;
- paraPtr->tail++;
- }// Of enqueue
-
- /**
- * Dequeue.
- *
- * @return The value at the head.
- */
- int dequeue(CircleIntQueuePtr paraPtr)
- {
- int resultValue;
- if (paraPtr->head == paraPtr->tail) {
- printf("No element in the queue.\r\n");
- return -1;
- } // Of if
-
- resultValue = paraPtr->data[paraPtr->head % TOTAL_SPACE];
- paraPtr->head++;
-
- return resultValue;
- }// Of dequeue
-
- /**
- * Output the queue.
- */
- void outputLinkQueue(CircleIntQueuePtr paraPtr)
- {
- int i;
- if (paraPtr->head == paraPtr->tail)
- {
- printf("Empty queue.");
- return;
- } // Of if
-
- printf("Elements in the queue: ");
- for (i = paraPtr->head; i < paraPtr->tail; i++)
- {
- printf("%d, ", paraPtr->data[i % TOTAL_SPACE]);
- } // Of for i
-
- printf("\r\n");
- }//Of outputLinkQueue
-
- /**
- * Unit test.
- */
- void testLinkQueue()
- {
- int i = 10;
- CircleIntQueuePtr tempPtr = initQueue();
- for (; i < 16; i ++)
- {
- enqueue(tempPtr, i);
- }//Of for i
-
- outputLinkQueue(tempPtr);
-
- for (i = 0; i < 6; i ++)
- {
- printf("dequeue gets %d\r\n", dequeue(tempPtr));
- }//Of for i
-
- enqueue(tempPtr, 8);
- outputLinkQueue(tempPtr);
- }//Of testLinkQueue
-
- /**
- * The entrance.
- */
- int main()
- {
- testLinkQueue();
- return 1;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。