Queue-using-C-Data-Structure
admin
#Queue-using-C-Data-Structure
A queue is a linear data structure that operates on the First In, First Out (FIFO) principle. It plays a crucial role in computer science and real-world applications, such as handling requests in web servers, managing processes in operating systems, and simulating real-world lines.
In this article, we will cover the fundamentals of queues, their variations, and step-by-step implementations in C. Along the way, we’ll focus on SEO-optimized keywords and practical insights for high-ranking search results.
A queue organizes data in a linear format, with elements being added from one end (the rear) and removed from the other (the front). This FIFO principle ensures the element enqueued first is dequeued first.
For example:
front
: Tracks the element to be dequeued.rear
: Tracks the last inserted element.In this tutorial, we focus on the Priority Queue implemented using a linked list.
Below is an implementation of a Priority Queue using a linked list in C. This ensures flexibility in managing dynamic data while handling priorities efficiently.
#include <stdio.h>
#include <stdlib.h>
struct queue {
int no, pri;
struct queue *next;
};
struct queue *front = NULL, *rear = NULL;
void insert(int val, int pr) {
struct queue *p, *q;
q = (struct queue *)malloc(sizeof(struct queue));
q->no = val;
q->pri = pr;
if (front == NULL) {
q->next = NULL;
front = rear = q;
return;
}
p = front;
while ((p->next != NULL) && (p->next->pri <= q->pri)) {
p = p->next;
}
if (front->pri > q->pri) {
q->next = front;
front = q;
} else {
q->next = p->next;
p->next = q;
}
}
int deletefront() {
int val;
struct queue *q;
if (front == NULL) {
printf("\nQueue is Empty");
return -999;
}
q = front;
val = front->no;
if (front == rear) {
front = rear = NULL;
} else {
front = front->next;
}
free(q);
return val;
}
void show() {
struct queue *q = front;
if (front == NULL) {
printf("\nQueue is Empty");
return;
}
while (q != NULL) {
printf("%d (Priority: %d) -> ", q->no, q->pri);
q = q->next;
}
printf("NULL\n");
}
int main() {
int ch, n, pr;
do {
printf("\n1. Insert");
printf("\n2. Delete from Front");
printf("\n3. Show");
printf("\n0. Exit");
printf("\n\nEnter Your Choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("\nEnter Number: ");
scanf("%d", &n);
printf("\nEnter Priority: ");
scanf("%d", &pr);
insert(n, pr);
break;
case 2:
n = deletefront();
if (n != -999)
printf("\n%d deleted", n);
break;
case 3:
show();
break;
case 0:
break;
default:
printf("\nWrong Choice");
}
} while (ch != 0);
return 0;
}
val
) with a priority (pr
).Queues are a fundamental data structure with extensive applications in software development and problem-solving. Whether you’re implementing a Simple Queue, Circular Queue, or Priority Queue, understanding the underlying principles is essential.
The priority queue implementation in C presented above demonstrates how to dynamically manage data with varying priorities using linked lists. By mastering such techniques, developers can optimize performance and ensure scalability in their applications.
For more tutorials on data structures and algorithms, visit developerIndian.com.