forked from balu/es242
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslist.cpp
More file actions
45 lines (40 loc) · 763 Bytes
/
slist.cpp
File metadata and controls
45 lines (40 loc) · 763 Bytes
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
#include <cstdio>
template <typename T>
struct SListNode
{
T data;
SListNode<T>* next;
};
template <typename T>
SListNode<T>* new_node(T val)
{
auto p = new SListNode<T>;
p->data = val;
p->next = 0;
return p;
}
template <typename T>
SListNode<T>* insert_after(SListNode<T>* pos, T val)
{
auto q = new_node(val);
q->next = pos->next;
pos->next = q;
return q;
}
void print_int_list(SListNode<int>* first)
{
if (first == 0) return;
auto current = first;
while (current) {
printf("%d, ", current->data);
current = current->next;
}
printf("\n");
}
int main()
{
auto xs = new_node(1);
insert_after(insert_after(insert_after(xs, 2), 4), 5);
print_int_list(xs);
return 0;
}