forked from Code-Recursion/DataStructuresAndAlgorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathADT_in_cpp.cpp
83 lines (77 loc) · 1.37 KB
/
ADT_in_cpp.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
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
#include <iostream>
using namespace std;
template <class T>
class Array
{
private:
T *A;
int size;
int length;
public:
Array() // constructor
{
size = 10;
A = new T[size];
length = 0;
}
Array(int sz) // parameterized constructor
{
size = sz;
length = 0;
A = new T[size];
}
~Array()
{
delete[] A;
}
void Display();
void Insert(int index, T item);
void Delete(int index);
};
template <class T>
void Array<T>::Display()
{
cout << "Elements in array : " << endl;
for (int i = 0; i < length; i++)
cout << A[i] << " ";
cout << endl;
}
template <class T>
void Array<T>::Insert(int index, int item)
{
if (index >= 0 && index <= length)
{
for (int i = length - 1; i >= 0; i--)
{
A[i + 1] = A[i];
}
A[index] = item;
}
length++;
}
template <class T>
void Array<T>::Delete(int index)
{
if (index >= 0 && index < length)
{
T x = A[index];
for (int i = index; i < length; i++)
{
A[i] = A[i + 1];
}
length--;
}
}
int main()
{
Array<int> arr(10); // array object
arr.Insert(0, 5);
arr.Insert(0, 6);
arr.Insert(0, 8);
arr.Insert(0, 9);
arr.Insert(0, 10);
arr.Display();
arr.Delete(9);
arr.Display();
return 0;
}