-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionDeletionArray.cpp
More file actions
80 lines (67 loc) · 1.18 KB
/
Copy pathInsertionDeletionArray.cpp
File metadata and controls
80 lines (67 loc) · 1.18 KB
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
// Array Insertion and Deletion
#include<iostream>
using namespace std;
class insertDel
{
int *array;
int size;
int index=0;
public:
insertDel(int n=0)
{
if(n<0) throw " Bad Initialiser";
size= n;
array = new int[size];
}
insertDel(const insertDel& m)
{
size= m.size;
array= new int[size];
}
~insertDel()
{
delete [] array;
}
void insert(int value);
void del();
void display();
};
void insertDel::insert(int value)
{
if(index==size) throw " Reached Capacity cannot add more elements ";
array[index]= value;
index++;
}
void insertDel::del()
{
if(index<=0) throw "Empty array Operation not possible ";
cout<<"\n Deleting element at position : "<<index-1<<" with value "<<array[index-1];
index--;
}
void insertDel::display()
{
cout<<"\n Displaying the array "<<endl;
for(int i=0;i<index;i++)
cout<<array[i]<<" ";
cout<<endl;
cout<<"\n Reached Index :"<<index<<endl;
}
// driver function
int main()
{
try
{
insertDel obj(5);
obj.insert(10);
obj.insert(20);
obj.display();
obj.del();
obj.del();
obj.display();
obj.del();
}
catch(const char *str)
{
cout<<str;
}
}