-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicArray.cpp
More file actions
99 lines (95 loc) · 2.27 KB
/
DynamicArray.cpp
File metadata and controls
99 lines (95 loc) · 2.27 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <stdio.h>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
template<class T>
class List
{
public:
int Remove(unsigned int index){
try{
_Length--;
T** NewStartPoint = StartPoint;
StartPoint = new T * [_Length];
for (int i = 0,j=0; i < _Length+1; i++)
{
if(index!=i){
StartPoint[j] = NewStartPoint[i];
j++;
}
}
delete(NewStartPoint);
return 1;
}
catch (int myNum) {
return 0;
}
}
int GetIndex(unsigned int i) {
return (i >= _Length) ? 0 : *StartPoint[i];
}
void Read() {
for (int i = 0; i < _Length; i++) {
cout<< *StartPoint[i] << endl;
}
}
int Length() {
return _Length;
}
void Add(T value)
{
MemoryPointCreate(&value);
}
int * GetAll() {
int GetAllList[_Length];
for (int i = 0; i < _Length;i++) {
GetAllList[i] = *StartPoint[i];
}
return GetAllList;
}
private:
int _Length = 0;
T** StartPoint;
void MemoryPointCreate(T* value) {
T* NPoint = new T[1]; // (typeid(value))malloc(typeid(*value).name() + 1);
*NPoint = *value;
MemoryExpansion(NPoint);
}
void MemoryExpansion(T* NValue) {
if (_Length > 0) {
_Length++;
T** NewStartPoint = StartPoint;
StartPoint = new T * [_Length];
for (int i = 0; i < _Length-1; i++)
{
StartPoint[i] = NewStartPoint[i];
}
StartPoint[_Length - 1] = NValue;
delete(NewStartPoint);
}
else {
_Length++;
StartPoint = new T * [_Length];
StartPoint[0] = NValue;
}
}
};
int main()
{
List<int> array;
array.Add(10);
array.Add(20);
array.Add(30);
array.Add(40);
array.Add(50);
array.Add(60);
array.Add(70);
array.Add(80);
array.Remove(1);
for (int i = 0; i < array.Length(); i++)
{
cout << array.GetIndex(i)<<endl;
}
}