#ifndef DEF_STACK
#define DEF_STACK

//---------------------- Incudes 
#include "stdio.h"
//====================== Includes

class CStackEmptyEx {};

// FIFO
template <class TElem> class CStack { 
private: 
	class CStackElem {
	public:
		TElem  *elem;
		CStackElem  *prev;
	public:
		CStackElem(TElem *e) { elem = e; prev=NULL; };
		~CStackElem() { };

		TElem* Get() { return elem; };
	} *end, *cur;

public:
	CStack() { end=NULL; cur=end; };
	~CStack() { };

	inline void Push(TElem *e) 
	{ 
		CStackElem *tmp = new CStackElem(e);
			
		if (end==NULL) {
			end = tmp;
			end->prev = NULL;
			cur = end;
			return;
			
		}
		tmp->prev = cur;
		cur = tmp;
	};

	inline TElem* Pop() 
	{ 
		if (cur != NULL) {
			TElem *tmp = cur->Get();
			CStackElem *stmp = cur; 
			cur = cur->prev;
			delete stmp;
			return tmp;
		} else throw CStackEmptyEx();
	};
};

#endif

