Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Dr.Junkie

Pages: [1]
1
C - C++ / Stack Management without vector
« on: February 16, 2015, 04:34:41 pm »
Code: [Select]
/*
      Coded by Dr.Junkie @ evilzone.org
*/
template <typename T>
class CStackManager
{
private:
    T* m_pStack = NULL;
    int m_iSize;
public:
    void push_back(T &Data);
    T pop_back(int iIndex);
    T get_value(int iIndex);
};

template<typename T>
void CStackManager<T>::push_back(T &Data)
{
    if (m_pStack == NULL)
    {
        m_pStack = (T*)malloc(sizeof(T));
        m_pStack[0] = Data;
        m_iSize = 1;
    }
    else
    {
        m_iSize += 1;
        m_pStack = (T*)memrealloc(m_pStack, m_iSize * sizeof(T));
        m_pStack[m_iSize - 1] = Data;
    }
}

template<typename T>
T CStackManager<T>::pop_back(int iIndex)
{
    T Data = NULL;

    if (m_iSize > 1)
    {
        int iNewSize = 0;

        T* pStack = (T*)malloc(m_iSize * sizeof(T));
       
        for (unsigned int i = 0; i < m_iSize; i++)
        {
            if (iIndex == i)
                continue;

            int iNewIndex = i;

            if (i > iIndex)
                iNewIndex -= 1;

            pStack[iNewIndex] = m_pStack[i];

            iNewSize++;

        }

        Data = m_pStack[iIndex];
        m_pStack = pStack;

        m_iSize = iNewSize;
    }
    else
    {
        Data = m_pStack[iIndex];
        ZeroMemory(m_pStack, sizeof(T) * 1);
        m_iSize = 0;
    }

    return Data;
}

template<typename T>
T CStackManager<T>::get_value(int iIndex)
{
    return m_pStack[iIndex];
}

Pages: [1]