How To Write A C Program For Deletion And Insertion?

Last updated on Nov 25,2020 48.7K Views

How To Write A C Program For Deletion And Insertion?

This article on C Program For Deletion And Insertion will introduce you to basics of deleting and inserting elements in a C array. Following pointers will be covered in this article,

So let us get started then

C Program For Deletion And Insertion

Arrays are the fundamentals of any programming language. To master any programming language, you need to be proficient with arrays. In this blog, we will learn how to perform basic operations such as insert, delete & search in an array using C programming language.

Let’s first understand how to search a given element in an array.

Searching an Element in an Array

To search an element in an array you need to traverse through the array using loop and search for the given element.

So let us continue with this C Program For Deletion And Insertion article,

C Function to Search an Element in an Array

int findElement(int array[], int size, int keyToBeSearched) 
{ 
int i; 
// Finding & returning the position of the element 
for (i = 0; i < size; i++) 
if (array[i] == keyToBeSearched) 
return i; 
return - 1; 
}

So let us continue with this C Program For Deletion And Insertion article,

C Program to Search an Element in an Array

#include<stdio.h> 
// Function to find the element for deletion
// where array[] is the array from which element needs to be deleted
// size is the size of the array
// keyToFind is the element to be deleted from the array
int findElement(int array[], int size, int keyToBeSearched) 
{ 
int i; 
// Finding & returning the position of the element 
for (i = 0; i < size; i++) 
if (array[i] == keyToBeSearched) 
return i; 
return - 1; 
}  
// Main Function 
int main() 
{ 
int array[] = { 31, 27, 3, 54, 67, 31 };
int size = sizeof(array) / sizeof(array[0]); 
int keyToBeSearched = 67; 
// Calling the function to delete an element from the array
int pos = findElement(array, size, keyToBeSearched); 
if(pos==-1){
printf("n Element %d not found", keyToBeSearched);
}
else{
printf("n Position of %d: %d", keyToBeSearched ,pos+1);
}
return 0; 
}

Output

Output- C Program to delete or Insert an Element in an Array- Edureka.png

So let us continue with this C Program For Deletion And Insertion article,

Inserting An Element in An Array

Inserting an element in an unsorted array is faster as compared to sorted array. This is because in an unsorted array, you do not have to worry about the new position of the element. The position of the new element is the last position in the array.

You need to check the total length of the array. If the array isn’t full, you can add the element at the last place.

Let’s look at the C function to insert an element in an array.

C function to Insert An Element in An Array

//Function to insert an element in an array.
//arr[] = array, elements = number of elements present in the array
//keyToBeInserted = element to be inserted in the array
// size of the array
int insertElement(int arr[], int elements, int keyToBeInserted, int size) 
{ 
// Check if the capacity of the array is already full 
if (elements >= size) 
return elements; 
//If not then the element is inserted at the last index 
//and the new array size is returned
arr[elements] = keyToBeInserted; 
return (elements + 1); 
}

So let us continue with this C Program For Deletion And Insertion article,

C Program to Insert An Element In An Array

Example

#include<stdio.h> 
//Function to insert an element in an array.
//arr[] = array, elements = number of elements present in the array
//keyToBeInserted = element to be inserted in the array
// size of the array
int insertElement(int arr[], int elements, int keyToBeInserted, int size) 
{ 
// Check if the capacity of the array is already full 
if (elements >= size) 
return elements; 
//If not then the element is inserted at the last index 
//and the new array size is returned
arr[elements] = keyToBeInserted;   
return (elements + 1); 
}  
// Main Function 
int main() 
{ 
int array[20] = { 31, 27, 3, 54, 67, 31 };
int size = sizeof(array) / sizeof(array[0]); 
int elements = 6; 
int i, keyToBeInserted = 32; 
printf("n Before Insertion: "); 
for (i = 0; i < elements; i++) 
printf("%d  ", array[i]); 
// Calling the function to insert the element in the array
elements = insertElement(array, elements, keyToBeInserted, size); 
printf("n After Insertion: "); 
for (i = 0; i < elements; i++) 
printf("%d  ",array[i]); 
return 0; 
}

Output:

Output- C Program to delete or Insert an Element in an Array- Edureka.png

Next we will understand how to delete an element from an array.

Deleting An Element From An Array

To delete an element in an array, we need to first search the element. Then we need to delete the element and shift the rest of the elements to the left.

Let’s look at the deletion function in C.

C Function to Delete An Element From An Array

// Function to delete an element
// where array[] is the array from which element needs to be deleted
// size is the size of the array
// keyToBeDeleted is the element to be deleted from the array
int deleteElement(int array[], int size, int keyToBeDeleted) 
{ 
// Calling findElement function to get the position of the element which needs to be deleted
int pos = findElement(array, size, keyToBeDeleted); 
// If element is not found then it prints Element not found
if (pos == - 1) 
{ 
printf("Element not found"); 
return size; 
} 
// Otherwise it deletes the element & moves rest of the element by one position
int i; 
for (i = pos; i < size - 1; i++) 
array[i] = array[i + 1]; 
return size - 1; 
} 

So let us continue with this C Program For Deletion And Insertion article,

C Program to Delete An Element From An Array

Example

#include<stdio.h> 
// Function to delete an element
// where array[] is the array from which element needs to be deleted
// size is the size of the array
// keyToBeDeleted is the element to be deleted from the array
int deleteElement(int array[], int size, int keyToBeDeleted) 
{ 
// Calling findElement function to get the position of the element which needs to be deleted
int pos = findElement(array, size, keyToBeDeleted); 
// If element is not found then it prints Element not found
if (pos == - 1) 
{ 
printf("Element not found"); 
return size; 
} 
// Otherwise it deletes the element & moves rest of the element by one position
int i; 
for (i = pos; i < size - 1; i++) 
array[i] = array[i + 1]; 
return size - 1; 
} 
// Function to find the element for deletion
int findElement(int array[], int size, int keyToBeDeleted) 
{ 
int i; 
// Finding & returning the position of the element 
for (i = 0; i < size; i++) 
if (array[i] == keyToBeDeleted) 
return i; 
return - 1; 
}   
// Main Function 
int main() 
{ 
int array[] = { 31, 27, 3, 54, 67, 31 };
int size = sizeof(array) / sizeof(array[0]); 
int i, keyToBeDeleted = 67; 
printf("n Before Deletion: "); 
for (i = 0; i < size; i++) 
printf("%d  ", array[i]); 
// Calling the function to delete an element from the array
size = deleteElement(array, size, keyToBeDeleted); 
printf("n After Deletion: "); 
for (i = 0; i < size; i++) 
printf("%d  ",array[i]); 
return 0; 
}

Output:

Output- C Program to delete or Insert an Element in an Array- Edureka.png

Now after going through the above programs you would have understood how to perform basic array operations in C language. This brings us to the end of this C Program For Deletion And Insertion article,

If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this article  and we will get back to you as soon as possible.

Comments
0 Comments

Join the discussion

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.