64,648
社区成员
发帖
与我相关
我的任务
分享
#include "stdafx.h"
#include <iostream>
using namespace std;
#define MAXSIZE 100
class CircleQueue
{
int arr[MAXSIZE ];
int front,rear;
public:
CircleQueue()
{
front=0;
rear=0;
};
bool IsEmpty() const
{
return front==rear;
}
bool IsFull() const
{
return front==(rear+1)%MAXSIZE ;
}
void Push(int val)
{
if(! IsFull())
{
rear=(rear+1)%MAXSIZE;
arr[rear-1]=val;
}
}
int Pop() throw(out_of_range)
{
if( ! IsEmpty() )
{
{
int tmp=front;
front=(front+1)%MAXSIZE;
return arr[tmp];
}
}
else
throw out_of_range("队列空");
}
int size() const
{
return (rear+MAXSIZE-front)%MAXSIZE;
}
};
int main()
{
CircleQueue cq;
int i;
int const count=10;
for(i=0; i<count; i++)
{
cq.Push(i);
}
cout<<"循环队列的元素为:"<<cq.size()<<endl;
int size=cq.size();
for(i=0; i<cq.size(); i++)
{
try
{
cout<<cq.Pop()<<endl;
}
catch(out_of_range e)
{
cout<<e.what()<<endl;
}
}
return 0;
}