Monday, 19 October 2015

C program to implement queue

#include<stdio.h>
#include<conio.h>
#define MAX 10

int queue[MAX],front=-1,rear=-1;

void push(int[],int);
int pop(int []);
int peep(int[]);

void display(int []);

void main()
{
    int ch,m,value;
    clrscr();
    while(1)
    {
        printf("\n press 1 for push operation");
        printf("\n press 2 for pop operation");
        printf("\n press 3 for peep operation");
        printf("\n press 4 for display");
        printf("\n press 5 for exit");
        printf("\n enter your choice:");
        scanf("%d",&ch);

        switch(ch)
        {
            case 1:
                push(queue,value);
            break;

            case 2:
                printf(" REar %d\n",rear);
                printf(" FRont %d\n",front);
                if(front==-1||front>rear)
                {
                    printf("\n stack underflow");
                    break;
                }
                m=pop(queue);
                printf("%d value is deleted",m);
            break;

            case 3:

                printf(" FRont %d\n",front);
                printf(" REar %d\n",rear);

                if(front==-1||front>=rear)
                {
                    printf("\n stack underflow");
                    break;
                }
                m=peep(queue);
                printf("%d",m);
            break;

            case 4:
                display(queue);
            break;

            case 5:
                exit(1);
            break;

            default:
                printf("\n wrong choice");
        }
        getch();
    }
}

void push(int queue[],int val)
{
        printf("\n enter data to be inserted:");
        scanf("%d",&val);
        if(rear==MAX-1)
              printf("\n queue overflow");
        if(front==-1&&rear==-1)
             front=rear=0;
        else
        {
             rear++;
        }

        queue[rear]=val;

}

int pop(int queue[])
{
    int val;
    val=queue[front];
    front++;
    return val;
}

int peep(int queue[])
{
     return queue[front];
}

void display()
{
    int i;
    for(i=front;i<=rear;i++)
    {
        printf("%d\n",queue[i]);
    }
}





No comments:

Post a Comment