Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update circular-queue.md #1377

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions docs/Queue/circular-queue.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,99 @@ public class CircularQueue {
}
```


#### C Implementation

```C
#include <stdio.h>

#define SIZE 5

int items[SIZE];
int front = -1, rear = -1;

// Check if the queue is full
int isFull() {
return (front == (rear + 1) % SIZE) || (front == 0 && rear == SIZE - 1);
}

// Check if the queue is empty
int isEmpty() {
return front == -1;
}

// Add an element to the queue
void enQueue(int element) {
if (isFull()) {
printf("\nQueue is full!!\n");
} else {
if (isEmpty()) {
front = 0;
}
rear = (rear + 1) % SIZE;
items[rear] = element;
printf("\nInserted -> %d", element);
}
}

// Remove an element from the queue
int deQueue() {
if (isEmpty()) {
printf("\nQueue is empty!!\n");
return -1;
} else {
int element = items[front];
if (front == rear) { // Queue has only one element
front = -1;
rear = -1;
} else {
front = (front + 1) % SIZE;
}
printf("\nDeleted element -> %d\n", element);
return element;
}
}

// Display the elements in the queue
void display() {
if (isEmpty()) {
printf("\nEmpty Queue\n");
} else {
printf("\nFront -> %d\n", front);
printf("Items -> ");
for (int i = front; i != rear; i = (i + 1) % SIZE) {
printf("%d ", items[i]);
}
printf("%d ", items[rear]); // Print the last element
printf("\nRear -> %d\n", rear);
}
}

int main() {
deQueue(); // Attempt to dequeue from an empty queue

enQueue(1);
enQueue(2);
enQueue(3);
enQueue(4);
enQueue(5);

enQueue(6); // Attempt to enqueue into a full queue

display(); // Display the queue

deQueue(); // Dequeue one element
display(); // Display the queue again

enQueue(7); // Enqueue one element to test circular queue functionality
display(); // Display the queue

enQueue(8); // Attempt to enqueue into a full queue

return 0;
}

```
### Complexity

- **Time Complexity**:
Expand Down
Loading