5/01/2009

C Program Circular Queue


/*C Program Circular Queue*/
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define size 3
int item,x,f,r,connt,i,cQ[size];
void insert()
{
    if(connt==size-1)
    {
        printf("overflow\n");
        return;
    }
    printf("enter elements to be inserted:\n");
    scanf("%d",&item);
    r=r+1;
    cQ[r]=item;
    r=r%size;
    connt++;
}
void delete()
{
    if(connt==-1)
    {
        printf("empty\n");
        return;
    }
    x=cQ[f];
    printf("deleted element: %d\n",x);
    f=f+1;
    f=f%size;
    connt--;
}
void display()
{
    if(connt==-1)
    {
        printf("empty\n");
        return;
    }
    printf("elements in the queue:\n");
    for(i=0; i<=connt; i++)
    {
        printf("%d\t",cQ[f++]);
        f=f%size;
    }
    printf("\n");
}
void main()
{
    int ch;

    f=0,r=-1,connt=-1;
    while(1)
    {
        printf("1.insert\t2.delete\t3.display\n");
        printf("enter the choice:\n");
        scanf("%d",&ch);
        switch(ch)
        {
        case 1:
            insert();
            break;
        case 2:
            delete();
            break;
        case 3:
            display();
            break;
        default:
            exit (0);
        }

    }
}
Out Put:
1.insert        2.delete        3.display
enter the choice:
1
enter elements to be inserted:
1
1.insert        2.delete        3.display
enter the choice:
1
enter elements to be inserted:
2
1.insert        2.delete        3.display
enter the choice:
1
enter elements to be inserted:
3
1.insert        2.delete        3.display
enter the choice:
1
overflow
1.insert        2.delete        3.display
enter the choice:
3
elements in the queue:
1       2       3
1.insert        2.delete        3.display
enter the choice:
2
deleted element: 1
1.insert        2.delete        3.display
enter the choice:
2
deleted element: 2
1.insert        2.delete        3.display
enter the choice:
2
deleted element: 3
1.insert        2.delete        3.display
enter the choice:
2
empty
1.insert        2.delete        3.display
enter the choice:
4

No comments:

Post a Comment