#include <stdio.h>
#include <stdlib.h>
#define MAX_CAPACITY 5
// Define the queue structure
typedef struct {
int data[MAX_CAPACITY];
int front;
int rear;
int size;
} Queue;
// Initialize the queue
void initQueue(Queue *q) {
q->front = 0;
q->rear = -1;
q->size = 0;
}
// Returns the current size of the queue
int size(Queue *q) {
return q->size;
}
// Pushes two values into the queue
void Push(Queue *q, int val1, int val2) {
if (q->size + 2 <= MAX_CAPACITY) {
q->rear = (q->rear + 1) % MAX_CAPACITY;
q->data[q->rear] = val1;
q->size++;
q->rear = (q->rear + 1) % MAX_CAPACITY;
q->data[q->rear] = val2;
q->size++;
} else {
printf("Overflow\n");
}
}
// Pops the front two elements and pushes their absolute difference
void pop(Queue *q) {
if (q->size < 2) {
printf("Underflow\n");
} else {
int val1 = q->data[q->front];
q->front = (q->front + 1) % MAX_CAPACITY;
q->size--;
int val2 = q->data[q->front];
q->front = (q->front + 1) % MAX_CAPACITY;
q->size--;
int diff = abs(val1 - val2);
if (q->size < MAX_CAPACITY) {
q->rear = (q->rear + 1) % MAX_CAPACITY;
q->data[q->rear] = diff;
q->size++;
} else {
printf("Overflow\n");
}
}
}
// Returns the front element of the queue
int front(Queue *q) {
if (q->size > 0) {
return q->data[q->front];
} else {
return -1; // Queue is empty
}
}
// Returns the back element of the queue
int back(Queue *q) {
if (q->size > 0) {
return q->data[q->rear];
} else {
return -1; // Queue is empty
}
}
// Main function to demonstrate the queue operations
int main() {
Queue q;
initQueue(&q);
// Demonstrate functionalities
Push(&q, 3, 5);
printf("Front: %d\n", front(&q));
printf("Back: %d\n", back(&q));
printf("Size: %d\n", size(&q));
Push(&q, 8, 2);
printf("Front: %d\n", front(&q));
printf("Back: %d\n", back(&q));
printf("Size: %d\n", size(&q));
pop(&q);
printf("After pop\n");
printf("Front: %d\n", front(&q));
printf("Back: %d\n", back(&q));
printf("Size: %d\n", size(&q));
Push(&q, 7, 1);
printf("After pushing more elements\n");
printf("Front: %d\n", front(&q));
printf("Back: %d\n", back(&q));
printf("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29sdXRlIGRpZmZlcmVuY2UKdm9pZCBwb3AoUXVldWUgKnEpIHsKICAgIGlmIChxLT5zaXplIDwgMikgewogICAgICAgIHByaW50ZigiVW5kZXJmbG93XG4iKTsKICAgIH0gZWxzZSB7CiAgICAgICAgaW50IHZhbDEgPSBxLT5kYXRhW3EtPmZyb250XTsKICAgICAgICBxLT5mcm9udCA9IChxLT5mcm9udCArIDEpICUgTUFYX0NBUEFDSVRZOwogICAgICAgIHEtPnNpemUtLTsKCiAgICAgICAgaW50IHZhbDIgPSBxLT5kYXRhW3EtPmZyb250XTsKICAgICAgICBxLT5mcm9udCA9IChxLT5mcm9udCArIDEpICUgTUFYX0NBUEFDSVRZOwogICAgICAgIHEtPnNpemUtLTsKCiAgICAgICAgaW50IGRpZmYgPSBhYnModmFsMSAtIHZhbDIpOwogICAgICAgIGlmIChxLT5zaXplIDwgTUFYX0NBUEFDSVRZKSB7CiAgICAgICAgICAgIHEtPnJlYXIgPSAocS0+cmVhciArIDEpICUgTUFYX0NBUEFDSVRZOwogICAgICAgICAgICBxLT5kYXRhW3EtPnJlYXJdID0gZGlmZjsKICAgICAgICAgICAgcS0+c2l6ZSsrOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHByaW50ZigiT3ZlcmZsb3dcbiIpOwogICAgICAgIH0KICAgIH0KfQoKLy8gUmV0dXJucyB0aGUgZnJvbnQgZWxlbWVudCBvZiB0aGUgcXVldWUKaW50IGZyb250KFF1ZXVlICpxKSB7CiAgICBpZiAocS0+c2l6ZSA+IDApIHsKICAgICAgICByZXR1cm4gcS0+ZGF0YVtxLT5mcm9udF07CiAgICB9IGVsc2UgewogICAgICAgIHJldHVybiAtMTsgLy8gUXVldWUgaXMgZW1wdHkKICAgIH0KfQoKLy8gUmV0dXJucyB0aGUgYmFjayBlbGVtZW50IG9mIHRoZSBxdWV1ZQppbnQgYmFjayhRdWV1ZSAqcSkgewogICAgaWYgKHEtPnNpemUgPiAwKSB7CiAgICAgICAgcmV0d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