Circular Queue

Circular queue

We know that  major drow back of linear queue is that we can not insert on the deleted position but in case of circular queue new element can be inserted on the deleted position in this case rear goes back to zero if rear is equivalent to size-1.
circular queue implementation through program.
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<stdlib.h>


void cqinsert();
void cqdisplay();
void cqdelet();

int cqueue[50],front=-1,rear=-1,size;
 int main()
 {
     int choice;
     printf("enter the size of queue");
         scanf("%d",&size);
     do
     {
         system("cls");
         printf("====SELECT YOUR CHOICE=====\n press 1 for insertinon-\n press 2 for display the element-\n press 3 for delete-----\n  enter choice=");
         scanf("%d",&choice);
         switch(choice)
         {
         case 1:
            cqinsert(); break;
         case 2:
            cqdisplay(); break;
         case 3:
            cqdelet(); break;
         default:
            printf("invalid choice!");

         }
     }
     while(choice>=1||choice<=3);

 }

 void cqinsert()
{
    int i;
    if(front==-1)
    {
        front=rear=0;
        printf("enter the element for queue");
        scanf("%d",&cqueue[rear]);
    }
    else{
        if(rear==size-1)
            i=0;
        else
            i=rear+1;
        if(i==front)
            printf("queue is overflow");
        else{
            rear=i;
            printf("enter the element of queue");
            scanf("%d",&cqueue[rear]);
        }

    }
    system("pause");
}

 void cqdisplay()
 {
     int i;
     if(front==-1)
        printf("Queue is underflow");
     else{
        if(front<=rear)
        {
            for(i=front;i<=rear;i++)
            {
                printf("%d\n",cqueue[i]);
            }
        }
        else
        {
            for(i=front;i<=size-1;i++)
            {
              printf("%d\n",cqueue[i]);
            }
            for(i=0;i<=rear;i++)
            {
              printf("%d\n",cqueue[i]);
            }
        }
     }
     system("pause");
 }


 void cqdelet()
 {
     if(front==-1)
        printf("queue is underflow");
     else{
        printf("deletd element=%d\n",cqueue[front]);
        if(front==rear)
            front=rear=-1;
        else{
            if(front==size-1)
                front=0;
            else
                front=front+1;
        }
     }
system("pause");
 }

Comments

Popular posts from this blog

python pattern programming

Decision making statement in python

javaScript Calculator.