-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain2.cpp
54 lines (48 loc) · 1018 Bytes
/
main2.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
#ifdef MAIN2
#include <iostream>
using namespace std;
template <class T>
class ShiftedList{
T *array;
int offset, size;
public:
ShiftedList(int sz):offset(0), size(sz){
array = new T[size];
}
~ShiftedList(){
delete [] array;
}
void shiftBy(int n){
offset = (offset + n) % size;
}
T getAt(int i){
return array[convertIndex(i)];
}
void setAt(T item, int i){
array[convertIndex(i)] = item;
}
private:
int convertIndex(int i){
int index = (i - offset) % size;
while(index < 0){
index += size;
}
return index;
}
};
int main()
{
int size = 4;
ShiftedList<int> *list = new ShiftedList<int>(size);
for(int i = 0; i < size; i++){
list->setAt(i, i);
}
cout << list->getAt(0) << endl;
cout << list->getAt(1) << endl;
list->shiftBy(1);
cout << list->getAt(0) << endl;
cout << list->getAt(1) << endl;
delete list;
return 0;
}
#endif