0% found this document useful (0 votes)
13 views

Queue of Data Stracture and Algorithms by C - Exp...

Uploaded by

tasmiul02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Queue of Data Stracture and Algorithms by C - Exp...

Uploaded by

tasmiul02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Queues: A First-In-First-Out (FIFO) Data Structure

A queue is a linear data structure that follows the First-In-First-Out (FIFO) principle. This means
that the first element added to the queue is the first one to be removed. It's like a line of people
waiting for their turn, where the person who arrived first is the first to be served.
Basic Operations on Queues
1. Enqueue: Adds an element to the rear of the queue.
2. Dequeue: Removes an element from the front of the queue.
3. Front: Returns the element at the front of the queue without removing it.
4. Rear: Returns the element at the rear of the queue without removing it.
5. IsEmpty: Checks if the queue is empty.
6. IsFull: Checks if the queue is full.
Implementation of Queues in C
We can implement queues using arrays or linked lists. Here's an implementation using an array:

#include <stdio.h>
#include <stdlib.h>

#define MAX_SIZE 100

struct Queue {
int items[MAX_SIZE];
int front;
int rear;
};

void enqueue(struct Queue* q, int item) {


if (q->rear == MAX_SIZE - 1) {
printf("Queue Overflow\n");
return;
}
q->rear++;
q->items[q->rear] = item;
}

int dequeue(struct Queue* q) {


if (q->front == -1) {
printf("Queue Underflow\n");
return -1;
}
int item = q->items[q->front];
q->front++;
return item;
}
int front(struct Queue* q) {
if (q->front == -1) {
printf("Queue is empty\n");
return -1;
}
return q->items[q->front];
}

int rear(struct Queue* q) {


if (q->rear == -1) {
printf("Queue is empty\n");
return -1;
}
return q->items[q->rear];
}

int isEmpty(struct Queue* q) {


return q->front == -1;
}

int isFull(struct Queue* q) {


return q->rear == MAX_SIZE - 1;
}

int main() {
struct Queue q;
q.front = q.rear = -1;

enqueue(&q, 10);
enqueue(&q, 20);
enqueue(&q, 30);

printf("Dequeued item is %d\n", dequeue(&q));


printf("Front item is %d\n", front(&q));
printf("Rear item is %d\n", rear(&q));

return 0;
}

Applications of Queues
● Breadth-First Search (BFS): Used to explore all neighbor nodes at the present depth
prior to moving on to the next depth level.
● Print jobs: In operating systems, print jobs are often queued.
● Task scheduling: In multi-tasking systems, tasks can be queued for processing.
● Simulation: Queues are used to model real-world systems like traffic, customer service,
etc.
Key Points to Remember
● FIFO Principle: The first element added is the first one removed.
● Array Implementation: Has a fixed size limitation.
● Linked List Implementation: More flexible, can grow dynamically.
● Circular Queue: An efficient implementation to reuse space.
● Priority Queue: A queue where elements are prioritized based on a certain key.
By understanding the concepts of queues and their implementations, you can effectively solve
various programming problems and design efficient algorithms.
সোর্স
1. https://tableaupracticetest.com/data-structure-interview-questions-and-answers/
2. https://github.com/ab-max-h/queues
3. https://prepinsta.com/c-program/reverse-a-queue/

You might also like