Software & Finance





Turbo C - Sample Queue and Regular Sorting





I have given here the source code in Turbo C for Queue (First In First Out).  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 <alloc.h>

#include <stdlib.h>

#include <string.h>

#include <stdio.h>

 

typedef struct _MyQueue

{

    int *m_data;

    int m_numElements;

} MyQueue;

 

int size(MyQueue *q)

{

        return q->m_numElements;

}

 

int front(MyQueue *q)

{

        if(q->m_numElements > 0)

            return q->m_data[0];

        return -1;

}

 

int push(MyQueue *q, int data)

{

        if(q->m_data == NULL) // root node

        {

            q->m_numElements = 1;

            q->m_data = (int*) malloc(sizeof(int));

        }

        else

        {

            q->m_numElements++;

            q->m_data = (int*) realloc(q->m_data, q->m_numElements * sizeof(int));

        }

 

        q->m_data[q->m_numElements - 1] = data;

        return 1;

 

}

 

int pop(MyQueue *q)

{

        if(q->m_data == NULL) // root node

        {

            q->m_numElements = 0;

            return 0;

        }

        else

        {

            if(q->m_numElements == 1)

            {

                // last item

                q->m_numElements = 0;

                free(q->m_data);

                q->m_data = NULL;

            }

            else

            {

                q->m_numElements--;

                memmove(q->m_data, &q->m_data[1], q->m_numElements * sizeof(int));

                q->m_data = (int*) realloc(q->m_data, q->m_numElements * sizeof(int));

            }

        }

        return 1;

}

 

int Sort(MyQueue *q)

{

        int i, j, temp;

        for(i = 1; i < q->m_numElements; i++)

        {

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

            {

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

                {

                    temp = q->m_data[j];

                    q->m_data[j] = q->m_data[j + 1];

                    q->m_data[j + 1] = temp;

                }

            }

        }

        return 1;

}

 

void Clear(MyQueue *q)

{

        free(q->m_data);

        q->m_data = NULL;

        q->m_numElements = 0;

 

}

 

 

int main()

{

    MyQueue *q1 = malloc(sizeof(MyQueue));

    int i, sz1;

    char inpstring[32];

 

    printf("Enter Queue : ");

    scanf("%s", inpstring);

 

    for( i = 0; i < strlen(inpstring); i++)

        push(q1,inpstring[i] - '0');

 

 

    sz1 = size(q1);

    Sort(q1);

 

    printf("Displaying Queue : %d elements\n", sz1);

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

    {

        int m = front(q1);

        printf("%d", m);

        pop(q1);

    }

    printf("\n");

 

    Clear(q1);

    free(q1);

    return 0;

}


Click here to download Turbo C Source Code and Executable 

 

Output


Enter Queue: 543126
Displaying Queue: 5 elements
123456

Press any key to continue . . .