facebook like button

17 August, 2011

Program 73: Queue implementation on array using UDFs

The need:
    To show how queue can be implemented on integer array with user defined functions (UDFs). For sake of simplicity in UFFs I have taken array as global variable.
The code:
-----------------------------------------------------------
/*
This is a program showing the queue implimentation on array
In this front and rear are taken global variables
We can also have a program in which these are not global variables
In this convention front indexeses to the first item
and rear indexes the location after last item
this convention lets you fill maximum (SIZE-1) items in a queue where SIZE is size of array being used
program writtem by RP Singh
compiled and tested on C-free4.0 standard
*/
#define SIZE 10
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void enqueue(int);
int dequeue(void);
int queue_length(void);

int queue[SIZE];                //global implementation of queue
int  front=0, rear=0;        //my convention

int main()
{
    int i=0,j,current_item;
    char c;
    printf("Enter some items into queue\n");
    do
    {
        printf("\nEnter integer %d\t=>\t",++i);
        scanf("%d",&current_item);                    //this is the value taken from user
        enqueue(current_item);
        printf("Enter press y to enqueue more items. else press any other key.\n");
        fflush(stdin);
        c=getchar();
    }
    while(c=='y'||c=='Y');
    printf("all items successfully entered into queue\n");
    printf("value of front = %d\t rear = %d\n",front,rear);
    printf("length of queue is %d\n",rear-front);
    printf("\n\npress any key to dequeue and see all values you entered\n");
    fflush(stdin);
    getchar();
    while(queue_length()!=0)
    {
        printf("%d -> ",dequeue());
    }
    printf("\n\npress any key to quit\n");
    while(!kbhit())
    ;
    return 0;
}

void enqueue(int p)                    //definition of enqueue function
{
    if(rear==SIZE-1)
        printf("cannot enqueue because the array end has been reached.\n");
    else
        {
            queue[rear++]=p;                    //enqueing an item
            printf("item %d inserted into queue successfully\n\n",p);
        }
}

int dequeue(void)                    //definition of dequeue function
{
    int temp;
    if(rear==front)        //this is the case when queue is empty
    {
        printf("queue is empty hence can not be dequeued\n");
    }
    else
    {
        temp=queue[front++];
    }
    return temp;
}

int queue_length(void)
{
    int length=0;
    length=rear-front;
    return length;
}

-----------------------------------------------------------
Remarks:
This is a simple program showing implementation of queue concept on array. For simplicity I have taken a simple integer array. I have used some UDFs here. I just wanted to show that queue is an abstract entity, we may or may not use UDFs. Its all our wish. The only thing to remember is the concept of queue.

No comments:

Post a Comment

feel free to ask your doubts... if any
you can post your doubts on
www.facebook.com/programsimply