-
Notifications
You must be signed in to change notification settings - Fork 361
/
Copy path05_inserting_an_element.cpp
54 lines (51 loc) · 1.57 KB
/
05_inserting_an_element.cpp
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
#include <iostream>
using namespace std;
struct Array
{
int A[10];
int size;
int length;
};
void Display(struct Array arr)
{
int i;
cout<<"printing all the elements "<<endl;
for (i = 0; i < arr.length; i++)
cout << arr.A[i] << " ";
}
void Add(struct Array *arr , int x) // element to be inserted
{
if (arr->length < arr->size)
arr->A[arr->length++] = x; // increment the size of array and fit the elements // it will take the constant time
}
void Insert(struct Array* arr, int index, int x) // the index at which we want to enter // and the value we want tpo enter
{
int i;
for (i = arr->length; i > index; i--) // we will strat from last of the array and shift the elements upto the given index
{
arr->A[i] = arr->A[i - 1]; // shifting the elements to next // i is on some index and copy the value from previous index
}
arr->A[index] = x; // set the value at the particular index
arr->length++; // increment the size of length
// work done shifting of elelmets and copying elements
// best case : --------------- min : O(1) // there may be zero shifting
// worst case : --------------- max : O(n)// there may be n shifing
}
int main() {
int no;
struct Array arr;
cout << "Enter the size of the array " << endl;
cin >> arr.size;
arr.A = new int[arr.size];
arr.length = 0;
cout << "Enter the length of array" << endl;
cin >> no;
cout << "Enter all the elements of array" << endl;
for (int i = 0; i < no; i++)
cin >> arr.A[i];
arr.length = no;
Add(&arr, 10);
Insert(&arr,3,25);
Display(arr);
return 0;
}