Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template<typename T> class CStack {
- public:
- static enum {CStack_StackEmpty, CStack_BadMemory};
- unsigned int size()
- {
- return StackSize;
- }
- bool empty()
- {
- return StackSize == 0;
- }
- T pop()
- {
- if(StackIter == 0) throw (int)CStack_StackEmpty;
- return _Stack[--StackIter];
- }
- void push(T j)
- {
- if(StackSize >= StackIter && _Stack != 0)
- {
- T* _newstack = (T*)malloc(sizeof(T) * (StackSize + 1));
- if(_newstack == NULL)
- {
- free(_newstack);
- this->~CStack();
- throw (int)CStack_BadMemory;
- }
- for(int i=0;i<StackSize;i++)
- _newstack[i] = _Stack[i];
- free(_Stack);
- _Stack = _newstack;
- }
- else if (_Stack == 0)
- {
- _Stack = (T*)malloc(sizeof(T) * (StackSize == 0 ? 1 : StackSize));
- if(_Stack == 0)
- {
- this->~CStack();
- throw (int)CStack_BadMemory;
- }
- }
- _Stack[StackSize++] = j;
- StackIter = StackSize;
- }
- void clear()
- {
- StackIter = 0;
- }
- void allclear() // useless
- {
- free(_Stack);
- StackIter = StackSize = 0;
- }
- CStack()
- {
- StackIter = StackSize = 0;
- _Stack = NULL;
- }
- ~CStack()
- {
- free(_Stack);
- StackIter = StackSize = 0;
- }
- private:
- unsigned int StackSize, StackIter;
- T* _Stack;
- };
Advertisement
Add Comment
Please, Sign In to add comment