0% found this document useful (0 votes)
2 views

2

This document contains a C program that implements a circular queue for managing calls with functions to enqueue, dequeue, peek, and display the queue. It handles the case where the queue is full by removing the oldest call and provides time complexity analysis for each operation. The main function demonstrates the usage of the queue operations.

Uploaded by

shikha.mscit24
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)
2 views

2

This document contains a C program that implements a circular queue for managing calls with functions to enqueue, dequeue, peek, and display the queue. It handles the case where the queue is full by removing the oldest call and provides time complexity analysis for each operation. The main function demonstrates the usage of the queue operations.

Uploaded by

shikha.mscit24
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>
#define N 5 // Maximum queue size

int queue[N];
int front = -1, rear = -1;

// Function to add a call to the queue


void enqueue(int value) {
if ((rear + 1) % N == front) { // Queue is full, remove the oldest call
printf("Queue is full. Removing oldest call: %d\n", queue[front]);
front = (front + 1) % N;
}

if (front == -1) { // First element


front = rear = 0;
} else {
rear = (rear + 1) % N;
}
queue[rear] = value;
printf("Enqueued: %d\n", value);
}

// Function to remove and process a call from the queue


void dequeue() {
if (front == -1) {
printf("Queue is empty\n");
return;
}
printf("Processing call: %d\n", queue[front]);
if (front == rear) {
front = rear = -1; // Reset queue if last element is dequeued
} else {
front = (front + 1) % N;
}
}

// Function to view the call at the front of the queue


void peek() {
if (front == -1) {
printf("Queue is empty\n");
} else {
printf("Next call to process: %d\n", queue[front]);
}
}

// Function to display all calls in the queue


void display() {
if (front == -1) {
printf("Queue is empty\n");
return;
}
printf("Queue: ");
int i = front;
while (i != rear) {
printf("%d ", queue[i]);
i = (i + 1) % N;
}
printf("%d\n", queue[rear]);
}
int main() {
enqueue(101);
enqueue(102);
enqueue(103);
enqueue(104);
enqueue(105);
display();

enqueue(106); // Should remove 101 and add 106


display();

dequeue();
display();

peek();

return 0;
}

/*
Time Complexity Analysis:
- enqueue(): O(1) [Constant time insertion]
- dequeue(): O(1) [Constant time removal]
- peek(): O(1) [Constant time access]
- display(): O(n) [Iterating through the queue]
*/

You might also like