The operations of a queue in C involve managing elements in a FIFO (First In, First Out) manner. These operations can be implemented using arrays or linked lists. Below is an explanation of each operation with examples.
1. Enqueue Operation
The enqueue operation inserts an element at the rear (end) of the queue.
Steps:
- Check if the queue is full (in case of an array-based implementation).
- If not full, increment the rear and add the element at queue[rear].
Code Example:
void enqueue(int element) {
if (rear == SIZE – 1) { // Queue full condition for array
printf(“Queue is full.\n”);
} else {
if (front == -1) front = 0; // Initialize front if it’s the first element
rear++;
queue[rear] = element;
printf(“Enqueued: %d\n”, element);
}
}
2. Dequeue Operation
The dequeue operation removes an element from the front of the queue.
Steps:
- Check if the queue is empty.
- Remove the element at queue[front] and increment the front.
- If front surpasses rear, reset both to -1 (empty queue).
Code Example:
void dequeue() {
if (front == -1 || front > rear) { // Queue empty condition
printf(“Queue is empty.\n”);
} else {
printf(“Dequeued: %d\n”, queue[front]);
front++;
if (front > rear) { // Reset queue when all elements are removed
front = -1;
rear = -1;
}
}
}
3. Peek Operation
The peek operation retrieves the element at the front without removing it.
Steps:
- Check if the queue is empty.
- Return the value at queue[front].
Code Example:
void peek() {
if (front == -1) { // Queue empty condition
printf(“Queue is empty.\n”);
} else {
printf(“Front element: %d\n”, queue[front]);
}
}
4. IsEmpty Operation
The isEmpty operation checks if the queue has no elements.
Steps:
- Return true if front == -1, otherwise false.
Code Example:
int isEmpty() {
return (front == -1);
}
5. IsFull Operation
The isFull operation checks if the queue is full (in array implementation).
Steps:
- Return true if rear == SIZE – 1, otherwise false.
Code Example:
int isFull() {
return (rear == SIZE – 1);
}
6. Display Operation
The display operation prints all elements in the queue.
Steps:
- Traverse from front to rear and print each element.
Code Example:
void display() {
if (front == -1 || front > rear) { // Queue empty condition
printf(“Queue is empty.\n”);
} else {
printf(“Queue elements: “);
for (int i = front; i <= rear; i++) {
printf(“%d “, queue[i]);
}
printf(“\n”);
}
}
Example Program: Array-based Queue
#include <stdio.h>
#define SIZE 5
int queue[SIZE];
int front = -1, rear = -1;
void enqueue(int element) {
if (rear == SIZE – 1) {
printf(“Queue is full.\n”);
} else {
if (front == -1) front = 0;
rear++;
queue[rear] = element;
printf(“Enqueued: %d\n”, element);
}
}
void dequeue() {
if (front == -1 || front > rear) {
printf(“Queue is empty.\n”);
} else {
printf(“Dequeued: %d\n”, queue[front]);
front++;
if (front > rear) { // Reset queue
front = -1;
rear = -1;
}
}
}
void peek() {
if (front == -1) {
printf(“Queue is empty.\n”);
} else {
printf(“Front element: %d\n”, queue[front]);
}
}
void display() {
if (front == -1 || front > rear) {
printf(“Queue is empty.\n”);
} else {
printf(“Queue elements: “);
for (int i = front; i <= rear; i++) {
printf(“%d “, queue[i]);
}
printf(“\n”);
}
}
int main() {
enqueue(10);
enqueue(20);
enqueue(30);
display();
dequeue();
display();
peek();
return 0;
}
Example Program: Queue Using Linked List
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
Node* front = NULL;
Node* rear = NULL;
void enqueue(int element) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = element;
newNode->next = NULL;
if (rear == NULL) {
front = rear = newNode;
} else {
rear->next = newNode;
rear = newNode;
}
printf(“Enqueued: %d\n”, element);
}
void dequeue() {
if (front == NULL) {
printf(“Queue is empty.\n”);
} else {
Node* temp = front;
printf(“Dequeued: %d\n”, front->data);
front = front->next;
if (front == NULL) rear = NULL; // Reset rear when queue becomes empty
free(temp);
}
}
void display() {
if (front == NULL) {
printf(“Queue is empty.\n”);
} else {
Node* temp = front;
printf(“Queue elements: “);
while (temp != NULL) {
printf(“%d “, temp->data);
temp = temp->next;
}
printf(“\n”);
}
}
int main() {
enqueue(10);
enqueue(20);
enqueue(30);
display();
dequeue();
display();
return 0;
}
Summary
- Enqueue: Adds elements to the rear.
- Dequeue: Removes elements from the front.
- Peek: Returns the front element.
- IsEmpty/IsFull: Checks the queue’s status.
- Display: Prints the current elements.
Both array and linked-list implementations are effective; however, linked lists are more flexible in terms of dynamic memory usage.