facebook like button

17 August, 2011

Program 74: Circular Queue implementation on array using UDFs

The need:
    To show how circular 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 implementation on an 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 indexes 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 the array being used
program writtem by RP Singh
compiled and tested on C-free4.0 standard
*/
#define SIZE 5
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void enqueue(int);
int dequeue(void);
int queue_length(void);
void view_queue(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("This is a demo program to show working of queues");
    anchor:                    //anchor is a label
    printf("\n\nyou have following options\n");
    printf("1. enqueue an item\n2. dequeue an item\n3. view queue\n");
    printf("4. count items in queue\n5. exit program\n\n");
    scanf("%d",&j);
    switch(j)
    {
        case 1:
            printf("\nEnter a number to be enqueued =>\t");
            scanf("%d",&current_item);
            enqueue(current_item);
            goto anchor;
        case 2:
            current_item = dequeue();
            goto anchor;
        case 3:
            view_queue();
            goto anchor;
        case 4:
            printf("total items in the queue are %d\n",queue_length());
            goto anchor;
        case 5:
            printf("Thank you\n");
            exit(0);
            goto anchor;
        default:
            printf("Invalid choice...!!!\n try choosing again\n\n");
            goto anchor;
    }
   
    return 0;
}

void enqueue(int p)                    //definition of enqueue function
{
    if((rear+1)%SIZE==front)
        printf("cannot enqueue because the queue is already full.\n");
    else
        {
            queue[rear]=p;                    //enqueing an item
            rear=(rear+1)%SIZE;
            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];
        front=(front+1)%SIZE;
        printf("item %d dequeued successfully\n",temp);
    }
    return temp;
}

int queue_length(void)
{
    int length=0;
    if(rear>=front)
    length=rear-front;
    else
    length=(rear+SIZE)-front;
    return length;
}

void view_queue(void)
{
    int temp_front;
    temp_front=front;
    if(queue_length()==0)
    {
        printf("\nThe queue is empty...!!!\n");
        return;
    }
    printf("The queue is\n");
    while(temp_front!=rear)
    {
        printf("%d -> ",queue[temp_front]);
        temp_front=(temp_front+1)%SIZE;
    }
    printf("\n");
    return;
}

-----------------------------------------------------------
Remarks:
This is a simple program showing implementation of circular 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