Queue Codes
Queue Codes
program. Let me add those insertion methods directly into the code for Circular Queue:
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
int queue[MAX];
int front = -1, rear = -1;
// Insert at Front
void insertFront(int element) {
if (isFull()) {
printf("Queue Overflow! Unable to insert %d at front\n", element);
} else {
if (front == -1) {
front = rear = 0; // If queue is empty, set both front and rear to 0
} else {
front = (front - 1 + MAX) % MAX; // Update front with wrap around
}
queue[front] = element;
printf("Inserted %d at front\n", element);
}
}
int main() {
// Test the Circular Queue operations
insertRear(10);
insertRear(20);
insertFront(30);
insertFront(40);
display(); // Expected output: 40 30 10 20
deleteFront();
deleteRear();
display(); // Expected output: 30 10
return 0;
}
Explanation of Insertions:
If the queue is empty, it sets both front and rear to 0 and inserts the element.
The rear pointer is then updated using modulo arithmetic to wrap around when necessary
(rear = (rear + 1) % MAX).
Similar to the rear insertion, it checks for full status using isFull().
The front pointer is updated with modulo arithmetic (front = (front - 1 + MAX) % MAX) to
maintain the circular nature of the queue.
The modulo operation ensures that when the front or rear pointer reaches the end of the
array, they wrap around to the start of the array.
This makes sure the queue behaves like a circular buffer and utilizes the available space
efficiently.
---
Here are the codes for Priority Queue and Double-Ended Queue (Deque) with explanations
for insertion and wrapping logic:
---
A Priority Queue is a type of queue where each element is assigned a priority. Elements with
higher priority are dequeued first.
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
int queue[MAX];
int front = -1, rear = -1;
int i;
if (front == -1) { // If the queue is empty
front = 0;
rear = 0;
queue[rear] = element;
} else {
for (i = rear; i >= front && priority > queue[i]; i--) {
queue[i + 1] = queue[i]; // Shift elements to make space for higher priority
}
queue[i + 1] = element;
rear++;
}
printf("Inserted %d with priority %d\n", element, priority);
}
int main() {
insert(10, 2);
insert(20, 1);
insert(30, 3);
display(); // Expected output: 30 10 20 (based on priority)
delete();
display(); // Expected output: 10 20
return 0;
}
1. Insertion: The insertion logic ensures that elements are inserted in descending order of
their priority. If the queue is full, no insertion occurs. The array is shifted to accommodate
higher priority elements.
2. Deletion: The element with the highest priority (front of the queue) is dequeued. After
deletion, the front pointer is incremented, and the queue is checked for emptiness.
---
A Deque is a data structure where elements can be added or removed from both the front
and rear ends.
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
int deque[MAX];
int front = -1, rear = -1;
int main() {
insertFront(10);
insertRear(20);
insertFront(30);
insertRear(40);
display(); // Expected output: 30 10 20 40
deleteFront();
deleteRear();
display(); // Expected output: 10 20
return 0;
}
1. Insertion at Front (insertFront): Adds an element at the front by adjusting the front pointer
with modulo arithmetic, ensuring it wraps around when needed.
2. Insertion at Rear (insertRear): Adds an element at the rear by adjusting the rear pointer
with modulo arithmetic.
3. Deletion from Front (deleteFront): Removes an element from the front and adjusts the
front pointer.
4. Deletion from Rear (deleteRear): Removes an element from the rear and adjusts the rear
pointer.
5. Overflow and Underflow: The queue checks for overflow and underflow conditions before
performing any operations.
Key Points:
Circular Queue Logic: Both Priority Queue and Deque use modulo arithmetic to handle the
circular nature and ensure pointers (front and rear) wrap around when they reach the limits
of the array.
Operations: The operations are adjusted for the circular nature, so the queue uses the
available space efficiently.