Header Ads Widget

Responsive Advertisement

Circular Queue Using Array

 



#include <stdio.h>
#include <stdlib.h>

struct circularQueue
{
    int size;
    int f;
    int r;
    int *arr;
};

int isFull(struct circularQueue *q)
{
    if ((q->r+1)%q->size == q->f)
    {
        return 1;
    }
    return 0;
}

int isEmpty(struct circularQueue *q)
{
    if (q->r == q->f)
    {
        return 1;
    }
    return 0;
}

void enqueue(struct circularQueue *q, int val)
{
    if (isFull(q))
    {
        printf("The queue is Full...\n");
    }
    else
    {
    q->r = (q->r+1) % q->size;
    q->arr[q->r] = val;
    }
    printf("The Enqueue element is : %d\n",val);
}

int Dequeue(struct circularQueue *q)
{  int a = -1;
    if (isEmpty(q))
    {
        printf("The queue is Empty...\n");
    }
    else
    {
    q->f = (q->f+1) % q->size;
    a = q->arr[q->f];
    }
    return a;
   
    //printf("The DEqueue element is : %d\n",a);
}

int main()
{
    struct circularQueue *q = (struct circularQueue *)malloc(sizeof(struct circularQueue));
    q->size = 4;
    q->f = q->r = 0;
    q->arr = (int *)malloc(q->size * sizeof(int));
   
    if(isEmpty(q)){
    printf("The Queue is Empty...\n");
    }

    enqueue(q,23);
    enqueue(q,3);
    enqueue(q,2);
    //enqueue(q,6);
    printf("The Dequeue Element is : %d\n",Dequeue(q));
    printf("The Dequeue Element is : %d\n",Dequeue(q));
    printf("The Dequeue Element is : %d\n",Dequeue(q));
    //printf("The Dequeue Element is : %d\n",Dequeue(q));
     enqueue(q,2);
      printf("The Dequeue Element is : %d\n",Dequeue(q));
    if(isEmpty(q)){
    printf("The Queue is Empty...\n");
    }
    // else{
    // printf("The Queue is Full ...\n");
    // }
    if(isFull(q)){
    printf("The Queue is Full...\n");
   }
    return 0;
}

Post a Comment

0 Comments