Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.5 kB
2
Indexable
Never
#include <stdio.h>
#include <stdlib.h>
struct Queue
{
    int size;
    int f;
    int r;
    int *arr;
};
int isFull(struct Queue *q)
{
    if (q->r == q->size - 1)
        return 1;
    return 0;
}
int isEmpty(struct Queue *q)
{
    if (q->r == q->f)
        return 1;
    return 0;
}
void enqueue(struct Queue *q, int val)
{
    if (isFull(q))
    {
        printf("This Queue is Full ");
    }
    else
    {
        q->r = q->r + 1;
        q->arr[q->r] = val;
    }
}
void dequeue(struct Queue *q)
{
    int a = 1;
    if (q->f == q->r) // IsEmpty()
    {
        printf("No element to queue");
    }
    else
    {
        q->f = q->f + 1;
        a = q->arr[q->f];
        // return a;
        printf("The value of the dequeue is %d\n", a);
    }
}

int main()
{
    struct Queue *q;
    q->size = 10;
    q->f = -1;
    q->r = -1;

    q->arr = (int *)malloc(q->size * sizeof(int));
    // if (isEmpty(q))
    // {
    //     printf("Queue is Empty");
    // }
    enqueue(q, 12);
    enqueue(q, 16);
    enqueue(q, 15);
    enqueue(q, 14);
    enqueue(q, 3);
    enqueue(q, 2);
    enqueue(q, 15);
    enqueue(q, 14);
    enqueue(q, 3);
    enqueue(q, 2);
    dequeue(q);
    dequeue(q);
    dequeue(q);
    dequeue(q);
    dequeue(q);
    dequeue(q);
    dequeue(q);
    dequeue(q);
    dequeue(q);

    if (isFull(q))
    {
        printf("Queue is Full");
    }
}