-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayList-Practice.cpp
More file actions
79 lines (78 loc) · 1.92 KB
/
ArrayList-Practice.cpp
File metadata and controls
79 lines (78 loc) · 1.92 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
#include <iostream>
using namespace std;
class TEST{
private:
int data[10] = {1,5};
int length = 0;
int p = 0;
int preCount(){
for(p;p<10;p++){
if(data[p] != NULL) length++;
else ;
}
//cout<<"COUNT:"<<length<<endl;
return length;
}
public:
void insertValue(int value,int index){
preCount();
if(length>=index) {
for(int i = length;i > index;i--){
data[i] = data[i-1];
}
data[index] = value;
}
else data[length] = value;
length++;
}
int valueFinder(int value){
preCount();
for(int i = 0;i < length;i++){
if (data[i] != value);
if(data[i] == value) return i;
}
}
void changeValue(int oldValue,int newValue){
int j = valueFinder(oldValue);
if(oldValue != newValue && j != -1)
{
data[j] = newValue;
}
}
void removeValue(int value){
preCount();
int j = valueFinder(value);
data[j] = NULL;
for(int i = j;i < length;i++){
data[i] = data[i+1];
}
for(int i = 0;i<length;i++){
if(data[i] == NULL) length--;
}
}
void outputValue(){
preCount();
for(int i = 0;i < length;i++){
cout<<data[i]<<" ";
}
cout<<endl;
}
};
int main(){
TEST a;
a.outputValue();//1,5
a.insertValue(99,2);
a.outputValue();//1,5,99
a.changeValue(99,27);
a.outputValue();//1,5,27
a.insertValue(8,9);
a.outputValue();//1,5,27,8
a.changeValue(8,3);
a.outputValue();//1,5,27,3
a.removeValue(8);
a.outputValue();//1,5,27,3
a.insertValue(8,0);
a.outputValue();//8,1,5,27,3
a.removeValue(8),a.removeValue(5),a.removeValue(27);
a.outputValue();//1,3
}