|
#include<iostream>
using namespace std;
class queue
{
int f,b; //f表示front一共增加的次数,b表示back...
int front;//指向队列头
int back;//指向队列尾
int size;
int *arr;
public:
queue():front(0),back(0),size(0){arr=NULL;}
queue(int m):front(0),back(0),size(m){f=b=0;arr=new int[size];}
~queue(){delete []arr;}
void push(int i)
{
if(b-f==size)
cout<<"the queue is full"<<endl;
else
{
arr[back]=i;
back++;b++;
back%=size;
}
}
int pop()
{
if(b==f)
{
cout<<"Can't pop from a empty queue~!"<<endl;
return -999;
}
else
{
front++;front%=size;f++;
return arr[front%size-1];
}
}
bool empty()
{
if(f==b)
return true;
else
return false;
}
void disp()
{
for(int i=front;i!=back;i++)
{
cout<<arr[i]<<" ";
//i%=size;
}
cout<<endl;
}
};
int main()
{
queue x(10);
x.push(3);
x.push(4);
x.push(5);
x.push(6);
x.disp();
cout<<x.pop()<<endl;
x.disp();
return 0;
} |
|