-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArray-base-list.cpp
222 lines (192 loc) · 3.98 KB
/
Array-base-list.cpp
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include<iostream>
using namespace std;
class ArrayList {
private:
int *data;
int length;
int size;
int *current;
int *ncurrent;
int *ptr_back;
int *ptr_fwd;
int *temp;
int *temp_dup;
int *temp2;
int *ptr;
int *ptr2;
int *start;
int *end;
// put rest of the variable there
public:
ArrayList(int size) {
length = 0;
current = 0;
this->size = size;
data = new int[size];
current = data;
for (int i=0; i<size; i++){
*(data+i) = -1;
}
}
// Basic funtions. First implement these then add others funtion if you want
void SimpleInsertion(int data) {
if (length < size){
*current = data;
current++;
length++;
}
else{
cout<<"List is full"<<endl;
}
}
void InsertionAt(int value, int index) {
int val_shifted;
ncurrent = &(*(data + index));
if(*ncurrent < 0)
*ncurrent = value;
else{
if(length != size){
ptr_fwd = (current-1);
ptr_back = (current-1);
for(int i=length-1; i>=index; i--){
val_shifted = *ptr_fwd;
ptr_fwd++;
*ptr_fwd = val_shifted;
if(i!=index){
ptr_back--;
ptr_fwd = ptr_back;
}
else{
*ptr_back = value;
}
}
length++;
current++;
}
else{
cout<<"List is full already."<<endl;
}
}
}
void DeletionAt(int index) {
temp = data;
for(int i=0; i<length; i++){
if( (i == index) ){
if( (temp+1) != current){
for(int j=i; j<=length; j++){ //from val's index to last element
*temp = *(temp+1);
temp = temp + 1;
}
*temp = -1;
current = current - 1;
length = length - 1;
return;
}
else{
*temp = -1;
current = current - 1;
length = length - 1;
return;
}
}
else{
temp = temp + 1;
}
}
}
void DeleteData(int value) {
temp = data;
for(int i=0; i<length; i++){
if ( (*temp == value) && ( (temp+1) != current) ) {
for(int j=i; j<=length; j++){ //from val's index to last element
*temp = *(temp+1);
temp = temp + 1;
}
*temp = -1;
current = current - 1;
length = length - 1;
return;
}
if ( (*temp == value) && ( (temp+1) == current) ){
*temp = -1;
current = current - 1;
length = length - 1;
return;
}
else{
temp = temp + 1;
}
}
}
int find_index(int value) {
temp = data;
for(int i=0; i<length; i++){
if(*temp == value){
return i;
}
else
temp = temp + 1;
}
}
int find_value(int index){
temp = data;
for (int i=0; i<length; i++){
if(i == index)
return *temp;
else
temp = temp + 1;
}
}
void emptyList() {
temp = data;
for(int i=0; i<=length; i++){
*(temp+i) = -1;
}
cout<<"List is empty now"<<endl;
}
void copy(ArrayList obj) {
this->length = obj.length;
this->size = obj.size;
ptr = obj.data;
ptr2 = this->data; //obj2
for(int i=0; i<length; i++){
*ptr2 = *ptr;
ptr = ptr + 1;
ptr2 = ptr2 + 1;
}
}
void PrintList() {
int *temp = data;
for(int i=0; i<length; i++){
cout<<*temp++<<" ";
}
cout<<endl;
}
int main() {
ArrayList obj1(4); //size must be accurate
ArrayList obj2(5);
ArrayList obj3(10);
obj1.SimpleInsertion(1);
obj1.SimpleInsertion(2);
obj1.SimpleInsertion(3);
obj1.SimpleInsertion(4);
obj2.SimpleInsertion(8);
obj2.SimpleInsertion(7);
obj2.SimpleInsertion(6);
obj2.SimpleInsertion(2);
obj2.SimpleInsertion(1);
// obj2.copy(obj);
// obj2.PrintList();
// obj.InsertionAt(9, 3);
// obj.PrintList();
// obj.emptyList();
// cout<<"Found at index: "<<obj.find_index(3)<<endl;
// cout<<"Found value: "<<obj.find_value(2)<<endl;
// obj.DeleteData(1); //val
// obj.PrintList();
// obj.DeleteData(5); //val
// obj.PrintList();
// obj.DeletionAt(4); //index
// obj.PrintList();
// obj.DeletionAt(0); //index
// obj.PrintList();