質問編集履歴
1
学んだことのまとめ。
test
CHANGED
File without changes
|
test
CHANGED
@@ -100,4 +100,104 @@
|
|
100
100
|
|
101
101
|
|
102
102
|
|
103
|
+
----------------------------------
|
104
|
+
|
103
|
-
|
105
|
+
皆さん、ご回答ありがとうございました。色々、各解答、とても勉強になりました。一番勉強になったのは初期化の大切さです。以下、皆さんの、回答をもとに、試した、自分なりの答えです。
|
106
|
+
|
107
|
+
```ここに言語を入力
|
108
|
+
|
109
|
+
#include <iostream>
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
struct Node{
|
114
|
+
|
115
|
+
Node(int val, Node* n); //初期化強制。ディフォルト無し。
|
116
|
+
|
117
|
+
~Node();
|
118
|
+
|
119
|
+
int value;
|
120
|
+
|
121
|
+
Node* next_address;
|
122
|
+
|
123
|
+
};
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
Node::Node(int val, Node* next_addr){
|
128
|
+
|
129
|
+
this->value = val;
|
130
|
+
|
131
|
+
this->next_address = next_addr;
|
132
|
+
|
133
|
+
}
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
void showList(Node* node){
|
138
|
+
|
139
|
+
while(node != nullptr){
|
140
|
+
|
141
|
+
std::cout << node->value << "\n";
|
142
|
+
|
143
|
+
node = node->next_address;
|
144
|
+
|
145
|
+
}
|
146
|
+
|
147
|
+
}
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
int main(){
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
Node* one = new Node(1, nullptr);
|
158
|
+
|
159
|
+
Node* two = new Node(2, nullptr);
|
160
|
+
|
161
|
+
Node* three = new Node(3, nullptr);
|
162
|
+
|
163
|
+
Node* four = new Node(4, nullptr);
|
164
|
+
|
165
|
+
Node* five = new Node(5, nullptr);
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
one->next_address = two;
|
170
|
+
|
171
|
+
two->next_address = three;
|
172
|
+
|
173
|
+
three->next_address = four;
|
174
|
+
|
175
|
+
four->next_address = five;
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
showList(one);
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
// 借りたら、返す
|
186
|
+
|
187
|
+
delete one;
|
188
|
+
|
189
|
+
delete two;
|
190
|
+
|
191
|
+
delete three;
|
192
|
+
|
193
|
+
delete four;
|
194
|
+
|
195
|
+
delete five;
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
return 0;
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
```
|