-
Notifications
You must be signed in to change notification settings - Fork 72
/
Array
73 lines (72 loc) · 949 Bytes
/
Array
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
#include <iostream>
using namespace std;
template<class T>
class Array
{
private:
T *A;
int size;
int length;
public:
Array()
{
size=10;
A=new T[10];
length=0;
}
Array(int sz)
{
size=sz;
length=0;
A=new T[size];
}
~Array()
{
delete []A;
}
void Display();
void Insert(int index,T x);
T Delete(int index);
};
template<class T>
void Array<T>::Display()
{
for(int i=0;i<length;i++)
cout<<A[i]<<" ";
cout<<endl;
}
template<class T>
void Array<T>::Insert(int index,T x)
{
if(index>=0 && index<=length)
{
for(int i=length-1;i>=index;i--)
A[i+1]=A[i];
A[index]=x;
length++;
}
}
template<class T>
T Array<T>::Delete(int index)
{
T x=0;
if(index>=0 && index<length)
{
x=A[index];
for(int i=index;i<length-1;i++)
A[i]=A[i+1];
length--;
}
return x;
}
int main()
{
Array<char> arr(10);
arr.Insert(0,'a');
arr.Insert(1,'c');
arr.Insert(2,'d');
arr.Display();
cout<<arr.Delete(0)<<endl;
arr.Display();
return 0;
}