向前推C和x2B+;用户实现 我想对C++双端队列实现一种推前线方法。我这样做的方式是移动数组的每个元素。它成功了,但我的程序最终崩溃了!在我的push-front方法中,我似乎“跑过了数组的末尾”,导致了堆损坏错误、调试断言等等
我无法在不改变阵列的情况下开发推送前端实现向前推C和x2B+;用户实现 我想对C++双端队列实现一种推前线方法。我这样做的方式是移动数组的每个元素。它成功了,但我的程序最终崩溃了!在我的push-front方法中,我似乎“跑过了数组的末尾”,导致了堆损坏错误、调试断言等等,c++,C++,我无法在不改变阵列的情况下开发推送前端实现 stack::stack(capacity) : items(new item[capacity]), front(*items), maxSize(capacity-1) { top = -1; } bool stack::pushFront(const int nPushFront) { if ( count == maxSize ) // indicates a full array {
stack::stack(capacity) : items(new item[capacity]), front(*items), maxSize(capacity-1)
{
top = -1;
}
bool stack::pushFront(const int nPushFront)
{
if ( count == maxSize ) // indicates a full array
{
return false;
}
for ( int entry_int = 0; entry_int < count; ) // loop less than however many we count.
{
if ( entry_int == top+1 )
{
front.n = items[top+1].n;
}
items->n = items[++entry_int].n;
items[entry_int].n = front.n;
front.n = items[++entry_int].n;
items[entry_int].n = items->n;
}
++count;
items[top+1].n = nPushFront;
return true;
}
stack::stack(容量):项(新项[capacity]),前端(*项),最大大小(容量-1)
{
top=-1;
}
bool stack::pushFront(const int nPushFront)
{
if(count==maxSize)//表示一个完整数组
{
返回false;
}
for(int entry_int=0;entry_intn=项目[++entry\u int].n;
项目[entry_int].n=前端.n;
front.n=项目[++entry\u int].n;
项目[输入].n=项目->n;
}
++计数;
项目[top+1].n=nPushFront;
返回true;
}
有人能帮忙吗?通过保持前后偏移量/指针,无需移动即可轻松完成此操作。请看一个例子。bool Stack::PushFront(const int neelement) {
}我对格式进行了分析,在我发布之前它看起来很好。我把它命名为push\u-front方法的“用户实现”,就像我在不使用boost、stl等的情况下这样做一样。循环缓冲区可以避免每次push\u-front调用上的所有元素都发生移动。在不使用boost的情况下,Nikolai向您指出了一个可以阅读的参考实现:循环缓冲区是双队列的标准实现,您应该尝试实现它,因为这是一个很好的练习。
if(count == maxSize) return false;
for(int i = count ; i > 0; i--)
{
items[i].n = items[i-1].n;
}
item[top+1].n = nElement;
count++;
return true;