📘 Lesson  ·  Lesson 64

Queue using Linked List

Queue using Linked List

What is a Queue?

💡 Note

A queue is FIFO (First In First Out). A linked list makes it easy to add at the rear and remove from the front.

Program

C
#include <stdio.h>
#include <stdlib.h>
struct Node { int data; struct Node* next; };
struct Node *front = NULL, *rear = NULL;

void enqueue(int x) {
    struct Node* n = malloc(sizeof(struct Node));
    n->data = x; n->next = NULL;
    if (rear == NULL) front = rear = n;
    else { rear->next = n; rear = n; }
}
int dequeue() {
    if (front == NULL) return -1;
    int x = front->data;
    front = front->next;
    return x;
}
int main() {
    enqueue(10); enqueue(20);
    printf("%d\n", dequeue());   // 10
    return 0;
}
Output:
10

Summary

  • Queue is FIFO: enqueue adds at rear, dequeue removes from front.
  • Linked list grows dynamically without a fixed size.

Queue क्या है?

💡 Note

Queue FIFO (First In First Out) है। Linked list से rear पर जोड़ना और front से हटाना आसान होता है।

Program

C
#include <stdio.h>
#include <stdlib.h>
struct Node { int data; struct Node* next; };
struct Node *front = NULL, *rear = NULL;

void enqueue(int x) {
    struct Node* n = malloc(sizeof(struct Node));
    n->data = x; n->next = NULL;
    if (rear == NULL) front = rear = n;
    else { rear->next = n; rear = n; }
}
int dequeue() {
    if (front == NULL) return -1;
    int x = front->data;
    front = front->next;
    return x;
}
int main() {
    enqueue(10); enqueue(20);
    printf("%d\n", dequeue());   // 10
    return 0;
}
Output:
10

सारांश

  • Queue FIFO है: enqueue rear पर जोड़ता, dequeue front से हटाता है।
  • Linked list बिना fixed size dynamically बढ़ती है।
← Back to C Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

\n