Software & Finance





Visual C++ - Sample Queue With Template and Regular Sorting





I have given here the source code in Visual C++ for Queue (First In First Out). I have used templates. For dynamic memory allocation, I have used malloc, realloc and free functions.

The other links for Visual C++ is,

Visual C++ - Sample Queue With Template and Regular Sorting 

Turbo C++ - Sample Queue With Template and Regular Sorting

I have given here the code for regular sorting. The actual sorting using queue technique is given at the following links:

Turbo C++ - Applying Queue Technique for Rail Road Car Rearrangement

Visual C++ - Applying Queue Technique for Rail Road Car Rearrangement



Source Code


#include <iostream>

 

template<class T>

class MyQueue

{

    T *m_data;

    int m_numElements;

 

public:

    MyQueue() : m_data(NULL), m_numElements(0) { }

    ~MyQueue()

    {

        free(m_data);

        m_data = NULL;

        m_numElements = 0;

    }

 

    bool push(T data)

    {

        if(m_data == NULL) // root node

        {

            m_numElements = 1;

            m_data = (T*) malloc(sizeof(T));

        }

        else

        {

            m_numElements++;

            m_data = (T*) realloc(m_data, m_numElements * sizeof(T));

        }

 

        m_data[m_numElements - 1] = data;

        return true;

    }

 

    bool pop()

    {

        if(m_data == NULL) // root node

        {

            return false;

            m_numElements = 0;

        }

        else

        {

            if(m_numElements == 1)

            {

                // last item

                m_numElements = 0;

                free(m_data);

                m_data = NULL;

            }

            else

            {

                m_numElements--;

                memmove(m_data, &m_data[1], m_numElements * sizeof(T));

                m_data = (T*) realloc(m_data, m_numElements * sizeof(T));

            }

        }

        return true;

    }

 

    T front()

    {

        if(m_numElements > 0)

            return m_data[0];

    }

 

    int size()

    {

        return m_numElements;

    }

 

    bool Sort()

    {

        for(int i = 1; i < m_numElements; i++)

        {

            for(int j = 0; j < m_numElements - i; j++)

            {

                if(m_data[j] > m_data[j + 1])

                {

                    T temp = m_data[j];

                    m_data[j] = m_data[j + 1];

                    m_data[j + 1] = temp;

                }

            }       

        }

        return true;

    }

 

};

 

int main()

{

    MyQueue<char> q1;

    MyQueue<int> q2;

    char inpstring[] = "581742963";

    int inpNumbers[] = { 22, 10, 12, 32, 62, 42, 52, 92, 82, 72 };

 

    for(int i = 0; i < sizeof(inpstring) / sizeof(char) - 1; i++)

        q1.push(inpstring[i]);

    for(int i = 0; i < sizeof(inpNumbers) / sizeof(int); i++)

        q2.push(inpNumbers[i]);

   

    q1.Sort();

    q2.Sort();

 

    int sz1 = q1.size();

    int sz2 = q2.size();

 

    std::cout << "\n\nSorted Queue<Char>\n\n";

    for(int i = 0; i < sz1; i++)

    {

        std::cout << q1.front();

        q1.pop();

    }

    std::cout << "\n";

 

    std::cout << "\n\nSorted Queue<int>\n\n";

    for(int i = 0; i < sz2; i++)

    {

        std::cout << q2.front() << "\n";

        q2.pop();

    }

    std::cout << "\n";

 

    return 0;

}

 

Click here to download Visual C++ Source Code and Executable

Output


 

Sorted Queue<Char>

 

123456789

 

 

Sorted Queue<int>

 

10

12

22

32

42

52

62

72

82

92

 

Press any key to continue . . .