Tazahindi

What is Queue in C | सी में क्यू क्या है- Complete Tutorial in Hindi

By Satyajit

What is Queue in C

अगर आप C Programming या Data Structure सीख रहे हैं तो Queue in C आपके लिए एक must-learn topic है। इस tutorial में आप Queue की basic definition से लेकर advanced implementation तक सब कुछ step by step सीखेंगे। आप जानेंगे कि FIFO (First In First Out) concept क्या होता है, Queue के types कौन-कौन से हैं, Enqueue और Dequeue operations कैसे काम करते हैं, और Queue को array तथा linked list दोनों की मदद से C language में कैसे implement करते हैं

साथ ही आपको real life examples, practical C programs, advantages, disadvantages और interview questions भी मिलेंगे, जिससे आपका concept clear और exam / interview ready हो जाएगा।

सी में क्यू क्या है (What is Queue in C Programming) ?

Queue in C एक linear data structure है जिसमें insertion operation rear end से होता है और deletion operation front end से।

Definition:
Queue is a linear data structure that follows the FIFO (First In First Out) principle.

Simple words में:
Queue एक line की तरह होती है जहाँ पहला आया व्यक्ति पहले बाहर जाता है।

सी में क्यू के प्रकार (Types of Queue in C)

1. Simple Queue

Basic queue जहाँ insertion rear से और deletion front से होता है।

2. Circular Queue

जब queue full होने पर भी खाली spaces reuse होती हैं, उसे Circular Queue कहते हैं।

3. Priority Queue

हर element को priority दी जाती है, high priority वाला element पहले process होता है।

4. Deque (Double Ended Queue)

इसमें insertion और deletion दोनों ends से possible होते हैं।

Basic Operations of Queue in C

Operation Description
Enqueue Element insert करना
Dequeue Element remove करना
Peek/ Front First element देखना
isEmpty Queue empty है या नहीं
isFull Queue full है या नहीं

यह भी पढ़ें: What is Stack in C | सी में स्टैक क्या है- Complete Tutorial हिंदी में

How to implement Queue in C using Array

Queue in C को array की मदद से implement करना बहुत आसान है। इसमें हम FIFO (First In First Out) rule follow करते हैं, यानी जो element पहले insert होगा वही सबसे पहले delete होगा।

Step 1: Declare Queue and Variables

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

  • queue[SIZE] → array जो queue को store करेगा
  • front → first element का index
  • rear → last element का index

Step 2: Enqueue Operation (Insertion)

void enqueue(int value) {
    if (rear == SIZE - 1) {
        printf("Queue is Full\n");
    } else {
        if (front == -1)
            front = 0;
        rear++;
        queue[rear] = value;
        printf("Inserted %d\n", value);
    }
}

Working:

  • अगर rear == SIZE-1 है → Queue Full
  • अगर queue empty है (front == -1) तो front = 0
  • rear++ करके element insert करते हैं

Step 3: Dequeue Operation (Deletion)

void dequeue() {
    if (front == -1 || front > rear) {
        printf("Queue is Empty\n");
    } else {
        printf("Deleted %d\n", queue[front]);
        front++;
    }
}

Working:

  • अगर queue empty है → deletion possible नहीं
  • Otherwise front element remove करके front++

Step 4: Display Queue

void display() {
    if (front == -1)
        printf("Queue is Empty\n");
    else {
        for (int i = front; i <= rear; i++)
            printf("%d ", queue[i]);
        printf("\n");
    }
}

Step 5: Main Function

int main() {
    enqueue(10);
    enqueue(20);
    enqueue(30);
    display();
    dequeue();
    display();
    return 0;
}

Output:

Inserted 10
Inserted 20
Inserted 30
10 20 30
Deleted 10
20 30

Queue in C Using Array – Complete Code with Explanation

#include <stdio.h>
#define MAX 5

int queue[MAX];
int front = -1, 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("%d inserted into queue\n", value);
    }
}

void dequeue()
{
    if(front == -1 || front > rear)
        printf("Queue Underflow\n");
    else
    {
        printf("%d removed from queue\n", queue[front]);
        front++;
    }
}

void peek()
{
    if(front == -1 || front > rear)
        printf("Queue is Empty\n");
    else
        printf("Front element is %d\n", queue[front]);
}

int main()
{
    enqueue(10);
    enqueue(20);
    enqueue(30);
    peek();
    dequeue();
    peek();
    return 0;
}

Code Explanation:

  • queue[MAX] – queue array define किया गया है
  • front = -1, rear = -1 – start में queue empty होती है
  • enqueue() – rear से element insert करता है
  • dequeue() – front से element remove करता है
  • peek() – front element दिखाता है
  • FIFO concept follow होता है – जो पहले आएगा वही पहले बाहर जाएगा

यह भी पढ़ें: Programming क्या है तथा programming कितने प्रकार के है

How to Implement Queue in C Using Linked List

Array implementation में size fixed होता है, जिससे overflow की problem आती है। इस problem को solve करने के लिए हम Queue in C using Linked List implement करते हैं, जिसमें dynamic memory allocation होती है।

Step 1: Define Node Structure

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

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

struct node *front = NULL;
struct node *rear  = NULL;

Step 2: Enqueue Operation (Insertion)

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

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

struct node *front = NULL;
struct node *rear  = NULL;

Step 3: Dequeue Operation (Deletion)

void dequeue()
{
    if(front == NULL)
        printf("Queue is Empty\n");
    else
    {
        struct node *temp = front;
        printf("%d removed from queue\n", front->data);
        front = front->next;

        if(front == NULL)
            rear = NULL;

        free(temp);
    }
}

Step 4: Peek Operation

void peek()
{
    if(front == NULL)
        printf("Queue is Empty\n");
    else
        printf("Front element is %d\n", front->data);
}

Step 5: Main Function

int main()
{
    enqueue(10);
    enqueue(20);
    enqueue(30);
    peek();
    dequeue();
    peek();
    return 0;
}

Output:

10 inserted into queue
20 inserted into queue
30 inserted into queue
Front element is 10
10 removed from queue
Front element is 20

Queue in C Using Linked List –Complete Code with Explanation

#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 = (struct node*)malloc(sizeof(struct node));
    newNode->data = value;
    newNode->next = NULL;

    if(rear == NULL)          // Queue is empty
        front = rear = newNode;
    else
    {
        rear->next = newNode;
        rear = newNode;
    }
    printf("%d inserted into queue\n", value);
}

void dequeue()
{
    if(front == NULL)
        printf("Queue is Empty\n");
    else
    {
        struct node *temp = front;
        printf("%d removed from queue\n", front->data);
        front = front->next;

        if(front == NULL)
            rear = NULL;

        free(temp);
    }
}

void peek()
{
    if(front == NULL)
        printf("Queue is Empty\n");
    else
        printf("Front element is %d\n", front->data);
}

int main()
{
    enqueue(10);
    enqueue(20);
    enqueue(30);
    peek();
    dequeue();
    peek();
    return 0;
}

Code Explanation:

  • struct node – linked list का node define करता है
  • front और rear – queue के starting और ending pointer हैं
  • enqueue() – rear से new node insert करता है
  • dequeue() – front से node remove करता है
  • peek() – front element show करता है
  • FIFO rule follow होता है – जो पहले आएगा वही पहले बाहर जाएगा

यह भी पढ़ें: PM Kisan Samman Nidhi Yojana 2025 – Beneficiary List, Eligibility, Online Apply

Applications of Queue in C

Queue का use बहुत जगह होता है:

  • CPU Scheduling in Operating System – Processes को FIFO order में execute करने के लिए Queue use होती है।
  • Printer Spooling System – Print commands queue में store होती हैं और one-by-one print होती हैं।
  • Call Center Waiting System – Customer calls को queue में रखा जाता है ताकि पहले call करने वाले को पहले attend किया जाए।
  • Online Ticket Booking System – Users की requests sequential order में process होती हैं।
  • Breadth First Search (BFS) in Graph – Graph traversal में nodes को visit करने के लिए Queue का use होता है।
  • Data Buffering – Streaming data या keyboard input को temporarily store करने के लिए Queue use की जाती है।
  • Task Scheduling in Multitasking Systems – Multiple tasks को proper order में execute करने के लिए।
  • Traffic Management Systems – Vehicles या signals को control करने के लिए FIFO based Queue concept apply किया जाता है।

Advantages of Queue in C

  • FIFO (First In First Out) rule follow करता है, जिससे processing fair और systematic रहती है।
  • Real life applications में बहुत useful – जैसे CPU scheduling, printer spooling, ticket booking systems, call center waiting list आदि।
  • Data loss की possibility कम होती है, क्योंकि elements orderly manner में process होते हैं।
  • C language में implement करना आसान है – array और linked list दोनों से।
  • Multi-tasking systems को support करता है, जहाँ multiple processes manage करने होते हैं।
  • Efficient resource utilization करता है – memory और processor time दोनों का सही use होता है।
  • Program structure को clear बनाता है, जिससे debugging और maintenance आसान हो जाती है।
  • Sequential data processing में best performance देता है।

Disadvantages of Queue in C

  • Random access possible नहीं होता, क्योंकि elements केवल front से delete और rear से insert होते हैं।
  • Array implementation में fixed size problem होती है – queue full होने पर overflow हो जाता है।
  • Memory wastage हो सकती है, especially simple queue में जब front आगे बढ़ जाता है और पीछे खाली space बच जाती है।
  • Insertion और deletion केवल defined ends से ही possible होते हैं, flexibility कम होती है।
  • Circular queue या linked list के बिना efficiency कम हो जाती है, simple queue जल्दी overflow दिखा देता है।
  • Debugging थोड़ा difficult हो सकता है अगर front और rear pointers properly manage न हों।

यह भी पढ़ें: अब AI नहीं, Superintelligence की बारी! Microsoft ला रहा है मेडिकल डायग्नोसिस में क्रांति

निष्कर्ष (Conclusion)

इस article में आपने detail में सीखा कि Queue in C क्या होता है, FIFO concept क्या है, Queue के types, operations, और C language में Queue program कैसे बनाते हैं।

अगर आप DSA या C programming सीख रहे हैं तो Queue एक बहुत ही important topic है। रोज practice करें और अपने concepts strong बनाइए।

FAQs

Q1. What is Queue in C?
Ans. Queue is a linear data structure following FIFO principle.

Q2. Difference between Stack and Queue?
Ans. Stack uses LIFO, Queue uses FIFO.

Q3. What is Circular Queue?
Ans. Queue where last position connects with first.

Q4. What are real life examples of Queue?
Ans. Bank line, printer jobs, call center waiting list.

Share with Social

Satyajit

Leave a Comment