需要在EnQueue函数中加入队列满的判断,当队列满时,抛出一个队列满异常。修改后的代码如下:

//入队操作(循环队列) Statue EnQueue(SqQueue& Q,int e) { if ((Q.rear + 1) % MAXQSIZE == Q.front) { //队列满,抛出异常 throw "Queue is full!"; } Q.base[Q.rear] = e; //新元素插入队尾 Q.rear = (Q.rear + 1) % MAXQSIZE; //队尾指针向上移动 cout << Q.base[Q.front] << endl; return 0; }

需要在main函数中加入try-catch语句来捕获异常,并输出异常信息。修改后的main函数代码如下:

int main() { try { fun_EnQueue(); } catch (const char* msg) { cerr << msg << endl; } cout << "Hello World!\n"; return 0;


原文地址: https://www.cveoy.top/t/topic/cVqz 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录