#include using namespace std; template struct node { T value; node* next; }; template class stack { public: stack() : _head{nullptr} {} ~stack() { auto iter = _head; while(iter) { delete iter; iter = iter->next; } } stack* push(T val) { if(!_head) { _head = new node{val, nullptr}; } else { _head = new node{val, _head}; } return this; } node* pop() { if(!_head) return nullptr; node* elem = _head; delete _head; _head = elem->next; return elem; } void print() { auto iter = _head; while(iter) { cout << iter->value << ' '; iter = iter->next; } cout << endl; } private: node* _head; }; int main() { stack* s = new stack(); s->pop(); s->push(4)->push(2)->push(8); s->print(); auto e = s->pop(); if(e) cout << e->value << endl; s->push(1); s->print(); delete s; return 0; }