0% found this document useful (0 votes)
4 views3 pages

Understanding Queue Data Structures

Uploaded by

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

Understanding Queue Data Structures

Uploaded by

johncristoling05
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

🧩 What is a Queue?

A queue is a linear data structure that follows the FIFO (First In, First Out) principle —
➡️The first element inserted is the first one to be removed.

It’s like a line of people waiting at a ticket counter — the first person to enter the line is the first
to get served.

⚙️Basic Queue Operations


Operation Description
Enqueue(x) Add an element x to the rear (end) of the queue.
Dequeue() Remove an element from the front of the queue.
Front / Peek() Get the element at the front without removing it.
isEmpty() Check if the queue has no elements.
isFull() Check if the queue is full (in case of a fixed-size queue).

🧮 Queue Representation
Queues can be implemented using:

1. Arrays
2. Linked Lists

🧱 1. Array Implementation

 Uses two pointers:


o front (points to the first element)
o rear (points to the last element)

Example in C:

#define MAX 5
int queue[MAX];
int front = -1, rear = -1;

void enqueue(int x) {
if (rear == MAX - 1)
printf("Queue Overflow\n");
else {
if (front == -1) front = 0;
queue[++rear] = x;
}
}

void dequeue() {
if (front == -1 || front > rear)
printf("Queue Underflow\n");
else
front++;
}

🔗 2. Linked List Implementation

 Each node contains:


o data
o next pointer
 Efficient for dynamic queues.

struct Node {
int data;
struct Node* next;
};

struct Node *front = NULL, *rear = NULL;

void enqueue(int x) {
struct Node* temp = malloc(sizeof(struct Node));
temp->data = x;
temp->next = NULL;
if (rear == NULL) {
front = rear = temp;
return;
}
rear->next = temp;
rear = temp;
}

void dequeue() {
if (front == NULL) return;
struct Node* temp = front;
front = front->next;
if (front == NULL) rear = NULL;
free(temp);
}

🔄 Types of Queues
Type Description
Simple Queue Standard FIFO queue.
Type Description
Connects the last position back to the first — avoids wasted
Circular Queue
space.
Priority Queue Each element has a priority; highest priority is served first.
Deque (Double-Ended
Elements can be added or removed from both ends.
Queue)

Time Complexity
Operation Complexity
Enqueue O(1)
Dequeue O(1)
Peek O(1)
isEmpty O(1)

💡 Real-Life Examples
 Printer job scheduling
 Customer service queues
 Task scheduling in operating systems
 Breadth-First Search (BFS) in graphs

You might also like