Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Explain linear data structure queue in C language
A queue is a linear data structure that follows the First In First Out (FIFO) principle. In a queue, elements are inserted at the rear end and deleted from the front end, similar to a real-world queue where the first person to join the line is the first to be served.
Syntax
// Basic queue operations void enqueue(queue, item); // Insert element at rear int dequeue(queue); // Remove element from front int front(queue); // Get front element int isEmpty(queue); // Check if queue is empty int isFull(queue); // Check if queue is full
Basic Operations
- Enqueue − Insert an element at the rear of the queue
- Dequeue − Remove an element from the front of the queue
- Front − Get the front element without removing it
- Rear − Get the rear element without removing it
- isEmpty − Check if the queue is empty
- isFull − Check if the queue is full
Queue Conditions
- Queue Overflow − Occurs when trying to insert an element into a full queue
- Queue Underflow − Occurs when trying to delete an element from an empty queue
Example: Simple Array-based Queue Implementation
Here's a complete implementation of queue using arrays in C −
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
struct Queue {
int front, rear, size;
unsigned capacity;
int* array;
};
struct Queue* createQueue(unsigned capacity) {
struct Queue* queue = (struct Queue*)malloc(sizeof(struct Queue));
queue->capacity = capacity;
queue->front = queue->size = 0;
queue->rear = capacity - 1;
queue->array = (int*)malloc(queue->capacity * sizeof(int));
return queue;
}
int isFull(struct Queue* queue) {
return (queue->size == queue->capacity);
}
int isEmpty(struct Queue* queue) {
return (queue->size == 0);
}
void enqueue(struct Queue* queue, int item) {
if (isFull(queue)) {
printf("Queue Overflow! Cannot insert %d<br>", item);
return;
}
queue->rear = (queue->rear + 1) % queue->capacity;
queue->array[queue->rear] = item;
queue->size = queue->size + 1;
printf("%d enqueued to queue<br>", item);
}
int dequeue(struct Queue* queue) {
if (isEmpty(queue)) {
printf("Queue Underflow! Cannot dequeue from empty queue<br>");
return INT_MIN;
}
int item = queue->array[queue->front];
queue->front = (queue->front + 1) % queue->capacity;
queue->size = queue->size - 1;
return item;
}
int front(struct Queue* queue) {
if (isEmpty(queue))
return INT_MIN;
return queue->array[queue->front];
}
int rear(struct Queue* queue) {
if (isEmpty(queue))
return INT_MIN;
return queue->array[queue->rear];
}
void display(struct Queue* queue) {
if (isEmpty(queue)) {
printf("Queue is empty<br>");
return;
}
printf("Queue contents: ");
int i = queue->front;
int count = 0;
while (count < queue->size) {
printf("%d ", queue->array[i]);
i = (i + 1) % queue->capacity;
count++;
}
printf("<br>");
}
int main() {
struct Queue* queue = createQueue(5);
printf("=== Queue Operations Demo ===<br>");
/* Enqueue operations */
enqueue(queue, 10);
enqueue(queue, 20);
enqueue(queue, 30);
enqueue(queue, 40);
display(queue);
printf("Front element: %d<br>", front(queue));
printf("Rear element: %d<br>", rear(queue));
/* Dequeue operations */
printf("Dequeued: %d<br>", dequeue(queue));
printf("Dequeued: %d<br>", dequeue(queue));
display(queue);
/* Try overflow */
enqueue(queue, 50);
enqueue(queue, 60);
enqueue(queue, 70);
enqueue(queue, 80); // This should cause overflow
display(queue);
free(queue->array);
free(queue);
return 0;
}
=== Queue Operations Demo === 10 enqueued to queue 20 enqueued to queue 30 enqueued to queue 40 enqueued to queue Queue contents: 10 20 30 40 Front element: 10 Rear element: 40 Dequeued: 10 Dequeued: 20 Queue contents: 30 40 50 enqueued to queue 60 enqueued to queue 70 enqueued to queue Queue Overflow! Cannot insert 80 Queue contents: 30 40 50 60 70
Key Characteristics
| Property | Description |
|---|---|
| Order | FIFO (First In First Out) |
| Insertion | At rear end only |
| Deletion | At front end only |
| Time Complexity | O(1) for enqueue and dequeue |
| Space Complexity | O(n) where n is queue capacity |
Applications
- CPU scheduling in operating systems
- Handling requests in web servers
- Breadth-First Search (BFS) in graphs
- Buffer for data streams
- Print queue management
Conclusion
Queue is a fundamental linear data structure that implements FIFO ordering. It provides efficient O(1) insertion and deletion operations, making it ideal for scenarios where order preservation is crucial. The circular array implementation optimizes space usage by reusing freed positions.
Advertisements
