Saturday, 8 September 2012

QUEUE IMPLEMENTED USING ARRAY IN C


/**
 *THIS PROGRAM IS A REPRESENTATION OF QUEUE IMPLEMENTATION.
 *HERE AN ARRAY USED TO RE-PRESENT A QUEUE
 *AND BASIC QUEUE OPERATION ARE DESCRIBED USING SOME BASIC FUNCTION
 *MENU DRIVEN FOR BETTER UNDERSTANDING
 *
 *
 *FILE NAME :queue_using_array.c
 *DATE      :08/09/2012
 *PROGMMER  :RAJESH KUMAR SAHOO.
 *BOLOG     :http://dsforeverystudent.blogspot.in/
 */

/*
   header files for function prototypes of inbuild library functions
 */

/*Program for queue using array*/
/*headers for avoid compile error and warnings*/
#include <stdio.h>
#include <stdlib.h>
/*macros used for easy modification*/
#define QUE_SIZE 10
#define INVALID -1
#define BEG 0
/*function prototypes*/
void init(void);
int menu(void);
void enque(int ele);
int deque(void);
void print(void);

/*global datas*/
int QUEUE[QUE_SIZE];
int end;
/*INITIALIZE THE QUEUE FOR OPERATION(CLEARING THE CONTENTS)*/
void init()
{
        end=INVALID;
        int i;
        for(i=0;i<QUE_SIZE;i++)
                QUEUE[i]=INVALID;
}

/*THIS FUNCTION IS USED FOR ENQUE AN ELEMENT(INSERT TO QUEUE)*/
void enque(int ele)
{
        if(end>=QUE_SIZE-1)
        {
                printf("\nQUEUE IS ALREDY FILLED\n");
                return;
        }
        ++end;
        QUEUE[end]=ele;
        printf("\n%d ENQUE TO THE QUEUE\n",ele);
}

/*THIS FUNCTION USED FOR DE-QUE OPERATION(PROCESS/REMOVE AN ELEMENT FROM QUEUE)*/
int deque(void)
{
        int ele,i;
        ele=INVALID;
        if(end==INVALID)
        {
                printf("\nQUEUE IS EMPTY\n");
                return ele;
        }
        ele=QUEUE[BEG];
        for(i=1;i<=end;i++)
        {
                QUEUE[i-1]=QUEUE[i];
        }
        --end;
        //printf("\n%d IS DEQUQ FROM QUEUE\n",ele);
        return ele;
}

/*THIS FUNCTION USED TO PRINT THE QUEUE ELEMENTS FOR VERIFICATION*/
void print(void)
{
        int i;
        if(end==INVALID)
        {
                printf("\nQUEUE IS EMPTY\n");
                return;
        }
        else
                printf("\n:::::::::::QUEUE:::::::::\n|BEG|");

        for(i=BEG;i<=end;i++)
        {
                if(i==end)
                        printf("%d|END|",QUEUE[i]);
                else
                        printf("%d<-",QUEUE[i]);
        }
}

/*MAIN FUNCTION TO CONTROL THE FLOW OF PROGRAM*/
int main()
{
        int choice,ele;
        init();
        while(1)
        {
                choice=menu();
                switch(choice)
                {
                        case 1:printf("\nEnter a number for enqueue: ");
                               scanf("%d",&ele);enque(ele);break;
                        case 2:ele=deque();
                               if(ele!=INVALID)
                                       printf("\n%d IS DEQUQ FROM QUEUE\n",ele);break;
                        case 3:print();break;
                        case 4:exit(0);break;
                        default:
                               printf("\nINVALID CHOICE\nTRY AGAIN.....\n");break;
                }
        }
}

/*THIS FUNCTION USED TO DISPLAY MENU IN THE OUTPUT DEVICE AND GETTING USER CHOICE*/
int menu()
{
        int choice;
        printf("\n:::::::::::MENU::::::::::::\n");
        printf("\n1.ENQUE\n2.DEQUE\n3.PRINT\n4.EXIT\n");
        printf("\nENTER YOUR CHOICE: ");
        scanf("%d",&choice);
        return choice;
}

No comments:

Post a Comment