blob: 873dfe90ca1119eeed2c51e57c31b42db4999d05 (
plain)
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
|
/* list.cpp
* Date: 2021-11-20
* Author: Aryadev Chavali
*/
#include <cstdio>
#include <iostream>
#include <cstdlib>
template <typename T>
struct List
{
T value;
struct List<T> *next;
~List()
{
if (next == NULL)
return;
delete next;
}
};
template <typename T>
List<T> *append(List<T> *lst, T value)
{
List<T> *node;
if (lst == NULL)
{
node = new List<T>;
node->value = value;
node->next = NULL;
return node;
}
for (node = lst; node->next != NULL; node = node->next)
continue;
node->next = new List<T>;
node->next->value = value;
node->next->next = NULL;
return lst;
}
template <typename T>
std::ostream& operator<<(std::ostream& ostream, const List<T> *lst)
{
if (lst == NULL)
return ostream;
ostream << "|" << lst->value << lst->next;
return ostream;
}
int main(void)
{
auto lst = append<int>(NULL, 1);
for (int i = 2; i < 10; ++i)
lst = append(lst, i);
std::cout << lst << std::endl;
return 0;
}
|