Pop函数的bug,没有招到
#include "stdafx.h"
#include <iostream>
using namespace std;
#define MAXSIZE 100
class CircleQueue
{
int arr[MAXSIZE ];
int front,rear;
public:
CircleQueue()
{
front=0;
rear=0;
};
bool IsEmpty() const
{
return front==rear;
}
bool IsFull() const
{
return front==(rear+1)%MAXSIZE ;
}
void Push(int val)
{
if(! IsFull())
{
rear=(rear+1)%MAXSIZE;
arr[rear-1]=val;
}
}
int Pop() throw(out_of_range)
{
if( ! IsEmpty() )
{
{
int tmp=front;
front=(front+1)%MAXSIZE;
return arr[tmp];
}
}
else
throw out_of_range("队列空");
}
int size() const
{
return (rear+MAXSIZE-front)%MAXSIZE;
}
};
int main()
{
CircleQueue cq;
int i;
int const count=10;
for(i=0; i<count; i++)
{
cq.Push(i);
}
cout<<"循环队列的元素为:"<<cq.size()<<endl;
int size=cq.size();
for(i=0; i<cq.size(); i++)
{
try
{
cout<<cq.Pop()<<endl;
}
catch(out_of_range e)
{
cout<<e.what()<<endl;
}
}
return 0;
}
弹出第5个元素的时候,程序就会终止,跟踪很多次,都没有发现问题
...全文
相关推荐
对python中的pop函数和append函数详解
本文主要介绍了两个重要的列表操作函数:pop()和append()。首先,我们来看pop()函数。它是用来从列
python中pop()函数的语法与实例
在Python编程中,`pop()`函数是一个非常实用且强大的工具,它主要用于操作列表和字典这两种内置数据结构。该函数具有不同的用法,具体取决于其应用于列表还是字典。1. 列表pop():
JavaScript数组函数unshift、shift、pop、push使用实例
pop函数用于移除数组的最后一个元素,并返回被移除的元素。如果数组为空,pop函数也会返回undefined。
【仅用于个人整理记忆】Python中列表的pop()函数用法
声明:如标题所示,因本人为深度学习路上的新手,文章仅用于辅助个人的整理记忆,理解难免有偏差之处,都是个人拙见,如给其他同僚造成困扰,还请见谅,非常非常非常欢迎私信共同讨论,共同进步函数名:pop()作
php数组函数序列之array_pop() – 删除数组中的最后一个元素
如果没有元素可删除,`array_pop()` 将返回 `NULL`。**2.


