-
Notifications
You must be signed in to change notification settings - Fork 2
/
list_node.h
111 lines (103 loc) · 3.16 KB
/
list_node.h
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
#ifndef _MY_LIST_NODE_H_
#define _MY_LIST_NODE_H_
namespace amo {
#define RESET "\033[0m"
#define BLACK "\033[30m" /* Black */
#define RED "\033[31m" /* Red */
#define GREEN "\033[32m" /* Green */
#define YELLOW "\033[33m" /* Yellow */
#define BLUE "\033[34m" /* Blue */
#define MAGENTA "\033[35m" /* Magenta */
#define CYAN "\033[36m" /* Cyan */
#define WHITE "\033[37m" /* White */
template<class T>
class Node{
private:
public:
T data;
Node<T>* pred;
Node<T>* succ;
Node() {
std::cout << MAGENTA << "[Node::Node()]: this:" << this << WHITE << std::endl;
}
Node(T const& t, Node<T>* p=NULL, Node<T>* s=NULL) : data(t), pred(p), succ(s) {
std::cout << MAGENTA << "[Node::Node(T&,Node*,Node*)]: this:" << this << ", data:" << data << ", pred:" << pred << ", succ:" << succ << WHITE << std::endl;
}
~Node() {
//std::cout << MAGENTA << "[Node::~Node()]: this:" << this << ", data:" << data << WHITE << std::endl;
}
Node<T>* insertPred(T const& t);
//Node<T> insertPred(T const& t);
Node<T>* insertSucc(T const& t);
//Node<T> insertSucc(T const& t);
Node<T>* next(int n);
};
#if 1
template<class T>
Node<T>* amo::Node<T>::insertPred(T const& t) {
Node<T>* node = new Node<T>(t, pred, this);
pred->succ = node;
pred = node;
return node;
}
/**
* local created node will be destroyed if there is not any variable outside referring to the local created node because RNO doesn't cover this case.
*/
#else
template<class T>
Node<T> amo::Node<T>::insertPred(T const& t) {
Node<T> node(t, pred, this);
pred->succ = &node;
pred = &node;
std::cout << MAGENTA << "[Node::insertPred(T&)]: returns new inserted node at " << &node << WHITE << std::endl;
return node;
}
#endif
#if 1
template<class T>
Node<T>* amo::Node<T>::insertSucc(T const& t) {
Node<T>* node = new Node(t, this, succ);
succ->pred = node;
succ = node;
return node;
}
/**
* local created node will be destroyed if there is not any variable outside referring to the local created node because RNO doesn't cover this case.
*/
#else
template<class T>
Node<T> amo::Node<T>::insertSucc(T const& t) {
Node<T> node(t, this, succ);
succ->pred = &node;
succ = &node;
std::cout << MAGENTA << "[Node::insertSucc(T&)]: returns new inserted node at " << &node << WHITE << std::endl;
return node;
}
#endif
template<class T>
Node<T>* amo::Node<T>::next(int n) {
Node<T>* node = this;
if (n >= 0) {
while (0<n--) {
node = node->succ;
if (node == NULL) {
std::cout << RED << "[Node::next(int)]: successor node is NULL and breaks from loop:" << n << WHITE << std::endl;
break;
}
}
} else {
int k = -n;
while(0<k--) {
node = node->pred;
if (node == NULL) {
std::cout << RED << "[Node::next(int)]: predecessor node is NULL and breaks from loop:" << n << WHITE << std::endl;
break;
}
}
}
if (node == NULL) std::cout << RED << "[Node::next(int)]: returns NULL" << WHITE << std::endl;
//else std::cout << MAGENTA << "[Node::next(int)]: returns:" << node->data << " at " << node << WHITE << std::endl;
return node;
}
}
#endif