Coding Insertion Operation in Array in Data Structures in C language

preview_player
Показать описание
Array Insertion C code: In this video we will code insertion operation out of the operations described in the previous videos

Best Hindi Videos For Learning Programming:

►C Language Complete Course In Hindi -

►JavaScript Complete Course In Hindi -

►Django Complete Course In Hindi -

Follow Me On Social Media
Рекомендации по теме
Комментарии
Автор

Please share this video guys! It will make it easy for me to provide better and better free quality content!
Instagram: Instagram.com/CodeWithHarry

CodeWithHarry
Автор

Morning : Data structures
Evening : practice C programs
Harry bhai on fire 🔥🔥🔥

ankur
Автор

After writing the whole code for traversal, Harry be like "bilkul nhi bataunga ye aapko main".🤣🤣🤣🤣🤣

asmereg
Автор

Solution to problem in C:
#include <stdio.h>

void traversal(int arr[], int n)
{
// traversal of arrays
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}

int indexInsertion(int arr[], int size, int element, int capacity, int index)
{
// code for inserting elements
if (size >= capacity)
{
return -1;
}
for (int i = size - 1; i >= index; i--)
{
arr[i+1]=arr[i];
}
arr[index] = element;
return 1;
}

int main()
{
int arr[100] = {1, 2, 3, 4, 5, 6, 7, 8};
int size = 8, element = 45, index = 5, capacity = 100;
traversal(arr, size);
int result = indexInsertion(arr, size, element, capacity, index);
if (result == 1) {
size++;
traversal(arr, size);
} else {
printf("Error: could not insert element\n");
}
return 0;
}

prathamIsOp
Автор

Dude you are literally gonna bring a coding revolution 😅
Hats off to you for doing this noble work✌✌

aishwarya
Автор

This guy always strives to make best quality material for free. You deserve our respect bhai❤️👍

Shubham-jzec
Автор

I am from Bangladesh...
Watching your Web development videos, my computer related interest is being grown-up....
Love you Harry sir...❤

alnaheen
Автор

harry bhai bahut kuch seekhne ko mila hai aapse...lockdown ka full faida utha rha hun...COLLEGE SE JYADA TO AAPNE SiKHAYA HAI PROGRAMMING 💕💕😃

Rahul-vmpv
Автор

Thank you for uploading this valuable content...
It is taking me to think deeper now!

harshitachaurasia
Автор

Bro You Are Amazing. Thanks for the great content that you provide..Please i have a request please can you implement these data structures in c++ because most probably in interviews most of the people use c++ to code and it would be great if you do so.

atharvakulkarni
Автор

// Code In C++
#include<iostream>
using namespace std;
void display(int arr[], int n){
for (int i = 0; i < n; i++)
{
/* code */
cout<<arr[i]<<" ";
}

}
int ind_insertion(int arr[], int size, int element, int capacity, int index){
if(size>=capacity){
return -1;
}
for (int i = size-1; i>=index; i--)
{
/* code */
arr[i+1]=arr[i];
}
arr[index] = element;
return 1;
}
int main(){
int arr[100] = {7, 8, 12, 27, 88};
int size = 5, element = 45, index = 1;
display(arr, size);
cout<<endl;
size += 1;
int check_display = ind_insertion(arr, size, element, 100, index);
if(check_display == 1){
cout<<"Display Successful"<<endl;
display(arr, size);
}
else{
cout<<"Insertion Failed "<<endl;
}
return 0;
}

sidhantjha
Автор

14:27
int flag = indInsertion(arr, size, element, 100, index);
size += 1;
if ( flag == 1 )
display(arr, size);
else
printf("Insertion failed!!");

tanishadixit_
Автор

Data structure notes are 🔥🔥🔥
Data structure vidoes🔥🔥🔥🔥

Question
Автор

Firstly, thank you so much harry bhai❤❤ for providing free and bestest lecture.. I have question can i use c++ instead of c??? I'm preparing for competitive programming.

amitrao
Автор

Wow! Today's video is here.. Good timing harry sir.

bigbossottupdate
Автор

This code has a bug.
For example if we have an array = {1, 2, 3, 4, 5} and the max capacity is 10,
if we insert an element, lets say 8, at index 7 the array will become {1, 2, 3, 4, 5, 0, 0, 8}, now the array has 8 elements, but since the size counter only increased by one, therefore, when we use display function, the output will be 1, 2, 3, 4, 5, 0

This is what I came up with to fix this problem:

#include <iostream>

void display(int *arr, int size)
{
std::cout << "Array: ";
for (int i = 0; i < size; i++)
{
std::cout << arr[i] << ' ';
}
std::cout << std::endl;

}

int indInsert(int *arr, int index, int *size, int capacity, int value)
{
if((*size) >= capacity || index >= capacity)
{
std::cout << "Insertion failed." << std::endl;
return -1;
}

else if (index > ((*size) - 1))
{
arr[index] = value;
(*size) = index + 1;
std::cout << "Insertion successfull." << std::endl;
return 0;
}

else
{
capacity = (*size) + 1;
for (int i = (*size); (*size) >= index; (*size)--)
{
arr[(*size)] = arr[(*size) - 1];
}
arr[index] = value;
(*size) = capacity;
std::cout << "Insertion successfull." << std::endl;
return 0;
}
}

int main()
{
int arr[10] = {1, 2, 3, 4, 5};
int size = 5;

display(arr, size);
if(!(indInsert(arr, 7, &size, 10, 8)))
{
std::cout << "Updated array: ";
display(arr, size);
}

return 0;
}

Output:
1 2 3 4 5
Insertion successfull.
1 2 3 4 5 0 0 8

This code is not the best because I am still a beginner.

manwtf
Автор

this is motivating me to write better code EVERYDAY!!!!

chilledfries
Автор

you are the coolest coder on the block!
you are in my prayers! thank you so much ^_^

abeermoeed
Автор

harry bhai aap insan hi ho n aap ko kya kuchh nhi aata yrr kitne talented ho aap love you

shubhamvalmiki
Автор

Really most valuable video Thanks Harry Sir for notes😊

rachanakushwaha