1个回答
展开全部
#include <iostream>
using namespace std;
const int queuesize=30;
template <class T>
class crilink
{
T m_data[queuesize];
int m_front;
int m_rear;
public:
crilink();
~crilink();
void enter(T e);
T leave();
T getfront();
bool isempty();
bool isfull();
int length();
};
template <class T>
crilink<T>::crilink()
{
m_front=m_rear=0;
}
template <class T>
crilink<T>::~crilink()
{}
template <class T>
void crilink<T>::enter(T e)
{
if((m_rear+1)%queuesize==m_front) throw "上溢";
m_data[m_rear]=e;
m_rear=(m_rear+1)%queuesize;
}
template <class T>
T crilink<T>::leave()
{
T e;
if(m_front==m_rear)throw "下溢";
e=m_data[m_front];
m_front=(m_front+1)%queuesize;
return e;
}
template <class T>
T crilink<T>::getfront()
{
if(m_front==m_rear)throw "下溢";
return m_data[m_front];
}
template <class T>
bool crilink<T>::isempty()
{
if(m_front==m_rear) return true;
return false;
}
template <class T>
bool crilink<T>::isfull()
{
if((m_rear+1)%queuesize==m_front) return true;
return false;
}
template <class T>
int crilink<T>::length()
{
return (m_rear-m_front+queuesize)%queuesize;
}
int main()
{
crilink<int> h;
int i,n;
cin>>n;
for(i=1;i<=n;i++)
{
h.enter(i);
}
while(!h.isempty())
{
cout<<h.leave()<<endl;
if(!h.isempty())
h.enter(h.leave());
}
return 0;
}
using namespace std;
const int queuesize=30;
template <class T>
class crilink
{
T m_data[queuesize];
int m_front;
int m_rear;
public:
crilink();
~crilink();
void enter(T e);
T leave();
T getfront();
bool isempty();
bool isfull();
int length();
};
template <class T>
crilink<T>::crilink()
{
m_front=m_rear=0;
}
template <class T>
crilink<T>::~crilink()
{}
template <class T>
void crilink<T>::enter(T e)
{
if((m_rear+1)%queuesize==m_front) throw "上溢";
m_data[m_rear]=e;
m_rear=(m_rear+1)%queuesize;
}
template <class T>
T crilink<T>::leave()
{
T e;
if(m_front==m_rear)throw "下溢";
e=m_data[m_front];
m_front=(m_front+1)%queuesize;
return e;
}
template <class T>
T crilink<T>::getfront()
{
if(m_front==m_rear)throw "下溢";
return m_data[m_front];
}
template <class T>
bool crilink<T>::isempty()
{
if(m_front==m_rear) return true;
return false;
}
template <class T>
bool crilink<T>::isfull()
{
if((m_rear+1)%queuesize==m_front) return true;
return false;
}
template <class T>
int crilink<T>::length()
{
return (m_rear-m_front+queuesize)%queuesize;
}
int main()
{
crilink<int> h;
int i,n;
cin>>n;
for(i=1;i<=n;i++)
{
h.enter(i);
}
while(!h.isempty())
{
cout<<h.leave()<<endl;
if(!h.isempty())
h.enter(h.leave());
}
return 0;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询