Queue
Queue
Queue
A queue is a popular linear data structure that follows the First-In-First-Out (FIFO)
principle. It represents a collection of elements in which the first element added is the first one to
be removed. Queues are used in various scenarios where maintaining order is crucial, such as
scheduling processes, managing network packets, and handling requests. In this comprehensive
guide, we will explore the concept of queues, queue operations (enqueue, dequeue, peek), queue
implementation using arrays and linked lists, circular queues, priority queues, as well as
applications and use cases of queues in the C programming language.
Introduction to Queues
A queue is a linear data structure that follows the First-In-First-Out (FIFO) principle. It
represents a collection of elements in which elements are added at one end (rear) and removed
from the other end (front). Queues are commonly used in scenarios that require maintaining order
and fairness in processing elements.
Queue Operations (Enqueue, Dequeue, Peek)
Queues support the following operations:
Enqueue
The enqueue operation adds an element to the rear (end) of the queue. The newly added element
becomes the last one in the queue.
Dequeue
The dequeue operation removes the element from the front of the queue. The next element in the
queue becomes the new front.
Peek
The peek operation allows you to view the element at the front of the queue without removing it.
It is useful for inspecting the element or performing certain checks.
Queue Implementation (Array-based and Linked List-based)
Queues can be implemented using either arrays or linked lists.
Array-based Queue Implementation
In an array-based queue implementation, an array is used to store the elements. Two indices,
front and rear, keep track of the position of the front and rear elements.
Here's an example of an array-based queue implementation in C:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int data[MAX_SIZE];
int front;
int rear;
} Queue;
int main() {
Queue queue;
queue.front = 0;
queue.rear = -1;
enqueue(&queue, 1);
enqueue(&queue, 2);
enqueue(&queue, 3);
return 0;
}
Explanation:
The code you provided is a queue implementation in C. Let's break it down step by step:
1. Header inclusions:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int data[MAX_SIZE];
int front;
int rear;
} Queue;
3. Queue operations:
The code defines three functions to perform enqueue, dequeue, and peek operations on
the queue:
4. Main function:
int main() {
Queue queue;
queue.front = 0;
queue.rear = -1;
enqueue(&queue, 1);
enqueue(&queue, 2);
enqueue(&queue, 3);
return 0;
}
In a linked list-based queue implementation, a linked list is used to store the elements. The front
and rear of the queue are represented by the head and tail of the linked list, respectively.
Here's an example of a linked list-based queue implementation in C:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
typedef struct {
Node* front;
Node* rear;
} Queue;
if (queue->front == NULL) {
queue->front = newNode;
queue->rear = newNode;
} else {
queue->rear->next = newNode;
queue->rear = newNode;
}
}
queue->front = queue->front->next;
if (queue->front == NULL) {
queue->rear = NULL;
}
free(temp);
return data;
}
int main() {
Queue queue;
queue.front = NULL;
queue.rear = NULL;
enqueue(&queue, 1);
enqueue(&queue, 2);
enqueue(&queue, 3);
return 0;
}
Explanation:
1. Header inclusions:
#include <stdio.h>
#include <stdlib.h>
These are the same as the previous program. stdio.h provides input/output functions and stdlib.h
provides memory allocation functions (used for creating nodes).
typedef struct {
Node* front;
Node* rear;
} Queue;
3. Queue operations:
void enqueue(Queue* queue, int element) {
// ...
}
4. Main function:
int main() {
// ...
}
This is similar to the previous program's main function. It creates a Queue object, enqueues
elements, peeks and dequeues them, demonstrating how the queue functions work.