14.10.25

Array Deletion and Shifting in C

Array Deletion and Shifting in C Programming


Code:

#include <stdio.h>

int main() {
    int magic_numbers[5];

    /* Initialize array */
    magic_numbers[0] = 5;
    magic_numbers[1] = 10;
    magic_numbers[2] = 100;
    magic_numbers[3] = 25;
    magic_numbers[4] = 10;

    /* Display original array */
    printf("Original Array:\n");
    printf("Element 1 = %d\n", magic_numbers[0]);
    printf("Element 2 = %d\n", magic_numbers[1]);
    printf("Element 3 = %d\n", magic_numbers[2]);
    printf("Element 4 = %d\n", magic_numbers[3]);
    printf("Element 5 = %d\n", magic_numbers[4]);

    int delete_position = 3; /* Delete 3rd element */
    printf("\nDeleting element at position %d and shifting.\n", delete_position);

    /* Shift elements manually */
    magic_numbers[2] = magic_numbers[3]; /* 4th → 3rd */
    magic_numbers[3] = magic_numbers[4]; /* 5th → 4th */

    printf("Element deleted and shift completed\n");

    /* Display modified array with NULL for deleted element */
    printf("\nModified Array:\n");
    printf("Element 1 = %d\n", magic_numbers[0]);
    printf("Element 2 = %d\n", magic_numbers[1]);
    printf("Element 3 = %d\n", magic_numbers[2]);
    printf("Element 4 = %d\n", magic_numbers[3]);
    printf("Element 5 = NULL\n"); /* Manually print NULL for the deleted element */

    printf("\nProgram Ended.\n");

    return 0;
}

Program Explanation

Step 1: Declaration and Initialization

An integer array magic_numbers[5] is declared to hold 5 elements.
Each element is assigned a value manually:

[5, 10, 100, 25, 10]

Step 2: Displaying Original Elements

Before performing deletion, the program prints all the array elements using printf statements.

Step 3: Selecting the Element to Delete

We specify the element position to delete:

int delete_position = 3;

This means the 3rd element (value 100) will be removed.

Step 4: Shifting the Elements

After deletion, all elements to the right of the deleted element are shifted 


magic_numbers[elementIndex] = magic_numbers[elementIndex + 1];

This fills the empty space in the array.

Step 5: Marking the Last Element as Empty

Since one element is removed, the last element is set to NULL  to represent that it no longer holds a valid value.

Step 6: Displaying the Updated Array

The modified array is displayed, showing that the 3rd element has been deleted and other elements have been shifted:

Original Array: [5, 10, 100, 25, 10]
Modified Array: [5, 10, 25, 10, NULL]

Output

Original Array:
Element 1 = 5
Element 2 = 10
Element 3 = 100
Element 4 = 25
Element 5 = 10

Deleting element at position 3 and shifting elements...

Element deleted and shifting completed successfully.

Modified Array:
Element 1 = 5
Element 2 = 10
Element 3 = 25
Element 4 = 10
Element 5 = NULL

Program Ended.

Conclusion

This program demonstrates a simple method to delete an element from an array in C by shifting subsequent elements to fill the vacant space.
Although arrays in C have a fixed size, this shifting approach provides an effective way to maintain data consistency after deletion.



No comments:

Post a Comment

rating System

Loading...

A Friendship Story of Learning Data Structures with C

Sr. No. DSU BLOGS CHAPTERS 1 Array Operations in C, The Group of Friendship (Create, Insert, Delete ...