题目描述

/循环队列中利用头尾指针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< }
catch(const char ms)
{
cout< }
}
while(!Q.Empty())
{
x=Q.DeQueue();
cout<<”DeQueue:”< }
try{
x=Q.GetQueue();
}
catch(const char
ms)
{
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

提示

来源

提交

  1. import java.util.Scanner;
  2. class CirQueue{
  3. Object[] queueElem;
  4. int front;
  5. int rear;
  6. int QueueSize=5;
  7. public CirQueue(){
  8. front = rear = 0;
  9. queueElem = new Object[QueueSize];
  10. }
  11. public void clear(){
  12. front = rear = 0;
  13. }
  14. public boolean isEmpty(){
  15. return front == rear;
  16. }
  17. public int length(){
  18. return (rear-front+queueElem.length)% queueElem.length;
  19. }
  20. public boolean isFull(){
  21. return (rear+1) % QueueSize ==front;
  22. }
  23. public void EnQueue(Object x) throws Exception{
  24. if((rear+1)%queueElem.length==front){
  25. throw new Exception(" Overflow");
  26. }else{
  27. queueElem[rear] = x;
  28. rear = (rear+1)%queueElem.length;
  29. }
  30. }
  31. public Object DeQueue() throws Exception {
  32. if(front==rear){
  33. throw new Exception("Downflow");
  34. }else{
  35. Object t = queueElem[front];
  36. front = (front+1)%queueElem.length;
  37. return t;
  38. }
  39. }
  40. public Object GetQueue() throws Exception {
  41. if(front==rear){
  42. throw new Exception("Downflow");
  43. }else{
  44. Object t = queueElem[front];
  45. return t;
  46. }
  47. }
  48. }
  49. public class Main {
  50. public static void main(String[] args) {
  51. CirQueue cirQueue = new CirQueue();
  52. Scanner scanner = new Scanner(System.in);
  53. while (true){
  54. String data = scanner.next();
  55. if(data.equals("#"))break;
  56. System.out.print("EnQueue:" + data);
  57. try {
  58. cirQueue.EnQueue(data);
  59. } catch (Exception e) {
  60. System.out.print(e.getMessage());
  61. }
  62. System.out.println();
  63. }
  64. while(!cirQueue.isEmpty()) {
  65. try {
  66. System.out.print("DeQueue:"+cirQueue.DeQueue());
  67. } catch (Exception e) {
  68. System.out.print(e.getMessage());
  69. }
  70. System.out.println();
  71. }
  72. try{
  73. cirQueue.GetQueue();
  74. } catch(Exception e) {
  75. System.out.print("GetQueue:The queue is empty,"+e.getMessage());
  76. }
  77. }
  78. }