Monday 20 August 2012

program to implement queue using array


#include<stdio.h>
#define MAX 10
int que[MAX];
int rear=-1,front=-1;
void push();
void pop();
void tra();
main()
{
    int ch;
    while(1)
    {
    printf("\n..............MENU..............\n");
    printf("\nselect your choice\n");
    printf("1.insert\n");
    printf("2.delete\n");
    printf("3.dispaly\n");
    printf("4.exit\n");
    printf("enter your choice:");
    scanf("%d",&ch);
    switch(ch)
    {
        case 1:

        insert();
        break;
        case 2:
        del();

        break;
        case 3:
        disp();
        break;
        case 4:
        exit(1);
        default :
        printf("invalid choice\n");

    }
}}

void insert()
{
    int val;
    if(rear==MAX-1)
    {
        printf("queue overflow\n");
        return;
    }
    else
    {
        if(front==-1)
        front=0;

      printf("enter the element to be inserted:");
        scanf("%d",&val);
    rear++;
    que[rear]=val;
    }
}
void del()
{
    if(front==-1 || front>rear)
    {

        printf("queue underflow");
        return;
    }
    else
    {
        printf("element deleted from the queue is=%d",que[front]);
        front++;
    }
}
void disp()
{
    int i;
    if(front==-1 || front>rear)
    {
        printf("queue is empty\n");
        return;
    }
    else
    {
        printf("the elements in the queue are\n");
        for(i=front;i<=rear;i++)
        printf("%d\t",que[i]);
    }
}

                                     OUTPUT

..............MENU..............

select your choice
1.insert
2.delete
3.dispaly
4.exit
enter your choice:1
enter the element to be inserted:1

..............MENU..............

select your choice
1.insert
2.delete
3.dispaly
4.exit
enter your choice:1
enter the element to be inserted:2

..............MENU..............

select your choice
1.insert
2.delete
3.dispaly
4.exit
enter your choice:1
enter the element to be inserted:3

..............MENU..............

select your choice
1.insert
2.delete
3.dispaly
4.exit
enter your choice:3
the elements in the queue are
1       2       3
..............MENU..............

select your choice
1.insert
2.delete
3.dispaly
4.exit
enter your choice:2
element deleted from the queue is=1
..............MENU..............

select your choice
1.insert
2.delete
3.dispaly
4.exit
enter your choice:2
element deleted from the queue is=2
..............MENU..............

select your choice
1.insert
2.delete
3.dispaly
4.exit
enter your choice:2
element deleted from the queue is=3
..............MENU..............

select your choice
1.insert
2.delete
3.dispaly
4.exit
enter your choice:2
queue underflow
..............MENU..............

select your choice
1.insert
2.delete
3.dispaly
4.exit
enter your choice:4

Process returned 1 (0x1)   execution time : 36.864 s
Press any key to continue.

No comments:

Post a Comment