循环队列作业

#include <stdio.h>
#include <malloc.h>

#define TOTAL_SPACE 5

/**
 * Circle int queue.
 */
 typedef struct CircleIntQueue{
     int data[TOTAL_SPACE];
     
     int front;
     
     int rear;
 }*CircleIntQueuePtr;
 
 /**
 * Initialize the queue.
 */
 CircleIntQueuePtr initQueue(){
     CircleIntQueuePtr resultPtr = (CircleIntQueuePtr)malloc(sizeof(struct CircleIntQueue));
     resultPtr->front = 0;
     resultPtr->rear = 0;
     
     return resultPtr;
 }//of the first constructor
 
 /**
 * Enqueue.
 * 
 * @param paraValue The value of the new node.
 */
 void enqueue(CircleIntQueuePtr paraPtr, int paraValue){
     printf("Enqueue: %d", paraValue);
     if ((paraPtr->rear + 1) % TOTAL_SPACE == paraPtr->front){
         printf("Queue full.\r\n");
         return;
     }//of if
     
     paraPtr->data[paraPtr->rear] = paraValue;
     paraPtr->rear = (paraPtr->rear + 1) % TOTAL_SPACE;
 }//of enqueue
 
 /**
 * Dequeue.
 * 
 * @return The value at the front.
 */
 int dequeue(CircleIntQueuePtr paraPtr){
     int resultValue;
     if (paraPtr->front == paraPtr->rear){
         printf("No element in the queue.\r\n");
         return -1;
     }//of if
     
     resultValue = paraPtr->data[paraPtr->front];
     paraPtr->front = (paraPtr->front +1) % TOTAL_SPACE;
     
     return resultValue;
 }//of dequeue
 
 /**
 * Output the queue.
 */
 void outputCircleIntQueue(CircleIntQueuePtr paraPtr){
     int i;
     if(paraPtr->front == paraPtr->rear){
         printf("Empty queue.");
         return;
     }//of if
     
     printf("Elements in the queue: ");
     for (i = paraPtr->front; i != paraPtr->rear; i = (i + 1) % TOTAL_SPACE){
         printf("data[%d] = %d, ", i, paraPtr->data[i]);
     }//of for i
     
     printf("\r\n");
 }//of outputCircleIntQueue
 
 /**
 * Unit test.
 */
 void testCircleIntQueue(){
     int i = 10;
     CircleIntQueuePtr tempPtr = initQueue();
     for(; i<16; i++){
         enqueue(tempPtr, i);
     }//of for i
     
     outputCircleIntQueue(tempPtr);
     
     for (i = 0; i < 6; i ++){
         printf("dequeue gets %d\r\n", dequeue(tempPtr));
     }//of for i
     
     for(i = 3; i <6; i ++){
          enqueue(tempPtr, i);
     }//of for i
     for (i = 20; i < 30; i ++){
          enqueue(tempPtr, i);
          printf("dequeue gets %d\r\n", dequeue(tempPtr));
          outputCircleIntQueue(tempPtr);
     }//of for i 
 }//of testCircleIntQueue
 
 /**
 * The entrance.
 */
 int main(){
     testCircleIntQueue();
     return 1;
 }//of main

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值