Inserting or deleting an element at the of an array can be easily done. If we need to insert or remove an element in the middle of an array, half of the items must be shifted to accommodate the new element while maintaining the order of the other elements.
Inserting a new element in an array
A new element can be inserted at any position of an array if there is enough memory space allocated to accommodate the new element. Some of the elements should be shifted forward to keep the order of the elements.
Suppose we want to add an element 93 at the 3rd position in the following array.
After inserting all elements comes after 32 must be moved to the next location to accommodate the new element and to keep the order of the elements as follows.
Example: Inserting new element in an array
The following program demonstrates how to insert a new element at the specified position in an array.
#include <stdio.h> int main() { int arr[100]; int i, item, pos, size=7; printf("Enter 7 elements: "); // reading array for (i = 0; i < size; i++) scanf("%d",&arr[i]); // print the original array printf("Array before insertion: "); for (i = 0; i < size; i++) printf("%d ", arr[i]); printf("\n"); // read element to be inserted printf("Enter the element to be inserted: "); scanf("%d",&item); // read position at which element is to be inserted printf("Enter the position at which the element is to be inserted: "); scanf("%d",&pos); // increase the size size++; // shift elements forward for (i = size-1; i >= pos; i--) arr[i] = arr[i - 1]; // insert item at position arr[pos - 1] = item; // print the updated array printf("Array after insertion: "); for (i = 0; i < size; i++) printf("%d ", arr[i]); printf("\n"); return 0; }
Output
Enter 7 elements: 22 32 44 51 65 71 80 Array before insertion: 22 32 44 51 65 71 80 Enter the element to be inserted: 93 Enter the position at which the element is to be inserted: 3 Array after insertion: 22 32 93 44 51 65 71 80
Deleting an element from an array
Suppose we want to delete element 44 at the 3rd position in the following array.
After deletion, all elements coming after 44 must be moved to the previous location to fill the free space and to keep the order of the elements as follows.
Algorithm
- Find the position of the element to be deleted in the array.
- If the element is found,
Shift all elements after the position of the element by 1 position.
Decrement array size by 1. - If the element is not found: Print “Element Not Found”
Example: Deleting an element from an array
The following program demonstrates how to delete an element at the specified position in an array.
#include<stdio.h> int main() { int key, i, pos = -1, size=5; int arr[5] = {1, 20, 5, 78, 30}; printf("Array before deletion: "); for (i = 0; i < size; i++) printf("%d ", arr[i]); printf("\n"); printf("Enter element to delete: "); scanf("%d",&key); // traverse the array // if any element matches the key, store its position for(i = 0; i < size; i++) { if(arr[i] == key) { pos = i; break; } } if(pos != -1) { //shift elements backwards by one position for(i = pos; i < size - 1; i++) arr[i] = arr[i+1]; printf("Array after deletion: "); for(i = 0; i < size - 1; i++) printf("%d ",arr[i]); } else printf("Element Not Found\n"); return 0; }
Output
Array before deletion: 1 20 5 78 30 Enter element to delete: 20 Array after deletion: 1 5 78 30