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

1015: 循环队列(1)

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

#include 
#include 
using namespace std;
const int QueueSize=5;
template         //定义模板类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 
CirQueue::CirQueue( )
{
 front=rear=QueueSize-1;
}

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

template 
CirQueue::~CirQueue( )
{

}

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

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

template 
bool CirQueue::Full( )
{
    return (rear+1) % QueueSize ==front;
}
int main()
{
 CirQueue Q;
 string x;
 while(1){
  cin>>x;
        if(x=="#") break;
  try{
   cout<<"EnQueue:";
   Q.EnQueue(x);
   cout<

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 
#include 

using namespace std;
const int QueueSize = 5;

template        //定义模板类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
CirQueue::CirQueue() {
    front = rear = QueueSize - 1;
}

template
CirQueue::~CirQueue() {
}

template
bool CirQueue::Empty() {
    return front == rear;
}

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

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

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

template
T CirQueue::GetQueue() {
    if(Empty()) throw "Downflow";
    return data[front];
}
//zhang sun li zhao wang xia #
int main() {
    CirQueue 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;
}

你可能感兴趣的:(数据结构学习,c++,数据结构,oj系统,队列)