Queue in data structure using c programming?

All QuestionsCategory: C LanguageQueue in data structure using c programming?
chetan shidling asked 4 years ago

I need short information.

1 Answers
chetan shidling answered 4 years ago

#include <stdio.h>
#include <stdlib.h>
struct queue{
int size;
int f;
int r;
int * arr;
};
int isEmpty(struct queue *q){
if(q->r==q->f){
return 1;
}
return 0;
}
int isFull(struct queue *q){
if(q->r==q->size-1){
return 1;
}
return 0;
}
void enqueue(struct queue *q, int val){
if(isFull(q)){
printf(“This Queue is full\n”);
}
else{
q->r++;
q->arr[q->r]=val;
}
}
int dequeue(struct queue *q){
int a = -1;
if(isEmpty(q)){
printf(“This Queue is Empty\n”);
}
else{
q->f++;
a = q->arr[q->f];
}
return a;
}

int main()
{
int O, val;
struct queue q;
q.size = 100;
q.f = q.r = -1;
q.arr = (int*)malloc(q.size*sizeof(int));
printf(“1: For Enqueue\n”);
printf(“2: For Dequeue\n”);
printf(“3: To check empty\n”);
printf(“4: To check full\n”);
printf(“5: To exit\n”);
while(q.r != q.size-1){
printf(“Enter the operation you want to perform\n”);
scanf(“%d”, &O);
switch(O)
{
case 1:
printf(“Enter the element you want to store\n”);
scanf(“%d”,&val);
enqueue(&q, val);
break;
case 2:
printf(“Dequeueing element %d\n”, dequeue(&q));
break;
case 3:
if(isEmpty(&q)){
printf(“Queue is Empty\n”);
}
else
printf(“Not Empty\n”);
break;
case 4:
if(isFull(&q)){
printf(“Queue is Full\n”);
}
else
printf(“Not full\n”);
break;
default:
exit(0);
}
}
return 0;
}