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

Queue Codes

Uploaded by

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

Queue Codes

Uploaded by

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

No, I didn't include both Insert at Rear and Insert at Front in the previous Circular Queue

program. Let me add those insertion methods directly into the code for Circular Queue:

Full Circular Queue Code with Both Insertion Methods:

#include <stdio.h>
#include <stdlib.h>

#define MAX 5

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

// Function to check if the queue is full


int isFull() {
return (front == (rear + 1) % MAX);
}

// Function to check if the queue is empty


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

// Insert at Rear (Enqueue)


void insertRear(int element) {
if (isFull()) {
printf("Queue Overflow! Unable to insert %d at rear\n", element);
} else {
if (front == -1) {
front = 0; // If queue is empty, set both front and rear to 0
}
rear = (rear + 1) % MAX; // Update rear with wrap around
queue[rear] = element;
printf("Inserted %d at rear\n", element);
}
}

// 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);
}
}

// Function to delete from front (Dequeue)


void deleteFront() {
if (isEmpty()) {
printf("Queue Underflow! Unable to delete from front\n");
} else {
printf("Deleted %d from front\n", queue[front]);
if (front == rear) { // If only one element exists, reset the queue
front = rear = -1;
} else {
front = (front + 1) % MAX; // Update front with wrap around
}
}
}

// Function to delete from rear


void deleteRear() {
if (isEmpty()) {
printf("Queue Underflow! Unable to delete from rear\n");
} else {
printf("Deleted %d from rear\n", queue[rear]);
if (front == rear) { // If only one element exists, reset the queue
front = rear = -1;
} else {
rear = (rear - 1 + MAX) % MAX; // Update rear with wrap around
}
}
}

// Function to display the elements of the queue


void display() {
if (isEmpty()) {
printf("Queue is empty!\n");
} else {
int i = front;
printf("Queue elements: ");
while (i != rear) {
printf("%d ", queue[i]);
i = (i + 1) % MAX;
}
printf("%d\n", queue[rear]); // Print the last 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:

1. Insert at Rear (insertRear):

It checks if the queue is full using the isFull() function.

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).

2. Insert at Front (insertFront):

Similar to the rear insertion, it checks for full status using isFull().

If the queue is empty, it initializes front and rear to 0.

The front pointer is updated with modulo arithmetic (front = (front - 1 + MAX) % MAX) to
maintain the circular nature of the queue.

Key Logic for Wrapping:

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.
---

Let me know if you need further clarification!

Here are the codes for Priority Queue and Double-Ended Queue (Deque) with explanations
for insertion and wrapping logic:

---

Priority Queue using Array:

A Priority Queue is a type of queue where each element is assigned a priority. Elements with
higher priority are dequeued first.

Priority Queue Operations:

Insert: Insert elements based on their priority.

Delete (Dequeue): Remove the element with the highest priority.

Code for Priority Queue using Array:

#include <stdio.h>
#include <stdlib.h>

#define MAX 5

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

// Function to check if the queue is full


int isFull() {
return (rear == MAX - 1);
}

// Function to check if the queue is empty


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

// Function to insert an element based on priority


void insert(int element, int priority) {
if (isFull()) {
printf("Queue Overflow! Unable to insert %d\n", element);
return;
}

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);
}

// Function to delete the element with the highest priority


void delete() {
if (isEmpty()) {
printf("Queue Underflow! No elements to delete\n");
} else {
printf("Deleted %d from front\n", queue[front]);
front++;
if (front > rear) {
front = rear = -1; // Reset queue if empty
}
}
}

// Function to display the elements of the queue


void display() {
if (isEmpty()) {
printf("Queue is empty!\n");
} else {
printf("Queue elements: ");
for (int i = front; i <= rear; i++) {
printf("%d ", queue[i]);
}
printf("\n");
}
}

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;
}

Explanation for Priority Queue:

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.

---

Double-Ended Queue (Deque) using Array:

A Deque is a data structure where elements can be added or removed from both the front
and rear ends.

Code for Deque using Array:

#include <stdio.h>
#include <stdlib.h>

#define MAX 5

int deque[MAX];
int front = -1, rear = -1;

// Function to check if the deque is full


int isFull() {
return ((front == 0 && rear == MAX - 1) || (front == rear + 1));
}

// Function to check if the deque is empty


int isEmpty() {
return (front == -1);
}
// Function to insert at the front
void insertFront(int element) {
if (isFull()) {
printf("Deque Overflow! Unable to insert %d at front\n", element);
} else {
if (front == -1) {
front = rear = 0;
} else {
front = (front - 1 + MAX) % MAX; // Wrap around using modulo
}
deque[front] = element;
printf("Inserted %d at front\n", element);
}
}

// Function to insert at the rear


void insertRear(int element) {
if (isFull()) {
printf("Deque Overflow! Unable to insert %d at rear\n", element);
} else {
if (front == -1) {
front = rear = 0;
} else {
rear = (rear + 1) % MAX; // Wrap around using modulo
}
deque[rear] = element;
printf("Inserted %d at rear\n", element);
}
}

// Function to delete from the front


void deleteFront() {
if (isEmpty()) {
printf("Deque Underflow! Unable to delete from front\n");
} else {
printf("Deleted %d from front\n", deque[front]);
if (front == rear) {
front = rear = -1; // Reset deque if empty
} else {
front = (front + 1) % MAX; // Wrap around using modulo
}
}
}

// Function to delete from the rear


void deleteRear() {
if (isEmpty()) {
printf("Deque Underflow! Unable to delete from rear\n");
} else {
printf("Deleted %d from rear\n", deque[rear]);
if (front == rear) {
front = rear = -1; // Reset deque if empty
} else {
rear = (rear - 1 + MAX) % MAX; // Wrap around using modulo
}
}
}

// Function to display the elements of the deque


void display() {
if (isEmpty()) {
printf("Deque is empty!\n");
} else {
printf("Deque elements: ");
int i = front;
while (i != rear) {
printf("%d ", deque[i]);
i = (i + 1) % MAX;
}
printf("%d\n", deque[rear]); // Print the last element
}
}

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;
}

Explanation for Deque:

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.

Let me know if you need further clarification!

You might also like