A Queue is a linear data structure that follows the FIFO (First In, First Out) principle. The element that is added first to the queue is the first one to be removed. A queue is often compared to a line at a ticket counter, where the first person to join the line is the first one to get served.
The primary operations of a queue include:
A queue can be implemented using an array. Hereβs an example of how to implement a queue using an array in C:
#include#define MAX 5 int queue[MAX]; int front = -1; int rear = -1; void enqueue(int value) { if (rear == MAX - 1) { printf("Queue Overflow\n"); } else { if (front == -1) front = 0; rear++; queue[rear] = value; printf("Enqueued %d\n", value); } } int dequeue() { if (front == -1 || front > rear) { printf("Queue Underflow\n"); return -1; } else { int dequeued = queue[front]; front++; return dequeued; } } int peek() { if (front == -1 || front > rear) { printf("Queue is Empty\n"); return -1; } else { return queue[front]; } } int isEmpty() { return (front == -1 || front > rear); } int isFull() { return (rear == MAX - 1); } int main() { enqueue(10); enqueue(20); enqueue(30); printf("Dequeued: %d\n", dequeue()); printf("Front: %d\n", peek()); return 0; }
Queues are used in several applications, including:
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!