Software & Finance





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





I have given here the source code in Turbo 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.h>

#include <alloc.h>

#include <stdlib.h>

 

typedef enum _boolean

{

    true = 1, false = 0

} bool;

 

 

 

template<class T>

class MyQueue

{

    class T *m_data;

    int m_numElements;

 

public:

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

    ~MyQueue()

    {

      free(m_data);

      m_data = NULL;

      m_numElements = 0;

    }

 

    T front();

    int size()

    {

      return m_numElements;

    }

 

    bool push(T data);

    bool pop();

    bool Sort();

 

};

 

template<class T>

bool MyQueue<class T>::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;

}

 

template<class T>

bool MyQueue<class T>::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;

}

 

template<class T>

bool MyQueue<class T>::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;

}

 

template<class T>

T MyQueue<class T>::front()

{

      if(m_numElements > 0)

          return m_data[0];

}

 

 

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 j = 0; j < sizeof(inpNumbers) / sizeof(int); j++)

      q2.push(inpNumbers[j]);

 

    q1.Sort();

    q2.Sort();

 

    int sz1 = q1.size();

    int sz2 = q2.size();

 

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

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

    {

      cout << q1.front();

      q1.pop();

    }

    cout << "\n";

 

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

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

    {

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

      q2.pop();

    }

    cout << "\n";

 

    return 0;

}

 

 
Click here to download Turbo 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 . . .