Posted By

Circular Queue in C programming Language

Education 24

#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
  • Bangladesh Inland Water Transport Authority Junior River Surveyor Job Circular
    Bangladesh Inland Water Transport Authority
    Education: Bachelor Degree
    Experience: 0 Years
    Deadline: 10 Feb 2019
  • Bangladesh Inland Water Transport Authority River Surveyor Job Circular
    Bangladesh Inland Water Transport Authority
    Education: Bachelor Degree
    Experience: 0 Years
    Deadline: 10 Feb 2019
  • Bangladesh Navy Sub Assistant Engineer Job Circular
    Bangladesh Navy
    Education: B.Sc in Mechanical/ Computer Science Engineering
    Experience: 0 Years
    Deadline: 9 Feb 2019
  • LankaBangla Finance Limited Trainee Credit Analyst Job Circular
    LankaBangla Finance Limited
    Education: Graduate/Post Graduate
    Experience: 0 Years
    Deadline: 2 Feb 2019
  • Jahangirnagar University Store Keeper Job Circular
    Jahangirnagar University
    Education: Bachelor Degree
    Experience: 0 Years
    Deadline: 7 Feb 2019