Saturday, October 20, 2018

c program for queue

/*Queues are frequently used in computer programming, and a typical example is the creation of a job queue by an operating system. If the operating system does not use priorities, then the jobs are processed in the order they enter the system. 
Write C program for simulating job queue.
Write functions to add job and delete job from queue.
*/

#include<stdio.h>
struct queue
{
        int rear,front;
        int a[10];
};

struct queue q;

void init()
{
        q.rear=0;
        q.front=-1;
}

int isfull()
{
        if(q.rear>=10)
        {
                return 1;
        }
        else
        {
                return 0;
        }
}

int isempty()
{
        if((q.rear-q.front)<=1)
        {
                return 1;
        }
        else
        {
                return 0;
        }
}

void insert(int x)
{
        q.a[q.rear]=x;
        q.rear++;
}

int del()
{
        q.front++;
        return q.a[q.front];
}

void list()
{
        int i;
        for(i=q.front+1;i<q.rear;i++)
        {
                printf("\n%d",q.a[i]);
        }
}

void main()
{
        int x;
        int ch;
       
        init();
       
        do{
                printf("\n1.Insert job ID\n2.delete job ID\n3.list\n0.exit");
                printf("\nEnter choice:");
                scanf("%d",&ch);
               
                switch(ch)
                {
                        case 1:
                                if(isfull()==1)
                                {
                                        printf("\nqueue is overflow");
                                }
                                else
                                {
                                        printf("\nEnter job ID:");
                                        scanf("%d",&x);
                                       
                                        insert(x);
                                }
                                break;
                               
                        case 2:
                                if(isempty()==1)
                                {
                                        printf("\nQueue is empty");
                                }
                                else
                                {
                                        printf("\nDeleted %d",del());
                                }
                                break;
                               
                        case 3:
                                printf("\nQueue");
                                list();
                                break;
                               
                        case 0:
                                printf("\nExit");
                                break;
                               
                        default:
                                printf("\nInvalid choice");
                }
        }while(ch!=0);
       
       
}

/*
output-

1.Insert job ID
2.delete job ID
3.list
0.exit
Enter choice:1

Enter job ID:120

1.Insert job ID
2.delete job ID
3.list
0.exit
Enter choice:1

Enter job ID:320

1.Insert job ID
2.delete job ID
3.list
0.exit
Enter choice:1

Enter job ID:520

1.Insert job ID
2.delete job ID
3.list
0.exit
Enter choice:1

Enter job ID:650

1.Insert job ID
2.delete job ID
3.list
0.exit
Enter choice:3

Queue
120
320
520
650
1.Insert job ID
2.delete job ID
3.list
0.exit
Enter choice:2

Deleted 120
1.Insert job ID
2.delete job ID
3.list
0.exit
Enter choice:3

Queue
320
520
650
1.Insert job ID
2.delete job ID
3.list
0.exit
Enter choice:0

*/

No comments:

Post a Comment