Stack 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647#include <iostream>using namespace std;template <class T>class Stack {private: T * data; int size; int head;public: Stack(int _s) : size(_s) { if ((data = (T*)malloc(sizeof(T)*size)) == NULL) { fprintf(stderr, "MALLOC ERROR\n"); } head = 0; } Stack() { size = 10; if ((data = (T*)malloc(sizeof(T)*size)) == NULL) { fprintf(stderr, "MALLOC ERROR\n"); } head = 0; } void push(T d) { if (head<size) { data[head++] = d; return; } else { size *= 2; if ((data = (T*)realloc(data, sizeof(T)*size)) == NULL) { fprintf(stderr, "REALLOC ERROR\n"); return; } data[head++] = d; return; } } void pop() { head--; } T top() { return data[head - 1]; }};int main(int argc, char** argv) { Stack<int> s; for (int i = 0; i<13; i++) s.push(i); for (int i = 0; i<13; i++) { cout << s.top() << endl; s.pop(); } return 0;}