Bird
0
0
DSA Cprogramming

Array Insertion at End in DSA C

Choose your learning style9 modes available
Mental Model
An array holds items in order, and inserting at the end means placing a new item right after the last one.
Analogy: Think of a row of boxes where you add a new box at the very end without moving any existing boxes.
Index: 0   1   2   3   4
Array: [1] [2] [3] [_] [_]
               โ†‘end
Dry Run Walkthrough
Input: array: [1, 2, 3, _, _], insert value 4 at end
Goal: Add the value 4 right after the last filled spot in the array
Step 1: Find the current end position (index 3) where insertion can happen
Index: 0   1   2   3   4
Array: [1] [2] [3] [_] [_]
               โ†‘end
Why: We need to know where the new value should go
Step 2: Place the value 4 at index 3
Index: 0   1   2   3   4
Array: [1] [2] [3] [4] [_]
                   โ†‘end
Why: Inserting at the end means putting the new value in the first empty spot
Step 3: Update the end position to index 4 for next insertion
Index: 0   1   2   3   4
Array: [1] [2] [3] [4] [_]
                   โ†‘end
Why: We move the end pointer forward to keep track of the next free spot
Result:
Index: 0   1   2   3   4
Array: [1] [2] [3] [4] [_]
                   โ†‘end
Annotated Code
DSA C
#include <stdio.h>

#define MAX_SIZE 5

// Function to insert value at the end of the array
void insertAtEnd(int arr[], int *size, int value) {
    if (*size >= MAX_SIZE) {
        printf("Array is full, cannot insert %d\n", value);
        return;
    }
    arr[*size] = value; // place value at current end
    (*size)++;          // move end forward
}

// Function to print the array
void printArray(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        printf("%d", arr[i]);
        if (i < size - 1) printf(" -> ");
    }
    printf(" -> null\n");
}

int main() {
    int arr[MAX_SIZE] = {1, 2, 3};
    int size = 3; // current number of elements

    insertAtEnd(arr, &size, 4);
    printArray(arr, size);

    return 0;
}
if (*size >= MAX_SIZE) {
check if array is full to avoid overflow
arr[*size] = value; // place value at current end
insert new value at the current end index
(*size)++; // move end forward
increment size to update end position
OutputSuccess
1 -> 2 -> 3 -> 4 -> null
Complexity Analysis
Time: O(1) because insertion at the end is a direct assignment without loops
Space: O(1) since no extra space is needed beyond the array
vs Alternative: Compared to inserting at the start or middle which may require shifting elements O(n), inserting at the end is faster and simpler
Edge Cases
array is already full
insertion is rejected with a message to avoid overflow
DSA C
if (*size >= MAX_SIZE) {
array is empty (size = 0)
insertion places the value at index 0 correctly
DSA C
arr[*size] = value;
When to Use This Pattern
When you need to add items to a collection in order without rearranging, reach for array insertion at end because it is simple and fast.
Common Mistakes
Mistake: Trying to insert without checking if the array is full, causing overflow
Fix: Always check if the current size is less than the maximum before inserting
Mistake: Not updating the size after insertion, causing next insertions to overwrite
Fix: Increment the size variable after placing the new value
Summary
It adds a new value at the next free spot at the end of the array.
Use it when you want to grow a list by adding items one after another.
Remember to check space and update the size to keep track of the end.