-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynArray.h
More file actions
144 lines (118 loc) · 2.29 KB
/
Copy pathdynArray.h
File metadata and controls
144 lines (118 loc) · 2.29 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#ifndef _DYNARRAY_H_
#define _DYNARRAY_H_
#define BLOCK 10
typedef unsigned int uint;
template <class TYPE>
void SWAP(TYPE a, TYPE b)
{
TYPE tmp = a;
a = b;
b = tmp;
}
template <class TYPE>
class Vector
{
private:
TYPE* vect = nullptr;
uint capacity = BLOCK;
uint num_elements = 0;
public:
Vector()//empty constructor
{
vect = new TYPE[capacity];
}
Vector(const TYPE v1)//constructor
{
capacity = v1;
vect = new TYPE[capacity];
}
Vector(const Vector& v2)//cpy constructor
{
num_elements = v2.num_elements;
capacity = v2.capacity;
vect = new TYPE[capacity];
memcpy(v2.vect, vect, num_elements);
}
~Vector()//destructor
{
delete[] vect;
}
void push_back(const TYPE& val)//adding values at the end
{
if (num_elements == capacity)
{
capacity += BLOCK;
TYPE* vect2 = new TYPE[capacity];
for (uint i = 0; i < num_elements; ++i)
vect2[i] = vect[i];
delete[] vect;
vect = vect2;
}
vect[num_elements++] = val;
}
bool pop_back(TYPE& result) //pop back
{
if (num_elements > 0)
{
result = data[--num_elements];
return true;
}
return false;
}
void clear() //clear
{
num_elements = 0;
}
uint size() const // returning the number of elements
{
return num_elements;
}
bool empty() const //return if its empty
{
return num_elements == 0;
}
TYPE* front() // returning the first element
{
if (num_elements > 0)
return &(data[0]);
return nullptr;
}
const TYPE* front() const // returning the first element
{
if (num_elements > 0)
return &(data[0]);
return nullptr;
}
TYPE* back() //returning the last element
{
if (num_elements > 0)
return &data[num_elements - 1];
return nullptr;
}
const TYPE* back() const //returning the last element
{
if (num_elements > 0)
return &data[num_elements - 1];
return nullptr;
}
int bubble_sort()
{
int count_swap = 0;
bool didSwap = true;
while (didSwap)
{
didSwap = false;
for (uint i = 0; i < num_elements - 2; i++)
{
count_swap++;
if (vect[i] > vect[i + 1])
{
SWAP(vect[i], vect[i + 1]);
didSwap = true;
}
}
}
return count_swap;
}
};
#endif // !_DYNARRAY_H_