-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPW4.cpp
123 lines (103 loc) · 1.75 KB
/
PW4.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
#include <iostream>
using namespace std;
class Node {
private:
Node* next;
Node* prev;
int value;
public:
Node() {
next = nullptr;
prev = nullptr;
value = 0;
}
Node(int v) {
next = nullptr;
prev = nullptr;
value = v;
}
friend class Iterator;
friend class Sequence;
};
class Iterator {
private:
Node* node;
public:
Iterator() {
node = nullptr;
}
Iterator(Node* n) {
node = n;
}
Iterator* operator++() {
node = node->next;
return this;
}
friend class Sequence;
};
class Sequence {
private:
Node* head;
Node* tail;
int size;
public:
Sequence() {
head = new Node;
tail = new Node;
head->next = tail;
tail->prev = head;
size = 0;
}
void insert(Iterator& p, int e) {
Node* newNode = new Node(e);
p.node->prev->next = newNode;
newNode->prev = p.node->prev;
p.node->prev = newNode;
newNode->next = p.node;
size++;
}
Node* begin() {
return head->next;
}
void solution(Iterator& p) {
if (p.node->prev == head) {
int num1 = 0;
int num2 = p.node->value;
int num3 = p.node->next->value;
cout << 1*num1+3*num2+1*num3<< " ";
}
else if (p.node->next == tail) {
int num1 = p.node->prev->value;
int num2 = p.node->value;
int num3 = 0;
cout << 1 * num1 + 3 * num2 + 1 * num3 << " ";
}
else {
int num1 = p.node->next->value;
int num2 = p.node->value;
int num3 = p.node->prev->value;
cout << 1 * num1 + 3 * num2 + 1 * num3 << " ";
}
}
};
int main() {
int t, n;
cin >> t;
while (t--) {
Sequence seq = Sequence();
Iterator p = Iterator(seq.begin());
cin >> n;
for (int i = 0; i < n; i++) {
int num;
cin >> num;
seq.insert(p, num);
}
p = seq.begin();
for (int i = 0; i < n; i++) {
seq.solution(p);
++p;
}
cout << "\n";
}
return 0;
}