-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInsertion_array.c
92 lines (60 loc) · 1.4 KB
/
Insertion_array.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// #include <stdio.h>
// //Traversal
// void display(int arr[], int n){
// for (int i = 0; i < n; i++)
// {
// printf("%d\n",arr[i]);
// }
// }
// // Insertion
// int indInsertion(int arr[], int size, int element , int capacity, int index ){
// 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 size=6, element = 45, index = 1;
// int arr[100] = {1,2,3,4,5,6};
// // display(arr, 6);
// indInsertion(arr, size, element , 100, index);
// size+=1;
// display(arr, 6);
// return 0;
// }
//same
#include <stdio.h>
//Traversal
void display(int arr[], int n){
for (int i = 0; i < n; i++)
{
printf("%d ",arr[i]);
}
}
void indInsertion(int arr[], int size, int capacity,int element, int index){
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 element = 245,index=1,size=8;
int arr[100] = {1,2,3,4,5,45,6,65};
indInsertion(arr, size, 100, element, index);
size+=1;
display(arr, size);
return 0;
}