You are on page 1of 3

#include <stdio.

h>
#define SIZE 5

int items[SIZE]; // even number


int odd[SIZE];
int waiting[SIZE]; // Waiting queue
int front = -1, rear = -1;
int front1 = -1, rear1 = -1;
int fr = -1, re = -1;

int isFull(int f, int r, int* queue) {


if ((f == 0 && r == SIZE - 1) || (f == r + 1)) return 1;
return 0;
}

int isEmpty(int f) {
return f == -1;
}

void enq(int element) {


if (isFull(front, rear, items))
printf("\n Queue is full \n");
else {
if (front == -1) front = 0;
rear = (rear + 1) % SIZE;
items[rear] = element;
}
}

int deq() {
int element;
if (isEmpty(front)) {
printf("\n Queue is empty \n");
return -1;
} else {
element = items[front];
if (front == rear) {
front = -1;
rear = -1;
} else {
front = (front + 1) % SIZE;
}
printf("\n Deleted element %d \n", element);
return element;
}
}

void display(int f, int r, int* queue) {


int i;
if (isEmpty(f))
printf(" \n Empty Queue\n");
else {
for (i = f; i != r; i = (i + 1) % SIZE) {
if (queue[i] != 0) {
printf("%d ", queue[i]);
}
}
if (queue[i] != 0) {
printf("%d ", queue[i]);
}
}
}

void enqe(int val) {


if (isFull(front1, rear1, odd)) {
waiting[++re] = val;
if (fr == -1) fr = 0;
printf("\n Queue is full, added to waiting queue: %d\n", val);
} else {
if (front1 == -1) front1 = 0;
rear1 = (rear1 + 1) % SIZE;
odd[rear1] = val;
}
}

void wd(int* queue) {


int i;
if (isEmpty(fr)) {
printf(" \n Waiting Queue is empty\n");
} else {
for (i = fr; i != re; i = (i + 1) % SIZE) {
if (queue[i] != 0) {
printf("%d ", queue[i]);
}
}
if (queue[i] != 0) {
printf("%d ", queue[i]);
}
}
}

void display1(int f, int r, int* queue) {


int i;
if (isEmpty(f))
printf(" \n Empty Queue\n");
else {
for (i = f; i != r; i = (i + 1) % SIZE) {
if (queue[i] != 0) {
printf("%d ", queue[i]);
}
}
if (queue[i] != 0) {
printf("%d ", queue[i]);
}
}
}

int main() {
int n, num;
printf("Enter the number of data need to enter: ");
scanf("%d", &n);

for (int i = 0; i < n; i++) {


printf("Enter the number: ");
scanf("%d", &num);

if (num % 2 == 0) {
enq(num);
} else {
enqe(num);
}
}

printf("even number :\n");


display(front, rear, items);
printf("\nodd number queue:\n");
display1(front1, rear1, odd);
printf("\nwaiting queue:\n");
wd(waiting);
}

You might also like