用array来做一个stack


#include

using namespace std;

 

 

class myStack{

public:

    int size = 0;

    int array[30];

    bool isEmpty(){

        return size == 0 ? true : false;

    }

    int getSize(){

        return size;

    }

    int peek(){

        return array[size-1];

    }

    int pop(){

        int temp = array[size-1];

        array[size-1] = 0;

        size--;

        return temp;

    }

    void push(int value){

        array[size] = value;

        size++;

    }

};

 

int main() {

    myStack test;

    for(int i = 1; i <= 5; i++){

        test.push(i);

    }

    cout<getSize()<<endl;

    cout<peek()<<endl;

    for(int i = 1; i <= 5; i++){

        cout<pop()<<endl;

    }

    if(test.isEmpty())cout<<"empty";

    return 0;

}