所以我一直在试图实现一个arrayStack,但我似乎有弹出超过100个元素的问题。我的程序在达到100个时会停止工作。这似乎是推动和流行的问题,但不太明白我的问题是什么。这里我的ArrayStack发生了什么?
#ifndef _ARRAYSTACK_1_HPP_
#define _ARRAYSTACK_1_HPP_
#include <stddef.h>
#include "StackADT.h"
#define DEFAULT_SIZE 50
template<class T>
class ArrayStack_1 : public StackADT<T> {
private:
T *arrayStack;
int index;
int maxSize;
public:
// a constructor for the arrayStack that creates and arrayStack of a given size
ArrayStack_1(int size = DEFAULT_SIZE) {
maxSize = size;
index = 0;
arrayStack = new T[size];
}
public:
// a constructor for the arrayStack that creates and arrayStack of a given size
ArrayStack_1(int size = DEFAULT_SIZE) {
maxSize = size;
index = 0;
arrayStack = new T[size];
}
//a destructor that deletes the arrayStack
~ArrayStack_1() {
delete[] arrayStack;
}
bool Empty() {
return index==0;
}
void Push(T& item = 0) {
if (index<maxSize) {
arrayStack[index]= item;
index+=1;
}else {
T* tempArrayStack = arrayStack; //making a copy of the array stack
maxSize+=1;
arrayStack = new T[maxSize];
for (int i=0; i<maxSize; i++){
arrayStack[i]=tempArrayStack[i];
}
arrayStack[index]=item;
index+=1;
delete[] tempArrayStack;
}
}
T Pop() {
if(arrayStack[index]==0){
return 0;
}else {
T element = arrayStack[index];
index--;
return element;
}
}
}
你可以显示你从运行你的代码得到的实际错误消息吗? –
它结束之前,即使它说数组增长为1. http://imgur.com/rf6GQ6p – dikshant
当人们为您修复代码时,您需要停止编辑您的问题。答案不再有意义,因为问题已经改变。 – John3136