Thursday, June 27, 2013

Write a C program function to represent linear queue operation.



#define N 50
struct queue {
                   int rear;
                   int front;
                   int Q[N];
                   };
typedef struct queue myqueue;
void makeempty (myqueue *pt)    //create an empty queue
{ pt->rear=-1;
 pt->front=0;
 }
int isfull (myqueue *pt)     //check whether the queue is full or not
{
if(pt->rear==N-1)
return(1);
else
return 0;
}
int isempty (myqueue *pt)   //check whether the queue is empty or not
{
if(pt->rear < pt->front)
return(1);
else
return 0;
}
void enqueue (myqueue *pt, int item)   //add the item in the queue
{
if(isfull(pt))
printf(“THE QUEUE IS FULL”);
else
{pt->rear++;  //increment the rear
pt->Q[pt->rear]=item;   //add the item
}
}
int dequeue (myqueue *pt)     //delete an item from the queue
{
int temp;
if(isempty(pt))
printf(“THE QUEUE IS EMPTY”);
else
{temp= pt->Q[pt->front];   //store front element in auxiliary variable
pt->front++;                  //increment ‘front’
return (temp);  
}
}
void display (myqueue *pt)                    //display all the items of the Queue
{
int i;
if(isempty(pt))
printf(“THE QUEUE IS EMPTY”);
else
{for(i=pt->front; i<=rear; i++)
printf(“%d\t”,pt->Q[i]);
}
}
void count (myqueue *pt)           //count the number of item in the queue
{
int i, cont=0;
if(isempty(pt))
printf(“THE QUEUE IS EMPTY”);
else
{for(i=pt->front; i<=rear; i++)
++cont;
return (cont);
}
}

No comments:

Post a Comment