0% found this document useful (0 votes)
12 views2 pages

Assign 5.1

The document contains a C program that implements a circular queue using a struct. It includes functions for creating a queue, checking if it is full or empty, enqueuing and dequeuing elements, and displaying the queue's contents. The main function demonstrates the usage of these queue operations.

Uploaded by

kaverialla07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Assign 5.1

The document contains a C program that implements a circular queue using a struct. It includes functions for creating a queue, checking if it is full or empty, enqueuing and dequeuing elements, and displaying the queue's contents. The main function demonstrates the usage of these queue operations.

Uploaded by

kaverialla07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <stdio.

h>
#include <stdlib.h>

#define MAX_SIZE 100


typedef struct {
int arr[MAX_SIZE];
int front;
int rear;
int size;
} Queue;

Queue* createQueue() {
Queue* queue = (Queue*)malloc(sizeof(Queue));
if (queue == NULL) {
perror("Memory allocation failed");
exit(EXIT_FAILURE);
}
queue->front = 0;
queue->rear = -1;
queue->size = 0;
return queue;
}

int isFull(Queue* queue) {


return queue->size == MAX_SIZE;
}

int isEmpty(Queue* queue) {


return queue->size == 0;
}

void enqueue(Queue* queue, int value) {


if (isFull(queue)) {
printf("Queue is full. Cannot enqueue.\n");
return;
}
queue->rear = (queue->rear + 1) % MAX_SIZE;
queue->arr[queue->rear] = value;
queue->size++;
printf("%d enqueued to the queue.\n", value);
}

int dequeue(Queue* queue) {


if (isEmpty(queue)) {
printf("Queue is empty. Cannot dequeue.\n");
return -1; // Or some other error value
}
int dequeuedValue = queue->arr[queue->front];
queue->front = (queue->front + 1) % MAX_SIZE;
queue->size--;
printf("%d dequeued from the queue.\n", dequeuedValue);
return dequeuedValue;
}

void display(Queue* queue) {


if (isEmpty(queue)) {
printf("Queue is empty.\n");
return;
}
printf("Elements in the queue: ");
for (int i = 0; i < queue->size; i++) {
printf("%d ", queue->arr[(queue->front + i) % MAX_SIZE]); }
printf("\n");
}

int main() {
Queue* queue = createQueue();

enqueue(queue, 10);
enqueue(queue, 20);
enqueue(queue, 30);
display(queue);

dequeue(queue);
display(queue);

enqueue(queue, 40);
enqueue(queue, 50);
enqueue(queue, 60);
enqueue(queue, 70);
enqueue(queue, 80);
enqueue(queue, 90);
enqueue(queue, 100);
enqueue(queue, 110);
display(queue);

dequeue(queue);
dequeue(queue);
dequeue(queue);
dequeue(queue);
dequeue(queue);
dequeue(queue);
dequeue(queue);
dequeue(queue);
dequeue(queue);
dequeue(queue);
display(queue);

free(queue);
return 0;
}

You might also like