5/01/2009

C Program Queue Operation


/*C Program Queue Operation*/
#include<stdio.h>
#include<conio.h>
#define max 5
int r,f,x,item,q[max];
void insert()
{
    if(r==max-1)
    {
        printf("queue is overflow! \n");
        return;
    }
    else
    {
        printf(" enter the element: \n");
        scanf("%d",&item);
        r=r+1;
        q[r]=item;
    }
}
void delete()
{
    if(f>r)
    {
        printf(" queue is underflow! \n");
        return;
    }
    x=q[f];
    printf(" deleted element = %d\n",x);
    f=f+1;
}
void display()
{
    int i;
    if(f>r)
    {
        printf(" queue is underflow \n");
    }
    printf(" contents of the queue: \n");
    for(i=f; i<=r; i++)
    {
        printf(" %d \t",q[i]);
    }
    printf("\n");
}

void main()
{
    int ch;
    f=0;
    r=-1;

    while(1)
    {
        printf("1.insert\t2.delete\t3.display\t4.exit \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       4.exit
 enter the choice:
1
 enter the element:
1
1.insert        2.delete        3.display       4.exit
 enter the choice:
1
 enter the element:
2
1.insert        2.delete        3.display       4.exit
 enter the choice:
1
 enter the element:
3
1.insert        2.delete        3.display       4.exit
 enter the choice:
1
 enter the element:
4
1.insert        2.delete        3.display       4.exit
 enter the choice:
1
 enter the element:
5
1.insert        2.delete        3.display       4.exit
 enter the choice:
1
queue is overflow!
1.insert        2.delete        3.display       4.exit
 enter the choice:
3
 contents of the queue:
 1       2       3       4       5
1.insert        2.delete        3.display       4.exit
 enter the choice:
2
 deleted element = 1
1.insert        2.delete        3.display       4.exit
 enter the choice:
2
 deleted element = 2
1.insert        2.delete        3.display       4.exit
 enter the choice:
2
 deleted element = 3
1.insert        2.delete        3.display       4.exit
 enter the choice:
2
 deleted element = 4
1.insert        2.delete        3.display       4.exit
 enter the choice:
2
 deleted element = 5
1.insert        2.delete        3.display       4.exit
 enter the choice:
2
 queue is underflow!
1.insert        2.delete        3.display       4.exit
 enter the choice:
4

No comments:

Post a Comment