首页 > 代码库 > 循环队列

循环队列

#include "stdafx.h"#include <iostream>using namespace std;typedef int DataType;#define MAXSIZE 5typedef struct{	DataType *base;	int front;	int rear;}CircleQueue;CircleQueue *init(CircleQueue *queue){	queue->base = (DataType*)malloc(MAXSIZE*sizeof(DataType));	if(!queue) exit(0);	queue->rear = queue->front =0;	return queue;}int getLength(CircleQueue *queue){	return (queue->rear - queue->front +MAXSIZE)%MAXSIZE;}void inQueue(CircleQueue *queue,DataType e){	if((queue->rear+1)%MAXSIZE ==queue->front)	{		cout<<"the queue is full"<<endl;		exit(0);	}else	{		queue->base[queue->rear] = e;		queue->rear = (queue->rear+1)%MAXSIZE;	}}void outQueue(CircleQueue *queue){	if(queue->front == queue->rear)	{		cout<<"the queue is empty"<<endl;		exit(0);	}else	{		cout<<"the out element is :"<<queue->base[queue->front];		queue->front= (queue->front+1)%MAXSIZE;	}}void main(){	CircleQueue Q;	Q = *init(&Q);	cout<<"after init queue length:"<<getLength(&Q)<<endl;	inQueue(&Q,1);	cout<<"after input queue length:"<<getLength(&Q)<<endl;	inQueue(&Q,2);	cout<<"after input queue length:"<<getLength(&Q)<<endl;	inQueue(&Q,3);	cout<<"after input queue length:"<<getLength(&Q)<<endl;	inQueue(&Q,4);	cout<<"after input queue length:"<<getLength(&Q)<<endl;	outQueue(&Q);	cout<<"after output queue length:"<<getLength(&Q)<<endl;	inQueue(&Q,5);	cout<<"after input queue length:"<<getLength(&Q)<<endl;	inQueue(&Q,6);	cout<<"after input queue length:"<<getLength(&Q)<<endl;	}

  

循环队列