Posted By

Circular Queue in C programming Language

Education 4

#include<stdio.h>

#define size 5

int q[size],front=-1,rear=-1;

void push(){

    int item;

    if((front==0 && rear==size-1)||(front==rear+1)){

        printf("\ncircular queue overflow  \n");

    }

    else if (rear == -1)

    {

        rear++;

        front++;

    }

    else if (rear == size -1 && front > 0)

    {

        rear = 0;

    }

    else

    {

        rear++;

    }


    printf("\nEnter your item:  ");

    scanf("%d",&item);

    q[rear]=item;

}

void pop(){

    int item;

   if (front == -1)

    {

        printf("\nQueue is empty \n");

    }

    else if (front == rear)

    {

        item=q[front];

        front = -1;

        rear = -1;

    }

    else

    {

        item=q[front];

        front++;

    }

}

void disp(){

    int i;

    if (front ==-1 && rear==-1)

        printf("\nQueue is empty \n\n");

    if (front > rear)

    {

        for (i = front; i < size; i++)

        {

            printf("%d ", q[i]);

        }

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

            printf("%d ", q[i]);

    }

    else

    {

        for (i = front; i <= rear; i++)

            printf("%d ", q[i]);

    }

}

int main(){

    int ch;

    while(1){

        printf("\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\n");

        printf("Enter your choice: ");

        scanf("%d",&ch);

        if(ch==1)

            push();

        else if(ch==2)

            pop();

        else if(ch==3)

            disp();

        else if(ch==4)

            break;

        else

            printf("\nInvalid Choice\n");

    }

    return 0;

}


Circular Queue in C programming Language

Login to comment login

Latest Jobs
  • Department of Environment Messenger Job Circular
    Department of Environment
    Education: S.S.C/ Equivalent
    Experience: 0 Years
    Deadline: 13 Dec 2018
  • Department of Environment Office Assistant Job Circular
    Department of Environment
    Education: Bachelor Degree
    Experience: 0 Years
    Deadline: 13 Dec 2018
  • Department of Environment Accountant Job Circular
    Department of Environment
    Education: Master Degree
    Experience: 0 Years
    Deadline: 13 Dec 2018
  • Department of Environment Monitoring Officer  Job Circular
    Department of Environment
    Education: Bachelor Degree Agriculture/ Chemistry/ Environmental Science/ Forestry
    Experience: 0 Years
    Deadline: 13 Dec 2018
  • Ministry of Health and Family Welfare Junior Accountant Job Circular
    Ministry of Health and Family Welfare
    Education: H.S.C/ Equivalent
    Experience: 0 Years
    Deadline: 8 Dec 2018