Bird
0
0
DSA Cprogramming

Enqueue Operation in DSA C

Choose your learning style9 modes available
Mental Model
Adding an item to the end of a queue means putting it behind all the others waiting in line.
Analogy: Imagine a line of people waiting to buy tickets. When a new person arrives, they go to the very end of the line.
front -> null
rear -> null
queue is empty
Dry Run Walkthrough
Input: queue initially empty, enqueue values 10, then 20
Goal: Add new values to the end of the queue, maintaining order
Step 1: enqueue 10 into empty queue
front -> [10] -> null
rear -> [10]
Why: First element becomes both front and rear because queue was empty
Step 2: enqueue 20 at the rear
front -> [10] -> [20] -> null
rear -> [20]
Why: New element added after rear, rear pointer updated to new node
Result:
front -> 10 -> 20 -> null
rear -> 20
Annotated Code
DSA C
#include <stdio.h>
#include <stdlib.h>

// Node structure for queue
typedef struct Node {
    int data;
    struct Node* next;
} Node;

// Queue structure with front and rear pointers
typedef struct Queue {
    Node* front;
    Node* rear;
} Queue;

// Create a new empty queue
Queue* createQueue() {
    Queue* q = (Queue*)malloc(sizeof(Queue));
    q->front = NULL;
    q->rear = NULL;
    return q;
}

// Enqueue operation: add value at rear
void enqueue(Queue* q, int value) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = value;
    newNode->next = NULL;

    if (q->rear == NULL) {
        // Queue empty, new node is front and rear
        q->front = newNode;
        q->rear = newNode;
        return;
    }

    // Link new node at rear and update rear pointer
    q->rear->next = newNode;
    q->rear = newNode;
}

// Print queue from front to rear
void printQueue(Queue* q) {
    Node* temp = q->front;
    while (temp != NULL) {
        printf("%d -> ", temp->data);
        temp = temp->next;
    }
    printf("null\n");
}

int main() {
    Queue* q = createQueue();

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

    printQueue(q);

    return 0;
}
if (q->rear == NULL) {
check if queue is empty to set front and rear to new node
q->rear->next = newNode;
link new node after current rear
q->rear = newNode;
update rear pointer to new node
OutputSuccess
10 -> 20 -> null
Complexity Analysis
Time: O(1) because enqueue adds at rear without traversing the queue
Space: O(1) because only one new node is created per enqueue
vs Alternative: Compared to array-based queue with shifting, linked list enqueue is faster as it avoids moving elements
Edge Cases
empty queue
new node becomes both front and rear
DSA C
if (q->rear == NULL) {
When to Use This Pattern
When you need to add items in order to a waiting line structure, use enqueue to add at the end efficiently.
Common Mistakes
Mistake: Not updating rear pointer after adding new node
Fix: Always set rear to the new node after linking it
Mistake: Not handling empty queue case separately
Fix: Check if rear is NULL and set front and rear to new node
Summary
Adds a new element to the end of the queue.
Use when you want to maintain order and add items at the back of a waiting line.
Remember to update both rear pointer and handle empty queue case.