// circqueue.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
//循环队列的实现(多采用静态分配实现)
#define MAX_SIZE 20
typedef int QElemType;
typedef struct{
QElemType elem[MAX_SIZE];
int front,rear;
}CircQueue;
//初始化队列
void InitQueue(CircQueue & Q )
{
Q.front = Q.rear = 0;
}
//判断队列是否满
bool IsFull( CircQueue & Q )
{
return ((Q.rear +1 ) % MAX_SIZE == Q.front )?true:false; //队满其实还是空了一个元素。主要是为了和空队区分开来
}
//对是否空
bool IsEmpty( CircQueue & Q )
{
return (Q.rear == Q.front )?true:false;
}
//入队
bool EnQueue( CircQueue & Q, QElemType &x )
{
if(IsFull(Q))return false;
Q.elem[Q.rear] = x;
Q.rear = (Q.rear+1) % MAX_SIZE;
printf("入队 = %d\n",x);
return true;
}
//出队
bool DeQueue( CircQueue & Q, QElemType &x )
{
if( IsEmpty( Q ) ) return false;
x = Q.elem[Q.front];
Q.front = (Q.front+1) % MAX_SIZE;
printf("出队=%d\n",x);
return true;
}
//得到队头
bool GetFront( CircQueue & Q , QElemType & x)
{
if( IsEmpty( Q ) ) return false;
x = Q.elem[Q.front];
return true;
}
//得到队大小
int GetSize( CircQueue & Q )
{
return (Q.rear - Q.front +MAX_SIZE ) % MAX_SIZE;
}
int _tmain(int argc, _TCHAR* argv[])
{
CircQueue Q;
InitQueue( Q );
for(int i = 0;i<29;i++)
{
EnQueue(Q,i);
}
int x;
for(int i = 0;i<29;i++)
{
DeQueue(Q,x);
}
getchar();
return 0;
}
c循环队列简单实现
最新推荐文章于 2020-07-09 16:38:10 发布