#include <iostream>
#include <algorithm>

using namespace std;

class AStack {
public:
    AStack(int size=25);
    AStack(const AStack&);
    ~AStack();
    AStack& operator = (AStack s);
    void push(int);
    int pop();
    int top();
    bool isEmpty();
    void flush();

private:
    int capacity ;
    int* a;
    int index; // Index of the top most element
};


AStack::AStack(int size) : index(-1), a(new int[size]()), capacity(size) {}

AStack::AStack(const AStack& s) : capacity(s.capacity), a(new int[capacity]()), index(s.index)
{    std::copy(s.a, s.a + capacity, a);  }

AStack::~AStack() 
{ delete[] a; }

AStack& AStack::operator = (AStack s) 
{
    std::swap(capacity, s.capacity);
    std::swap(a, s.a);
    std::swap(index, s.index);
    return *this;
}

void AStack::push(int x) {
    if (index == capacity - 1) {
        cout << "\n\nThe stack is full. Couldn't insert " << x << "\n\n";
        return;
    }
    a[++index] = x;
}

int AStack::pop() {
    if (index == -1) {
        cout << "\n\nNo elements to pop\n\n";
        return -1;
    }
    return a[index--];
}

int AStack::top() {
    if (index == -1) {
        cout << "\n\nNo elements in the Stack\n\n";
        return -1;
    }
    return a[index];
}

bool AStack::isEmpty() {
    return (index == -1);
}

void AStack::flush() {
    if (index == -1) {
        cout << "\n\nNo elements in the Stack to flush\n\n";
        return;
    }
    cout << "\n\nFlushing the Stack:  ";
    while (index != -1) {
        cout << a[index--] << "  ";
    }
    cout << endl << endl;
}

AStack& reverseStack(AStack& s1) {
    AStack s2;
    while (!s1.isEmpty()) {
        s2.push(s1.pop());
    }
    s1 = s2;
    return s1;
}

int main() {
    AStack s1;
    s1.push(1);
    s1.push(2);
    s1.push(3);
    s1.push(4);
    s1.push(5);
    s1 = reverseStack(s1);
    cout << "\n\nFlushing s1:\n";
    s1.flush();
    return 0;
}
