堆栈实现在30k和400k +值之后出现错误

时间:2018-03-03 02:43:33

标签: c++

Main.cpp
#include"ArrayStack.cpp" 
#include"DoublingArrayStack.cpp" 
#include"linkedlist.cpp"
#include"stopwatch.cpp" 
int main() { 
DoublingAbstractStack<int>testdoubly(50); 
AbstractStack <int>testfixed(50); 
linkedlist testlinked; 
CStopWatch doubly; 
CStopWatch fixed; 
CStopWatch linked; 

/*fixed.Reset();
for (int i =1; i <= 10000000; i++) { 
    testfixed.push(1); 
    if ( i % 10000 == 0 ) { 
        cout << "time at " << i << " iterations: " << fixed.GetElapsedSeconds() << endl; 
    } 
} 
float fixedend = fixed.GetElapsedSeconds();
cout << "total time for 10M values pushed: " << fixedend << endl; */

doubly.Reset();
for (int i =1; i <= 10000000; i++) { 
    testdoubly.push(1); 
    if ( i % 10000 == 0 ) { 
        cout << "time at " << i << " iterations: " << doubly.GetElapsedSeconds() << endl; 
    } 
} 
float doublyend = doubly.GetElapsedSeconds();
cout << "total time for 10M values pushed: " << doublyend << endl;
} 
ArrayStack.cpp
#include <array> 
#include <iostream> 
using namespace std; 
template <class Type>
class AbstractStack
{
private:
    Type elements; // elements in the array  
    Type max; 
    Type *s;

public:
   AbstractStack(Type num) { //CONSTRUCTOR
        elements= -1; 
        this->max = num; 
        s = new Type[max];
   }

   ~AbstractStack() {delete s; return; } // DESTRUCTOR 

   bool isEmpty() {     //IS EMPTY FUNCTION 
       if (elements== -1) { 
        cout << "no data currently being stored" << endl;
        return true; 
       } 
       else { 
        cout << "Stack not empty" << endl; 
        return false; 
       } 
   }

   int size(void) {      //SIZE FUNCTION
            return elements+1; 
   }

   Type top () {      // TOP VALUE FUNCTION 
   for (int i = 0; i < elements; i++) { 
    cout << s[i] << endl; 
   }

        if (elements == -1) {
            cout << "No elements currently in stack" << endl;
        }
        else { 
            return s[elements];
        }           
   }

   Type pop(){         //REMOVING THE TOP VALUE FUNCTION 
       if (isEmpty()) {
            return 0; 
       } 
       else { 
            elements--; 
            return s[elements];
       } 
   }

   void push ( Type e ) {             
       if (elements + 1 == max) { 
            Type *temp = s; 
            delete s; 
            this->max = max + 25;
            s = new Type[max];
            for (int i = 0; i < elements+1; i++) { 
                s[i] = temp[i]; 
            }
            elements++;
            s[elements] = e ;
            return; 
       }
       else { 
            elements++;
            s[elements] = e; 
       } 
   }
};
DoublyArrayStack.cpp
#include <array> 
#include <iostream> 
using namespace std; 
template <class Type>
class DoublingAbstractStack
{
private:
    Type elements; // elements in the array  
    Type max; 
    Type *s;

public:
   DoublingAbstractStack(Type num) { //CONSTRUCTOR
        elements= -1; 
        this->max = num; 
        s = new Type[max];
   }

   ~DoublingAbstractStack() {delete s; return; } // DESTRUCTOR 

   bool isEmpty() {     //IS EMPTY FUNCTION 
       if (elements== -1) { 
        cout << "no data currently being stored" << endl;
        return true; 
       } 
       else { 
        cout << "Stack not empty" << endl; 
        return false; 
       } 
   }

   int size(void) {      //SIZE FUNCTION
            return elements+1; 
   }

   Type top () {      // TOP VALUE FUNCTION 
   for (int i = 0; i < elements; i++) { 
    cout << s[i] << endl; 
   }

        if (elements == -1) {
            cout << "No elements currently in stack" << endl;
        }
        else { 
            return s[elements];
        }           
   }

   Type pop(){         //REMOVING THE TOP VALUE FUNCTION 
       if (isEmpty()) {
            return 0; 
       } 
       else { 
            elements--; 
            return s[elements];
       } 
   }

   void push ( Type e ) {             
       if (elements + 1 == max) { 

            Type *temp = s; 
            delete s; 
            this->max *= 2;
            s = new Type[max];
            for (int i = 0; i < elements+1; i++) { 
                s[i] = temp[i]; 
            }
            elements++;
            s[elements] = e ;
            return; 
       }
       else { 
            elements++;
            s[elements] = e; 
       } 
   }
};

我的ArrayStack是存储大约35k值的seg faulting,而我的doublyArraystack是存储大约405k值的seg faulting,我不知道为什么有人能够解释为什么会这样。我的链接列表版本在~1.2秒内运行10M值,但我在上面发布的2中遇到了seg故障,这对我来说没有意义,因为它已经存储了400k值,为什么它会在405k处出错?

0 个答案:

没有答案