c++队列基本功能

 

#include<string>
#include<assert.h>
#include<iostream>
typedef int status;
#define OK 1
#define ERROR 0
template<class type>
class order_tream
{

public:
order_tream(int a):size(a+1),n(0)
{
base =new type [a+1];
assert(base!=0);
front=0;
rear=0;
}
int n;
status full();//判断是否为满
status empty();//判断是否为空
void putin(int a);//输入
status enqueue(type a);//进队列
status sqqueue(type &a);//出队列
void clear();//清空
status out();//输出整个队列
private:
int rear;//尾
int front;//头
int size;
type* base;
};
/*进队列*/
template<class type>
status order_tream<type>::enqueue(type a)
{
if( full())
return ERROR;
base[rear]=a;
rear=(rear+1)%size;
n++;
return OK;
}
/*清空*/
template<class type>
void order_tream<type>::clear()
{
front=rear;
}
/*输入*/
template<class type>
void order_tream<type>::putin(int a)
{
type x;
cout<<"输入开始"<<endl;
for(int i=0;i<a;i++)
{
cin>>x;
enqueue(x);
}
}
/*出队列*/
template<class type>
status order_tream<type>::sqqueue(type& a)
{
if(empty())
return ERROR;
a=base[front];
front=(front+1)%size;
n--;
return OK;
}
/*判断是否为空*/
template<class type>
status order_tream<type>::empty()
{
if(front==rear)
return OK;
else
return ERROR;
}
/*判断是否为满*/
template<class type>
status order_tream<type>::full()
{
if(front==rear+1)
return OK;
else
return ERROR;
}
/*输出整个队列*/
template<class type>
status order_tream<type>::out()
{
if(empty())
return ERROR;
type a;
for(int i=0;i<n;i++)
{
a=base[i];
cout<<a<<'\t';
}
cout<<n;
cout<<endl;
return OK;
}

posted @ 2016-05-30 19:32  故木  阅读(473)  评论(0编辑  收藏  举报