⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cirque.c

📁 实现循环队列的小代码,可以加在自己的代码中,接口十分友好
💻 C
字号:
#include <memory.h>
#include "cirque.h"
/**置队空
*
*
*/
void InitQueue(CirQue *Q, int quesize)
{
	Q->q_size = quesize;    /*size of the queue*/
	Q->q_data = malloc(sizeof(QueDataType) * quesize);
	if(Q->q_data == NULL)
	{
		perror("malloc error!\n");
	}
	Q->q_front = Q->q_rear = 0;
	Q->q_count = 0;     //计数器置0
}
/**消除队列
*
*
*/
void DestQueue(CirQue *Q)
{
	if(Q->q_data == NULL)
	{
		return;
	}
	free(Q->q_data);
	Q->q_size = 0;
}
/**判队空
*
*
*/ 
inline int QueueEmpty(CirQue *Q)
{
	return Q->q_count == 0;  //队列无元素为空
}

/**判队满
*
*
*/
inline int QueueFull(CirQue *Q)
{
	return Q->q_count == Q->q_size;  //队中元素个数等于QueueSize时队满
}

/** 入队
*
*
*/
void EnQueue(CirQue *Q, QueDataType x)
{
	if(QueueFull(Q))                   
		perror("Queue overflow");     //队满上溢
	Q->q_count++;                        //队列元素个数加1
	Q->q_data[Q->q_rear] = x;                 //新元素插入队尾
	Q->q_rear=(Q->q_rear+1) % Q->q_size;      //循环意义下将尾指针加1
}

/**出队
*
*
*/
QueDataType DeQueue(CirQue *Q)
{
	QueDataType temp;
	if(QueueEmpty(Q))
		perror("Queue underflow!\n");     //队空下溢
	temp = Q->q_data[Q->q_front];
	Q->q_count--;                        //队列元素个数减1
	Q->q_front=(Q->q_front+1) % Q->q_size;   //循环意义下的头指针加1
	return temp; 
}

/**取队头元素
*
*
*/
QueDataType QueueFront(CirQue *Q)
{
	if(QueueEmpty(Q))
		perror("Queue is empty!\n");
	return Q->q_data[Q->q_front];
}

#ifdef CIRQUE_TEST
int main(int argc, char **argv)
{
   CirQue Q;
   int i;
   QueDataType ch;
   
   InitQueue(&Q, 20);   /*malloc some space for the queue*/
   for(i = 0; i < 20; i++)
   {
      /*write data to the queqe*/
      EnQueue(&Q, (i+65));
   }
   
   for(i = 0; i < 20; i++)
   {
      /*read data from the queue*/
      ch = DeQueue(&Q);
      printf("%c ", ch);
      if(i%10==0 && i!=0)
         printf("\n");
   }
   /*destroy the queque, free the space the queue used*/
   DestQueue(&Q);
   return 0;
}
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -