0% found this document useful (0 votes)
6 views2 pages

Array Insertion and Deletion in C

The document contains a C program that demonstrates basic array operations including insertion and deletion at various positions (start, middle, end). It defines functions to print the array, insert an element, and delete an element, handling cases for invalid positions and full arrays. The main function initializes an array, performs multiple insertions and deletions, and prints the array after each operation.

Uploaded by

heerjambudiya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Array Insertion and Deletion in C

The document contains a C program that demonstrates basic array operations including insertion and deletion at various positions (start, middle, end). It defines functions to print the array, insert an element, and delete an element, handling cases for invalid positions and full arrays. The main function initializes an array, performs multiple insertions and deletions, and prints the array after each operation.

Uploaded by

heerjambudiya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include <stdio.

h>

void printArray(int arr[], int size) {


for(int i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}

int insertAt(int arr[], int size, int capacity, int element, int pos) {
if(size >= capacity) {
printf("Array is full, cannot insert.\n");
return size;
}
if(pos < 0 || pos > size) {
printf("Invalid position.\n");
return size;
}
// Shift elements right
for(int i = size - 1; i >= pos; i--) {
arr[i + 1] = arr[i];
}
arr[pos] = element;
return size + 1;
}

int deleteAt(int arr[], int size, int pos) {


if(size <= 0) {
printf("Array is empty, cannot delete.\n");
return size;
}
if(pos < 0 || pos >= size) {
printf("Invalid position.\n");
return size;
}
// Shift elements left
for(int i = pos; i < size - 1; i++) {
arr[i] = arr[i + 1];
}
return size - 1;
}

int main() {
int capacity = 100; // max size of array
int arr[100] = {10, 20, 30, 40, 50};
int size = 5;

printf("Original array: ");


printArray(arr, size);

// Insert at start
size = insertAt(arr, size, capacity, 5, 0);
printf("After inserting 5 at start: ");
printArray(arr, size);

// Insert at middle
int mid = size / 2;
size = insertAt(arr, size, capacity, 25, mid);
printf("After inserting 25 at middle: ");
printArray(arr, size);
// Insert at end
size = insertAt(arr, size, capacity, 60, size);
printf("After inserting 60 at end: ");
printArray(arr, size);

// Delete at start
size = deleteAt(arr, size, 0);
printf("After deleting at start: ");
printArray(arr, size);

// Delete at middle
mid = size / 2;
size = deleteAt(arr, size, mid);
printf("After deleting at middle: ");
printArray(arr, size);

// Delete at end
size = deleteAt(arr, size, size - 1);
printf("After deleting at end: ");
printArray(arr, size);

return 0;
}

You might also like