QUEUE OPRATION IN C LANGUAGE
#include<stdio.h>
#include<stdlib.h>
struct queue
{
int *arr;
int f;
int r;
int size;
};
int isfull(struct queue *q ){
if(q->r==q->size-1){
return 1;
}
else
return 0;
}
int isempty(struct queue *q ){
if(q->r==q->f)
return 1;
else
return 0;
}
void enqueue(struct queue *q,int val){
if(isfull(q)){
printf("queue overflow \n");
}
else{
q->r++;
q->arr[q->r]==val;
printf("%d is stored in queue\n",val);
}
}
int dequeue(struct queue *q){
int a = -1;
if(isempty(q)){
printf("queue is underflow \n");
}
else {
q->f++;
a = q->arr[q->f];
}
return a;
}
void display(struct queue *q){
int i;
for(i=0;i < q->size;i++){
printf("%d\t",q->arr[i]);
printf("\n");
}
}
int main()
{ struct queue q;
q.size=5;
q.f=q.r=0;
q.arr=(int*) malloc(q.size*sizeof(int));
// if (isempty(q)){
// printf("empty");
// }
enqueue(&q, 15);
enqueue(&q, 12);
enqueue(&q, 12);
enqueue(&q, 12);
printf("%d is out from queue \n", dequeue(&q));
printf("%d is out from queue \n", dequeue(&q));
printf("%d is out from queue \n", dequeue(&q));
display(&q);
return(0);
}
0 Comments