forked from paulmedok/SinglyLinkedList
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArray.h
89 lines (78 loc) · 1.48 KB
/
Array.h
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
#include "stdafx.h"
using namespace std;
class Array{
private:
int N;
int* pA;
public:
Array(int* Arr, int size);
void FillArray();
void ShowArray();
void PushBack(int item);
void PushBack(int* pArr, int item);
Array(const Array &Right);
void SortArray();
int* GetArrayPtr();
~Array();
};
Array::Array(int* Arr, int size) : N(5) {
cout << endl << "Array Constructor" << endl << endl;
pA = Arr;
N = size;
}
Array::Array(const Array &Right){
cout << endl << "Constructor Copying" << endl;
N = Right.N;
pA = new int[Right.N];
memcpy(pA, Right.pA, sizeof(int)*Right.N);
cout << "Array copy const " << this << " from " << &Right << endl;
}
int* Array::GetArrayPtr(){
return pA;
}
void Array::FillArray(){
for (int i = 0; i < N; i++){
pA[i] = rand() % 100;
}
}
void Array::ShowArray(){
cout << "Array:" << endl;
for (int i = 0; i < N; i++){
cout << pA[i] << " ";
}
cout << endl;
}
void Array::PushBack(int item){
int *temp = new int[N + 1];
memcpy(temp, pA, sizeof(int)*N);
temp[N] = item;
N++;
delete[]pA;
pA = temp;
}
void Array::PushBack(int *pArr, int item){
int *temp = new int[N + 1];
memcpy(temp, pA, sizeof(int)*N);
temp[N] = item;
N++;
delete[]pA;
pA = temp;
}
Array::~Array(){
cout << endl << "Array Destructor" << endl;
delete[] pA;
}
void Array::SortArray(){
int min = 0;
int temp = 0;
for (int j = 0; j < N; j++){
min = pA[0];
for (int i = j + 1; i < N; i++){
if (pA[j] > pA[i]){
min = pA[i];
pA[i] = pA[j];
pA[j] = min;
}
}
}
}