数据结构C++版 王红梅 OJ习题

1015: 循环队列(1)

/*循环队列中利用头尾指针front、rear之间的关系实现队满(当队中仅剩一个空闲单元时即视为队满)、队空条件判断。循环队列类的定义、部分实现及主函数代码如下(勿改动),请在此基础上补充实现队列类中未实现的相关算法:*/

#include <iostream>
#include <string>
using namespace std;
const int QueueSize=5;
template <class T>        //定义模板类CirQueue
class CirQueue
{
public:
    CirQueue( );                 //构造函数,置空队
    ~ CirQueue( );               //析构函数
    void EnQueue(T x);           //将元素x入队,队满时抛出异常信息Overflow
     T DeQueue( );                //将队头元素出队,队空抛出异常信息Downflow
    T GetQueue( );               //取队头元素(并不删除),队空抛出异常信息Downflow
    bool Empty( );               //判断队列是否为空,空返回true,否则返回false
 bool Full();                 //判断队列是否为满,满返回true,否则返回false
private:
    T data[QueueSize];           //存放队列元素的数组
    int front, rear;    //队头和队尾指针,分别指向队头元素所在数组的前一下标和队尾元素的数组下标
};

/*
 * 前置条件:队列不存在
 * 输    入:无
 * 功    能:初始化队列
 * 输    出:无
 * 后置条件:创建一个空队列
 */

template <class T>
CirQueue<T>::CirQueue( )
{
 front=rear=QueueSize-1;
}

/*
 * 前置条件:队列已存在
 * 输    入:无
 * 功    能:销毁队列
 * 输    出:无
 * 后置条件:释放队列所占用的存储空间
 */

template <class T>
CirQueue<T>::~CirQueue( )
{

}

/*
 * 前置条件:队列已存在
 * 输    入:无
 * 功    能:判断队列是否为空
 * 输    出:如果队列为空,返回1,否则,返回0
 * 后置条件:队列不变
 */

template <class T>
bool CirQueue<T>::Empty( )
{
    return front==rear;
}
/*
 * 前置条件:队列已存在
 * 输    入:无
 * 功    能:判断队列是否为满
 * 输    出:如果队列为满,返回1,否则,返回0
 * 后置条件:队列不变
 */

template <class T>
bool CirQueue<T>::Full( )
{
    return (rear+1) % QueueSize ==front;
}
int main()
{
 CirQueue<string> Q;
 string x;
 while(1){
  cin>>x;
        if(x=="#") break;
  try{
   cout<<"EnQueue:";
   Q.EnQueue(x);
   cout<<x<<"\n";
  }
  catch(const char *ms)
  {
   cout<<x<<" "<<ms<<endl;
  }

 }
 while(!Q.Empty())
 {
  x=Q.DeQueue();
  cout<<"DeQueue:"<<x<<endl;
 }
 try{
  x=Q.GetQueue();
 }
 catch(const char *ms)
 {
  cout<<"GetQueue:The queue is empty,"<<ms<<endl;
 }
 return 0;
}

Input

Output

Sample Input

zhang sun li zhao wang xia #

Sample Output

EnQueue:zhang
EnQueue:sun
EnQueue:li
EnQueue:zhao
EnQueue:wang Overflow
EnQueue:xia Overflow
DeQueue:zhang
DeQueue:sun
DeQueue:li
DeQueue:zhao
GetQueue:The queue is empty,Downflow
#include <iostream>
#include <string>

using namespace std;
const int QueueSize = 5;

template<class T>        //定义模板类CirQueue
class CirQueue {
public:
    CirQueue();                 //构造函数,置空队
    ~ CirQueue();               //析构函数
    void EnQueue(T x);           //将元素x入队
    T DeQueue();                //将队头元素出队
    T GetQueue();               //取队头元素(并不删除)
    bool Empty();               //判断队列是否为空,空返回true,否则返回false
    bool Full();                 //判断队列是否为满,满返回true,否则返回false
private:
    T data[QueueSize];           //存放队列元素的数组
    int front, rear;    //队头和队尾指针,分别指向队头元素所在数组的前一下标和队尾元素的数组下标
};

template<class T>
CirQueue<T>::CirQueue() {
    front = rear = QueueSize - 1;
}

template<class T>
CirQueue<T>::~CirQueue() {
}

template<class T>
bool CirQueue<T>::Empty() {
    return front == rear;
}

template<class T>
bool CirQueue<T>::Full() {
    return (rear + 1) % QueueSize == front;
}

template<class T>
void CirQueue<T>::EnQueue(T x) {
    if(Full()) throw "Overflow";
    rear = (rear + 1) % QueueSize;
    data[rear] = x;
}

template<class T>
T CirQueue<T>::DeQueue() {
    if(Empty()) throw "Downflow";
    front = (front + 1) % QueueSize;
    return data[front];
}

template<class T>
T CirQueue<T>::GetQueue() {
    if(Empty()) throw "Downflow";
    return data[front];
}
//zhang sun li zhao wang xia #
int main() {
    CirQueue<string> Q;
    string x;
    while (1) {
        cin >> x;
        if (x == "#") break;
        try {
            cout << "EnQueue:";
            Q.EnQueue(x);
            cout << x << "\n";
        }
        catch (const char *ms) {
            cout << x << " " << ms << endl;
        }
    }
    while (!Q.Empty()) {
        x = Q.DeQueue();
        cout << "DeQueue:" << x << endl;
    }
    try {
        x = Q.GetQueue();
    }
    catch (const char *ms) {
        cout << "GetQueue:The queue is empty," << ms << endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值