题目描述
/循环队列中利用头尾指针front、rear之间的关系实现队满(当队中仅剩一个空闲单元时即视为队满)、队空条件判断。循环队列类的定义、部分实现及主函数代码如下(勿改动),请在此基础上补充实现队列类中未实现的相关算法:/
#include
#include
using namespace std;
const int QueueSize=5;
template
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
{
front=rear=QueueSize-1;
}
/
前置条件:队列已存在
输 入:无
功 能:销毁队列
输 出:无
后置条件:释放队列所占用的存储空间
/
template
CirQueue
{
}
/
前置条件:队列已存在
输 入:无
功 能:判断队列是否为空
输 出:如果队列为空,返回1,否则,返回0
后置条件:队列不变
/
template
bool CirQueue
{
return front==rear;
}
/
前置条件:队列已存在
输 入:无
功 能:判断队列是否为满
输 出:如果队列为满,返回1,否则,返回0
后置条件:队列不变
/
template
bool CirQueue
{
return (rear+1) % QueueSize ==front;
}
int main()
{
CirQueue
string x;
while(1){
cin>>x;
if(x==”#”) break;
try{
cout<<”EnQueue:”;
Q.EnQueue(x);
cout<
catch(const char ms)
{
cout<
}
while(!Q.Empty())
{
x=Q.DeQueue();
cout<<”DeQueue:”<
try{
x=Q.GetQueue();
}
catch(const char
{
cout<<”GetQueue:The queue is empty,”<
return 0;
}
输入
输出
样例输入
zhang sun li zhao wang xia #
样例输出
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
提示
来源
提交
import java.util.Scanner;class CirQueue{Object[] queueElem;int front;int rear;int QueueSize=5;public CirQueue(){front = rear = 0;queueElem = new Object[QueueSize];}public void clear(){front = rear = 0;}public boolean isEmpty(){return front == rear;}public int length(){return (rear-front+queueElem.length)% queueElem.length;}public boolean isFull(){return (rear+1) % QueueSize ==front;}public void EnQueue(Object x) throws Exception{if((rear+1)%queueElem.length==front){throw new Exception(" Overflow");}else{queueElem[rear] = x;rear = (rear+1)%queueElem.length;}}public Object DeQueue() throws Exception {if(front==rear){throw new Exception("Downflow");}else{Object t = queueElem[front];front = (front+1)%queueElem.length;return t;}}public Object GetQueue() throws Exception {if(front==rear){throw new Exception("Downflow");}else{Object t = queueElem[front];return t;}}}public class Main {public static void main(String[] args) {CirQueue cirQueue = new CirQueue();Scanner scanner = new Scanner(System.in);while (true){String data = scanner.next();if(data.equals("#"))break;System.out.print("EnQueue:" + data);try {cirQueue.EnQueue(data);} catch (Exception e) {System.out.print(e.getMessage());}System.out.println();}while(!cirQueue.isEmpty()) {try {System.out.print("DeQueue:"+cirQueue.DeQueue());} catch (Exception e) {System.out.print(e.getMessage());}System.out.println();}try{cirQueue.GetQueue();} catch(Exception e) {System.out.print("GetQueue:The queue is empty,"+e.getMessage());}}}
