C Basics ▶
1. Introduction to C
2. History & Features
3. Installation & Setup
4. Structure of C Program
5. Data Types
6. Variables
7. Constants
8. Operators
9. Input & Output
10. Control Statements
11. Loops
12. Break & Continue
13. Arrays
14. Strings
15. Functions
Pointers & Memory ▶
16. Pointers Basics
17. Pointer Arithmetic
18. Arrays & Pointers
19. Pointers to Functions
20. Dynamic Memory Allocation
21. malloc & calloc
22. realloc & free
23. Memory Leaks
24. 2D Arrays & Pointers
25. Pointer Mini Project
Structures, Unions & Files ▶
26. Structures
27. Nested Structures
28. Unions
29. Enums
30. typedef
31. File Handling Basics
32. File Operations
33. Reading & Writing Files
34. Binary Files
35. File Handling Project
Data Structures & Algorithms ▶
36. Introduction to DSA
37. Linked Lists
38. Stacks
39. Queues
40. Trees
41. Binary Search Tree
42. Sorting Algorithms
43. Searching Algorithms
44. Graph Basics
45. DSA Mini Project
Advanced C & Projects ▶
Queues in C
After learning stacks, we now study another important linear data structure called a Queue.
Queues follow a different rule than stacks and are used heavily in real-world systems.
What Is a Queue?
A queue is a linear data structure that follows:
FIFO – First In, First Out
The element inserted first is removed first.
Real-World Example
Think of a queue at a ticket counter:
- First person in line is served first
- New people join from the back
This is exactly how a queue works.
Basic Queue Operations
- Enqueue – Insert an element at the rear
- Dequeue – Remove an element from the front
- Front – View the front element
Queue Implementation Using Array
#include <stdio.h>
#define SIZE 5
int queue[SIZE];
int front = -1, rear = -1;
void enqueue(int value) {
if (rear == SIZE - 1) {
printf("Queue Overflow\n");
return;
}
if (front == -1)
front = 0;
queue[++rear] = value;
}
void dequeue() {
if (front == -1 || front > rear) {
printf("Queue Underflow\n");
return;
}
printf("Dequeued: %d\n", queue[front++]);
}
int main() {
enqueue(10);
enqueue(20);
enqueue(30);
dequeue();
dequeue();
return 0;
}
Limitations of Array Queue
- Fixed size
- Unused spaces after dequeuing
To overcome this, linked lists are used.
Queue Using Linked List
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
struct Node *front = NULL;
struct Node *rear = NULL;
void enqueue(int value) {
struct Node *newNode = malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
if (rear == NULL) {
front = rear = newNode;
return;
}
rear->next = newNode;
rear = newNode;
}
void dequeue() {
if (front == NULL) {
printf("Queue Underflow\n");
return;
}
struct Node *temp = front;
printf("Dequeued: %d\n", temp->data);
front = front->next;
if (front == NULL)
rear = NULL;
free(temp);
}
int main() {
enqueue(5);
enqueue(15);
enqueue(25);
dequeue();
dequeue();
return 0;
}
Where Queues Are Used
- CPU scheduling
- Printer spooling
- Network traffic
- Task scheduling
Mini Practice
- Create a queue using array
- Implement enqueue and dequeue
- Display front element
Quick Quiz
Q1. What does FIFO mean?
First In, First Out
Q2. Which operation inserts an element?
Enqueue
Q3. Which operation removes an element?
Dequeue
Q4. Which queue implementation removes size limitation?
Linked list
Q5. Are queues used in operating systems?
Yes