teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

追記

2020/03/28 23:46

投稿

episteme
episteme

スコア16612

answer CHANGED
@@ -9,4 +9,50 @@
9
9
 
10
10
  がそのメンバとして用意されています。
11
11
  data_t getdata() で要素(の値)を返し、
12
- list *getnext() が次の数珠玉(へのポインタ)を返します。
12
+ list *getnext() が次の数珠玉(へのポインタ)を返します。
13
+
14
+ [追記] list<char> をそれと等価な list_of_char に置き換えました。
15
+ これでわからんとしたら、templateがわからんわけじゃない。
16
+ ```C++
17
+ typedef char data_t;
18
+
19
+ class list_of_char {
20
+ data_t data;
21
+ list_of_char *next; // ・・・・・・(あ)
22
+ public:
23
+ list_of_char(data_t d);
24
+ void add(list_of_char *node) {node->next = this; next = 0; } // nodeの次にthisをつなぐ
25
+ list_of_char *getnext() { return next; } // ・・・・・(い)
26
+ data_t getdata() { return data; }
27
+ };
28
+
29
+ list_of_char::list_of_char(data_t d) {
30
+ data = d;
31
+ next = 0;
32
+ }
33
+
34
+ #include <iostream>
35
+ using namespace std;
36
+
37
+ int main() {
38
+ list_of_char start('a'); // ・・・・・・(う)
39
+ list_of_char *p, *last; // ・・・・・・(え)
40
+ int i;
41
+ // リストを作成する
42
+ last = &start;
43
+ for(i=1; i<26; i++) {
44
+ p = new list_of_char('a' + i);
45
+ p->add(last);
46
+ last = p;
47
+ }
48
+ // リストを追跡する
49
+ p = &start;
50
+
51
+ while(p) {
52
+ cout << p->getdata();
53
+ p = p->getnext(); // ・・・・・・(お)
54
+ }
55
+ cout << "\n";
56
+ return 0;
57
+ }
58
+ ```