tinyOS  v1.00
tList.c
Go to the documentation of this file.
1 
13 #include "tLib.h"
14 
19 void tNodeInit (tNode *node) {
20  node->nextNode = node;
21  node->preNode = node;
22 }
23 
24 // 以下是简化代码编写添加的宏
25 #define firstNode headNode.nextNode
26 #define lastNode headNode.preNode
27 
32 void tListInit (tList *list) {
33  list->firstNode = &(list->headNode);
34  list->lastNode = &(list->headNode);
35  list->nodeCount = 0;
36 }
37 
43 uint32_t tListCount (tList *list) {
44  return list->nodeCount;
45 }
46 
53  tNode *node = (tNode *) 0;
54 
55  if (list->nodeCount != 0) {
56  node = list->firstNode;
57  }
58  return node;
59 }
60 
66 tNode *tListLast (tList *list) {
67  tNode *node = (tNode *) 0;
68 
69  if (list->nodeCount != 0) {
70  node = list->lastNode;
71  }
72  return node;
73 }
74 
81 tNode *tListPre (tList *list, tNode *node) {
82  if ((node->preNode == &list->headNode) || (node->preNode == node)) {
83  return (tNode *) 0;
84  } else {
85  return node->preNode;
86  }
87 }
88 
95 tNode *tListNext (tList *list, tNode *node) {
96  if ((node->nextNode == node) || (node->nextNode == &list->headNode)) {
97  return (tNode *) 0;
98  } else {
99  return node->nextNode;
100  }
101 }
102 
107 void tListRemoveAll (tList *list) {
108  uint32_t count;
109  tNode *nextNode;
110 
111  // 遍历所有的结点
112  nextNode = list->firstNode;
113  for (count = list->nodeCount; count != 0; count--) {
114  // 先纪录下当前结点,和下一个结点
115  // 必须纪录下一结点位置,因为在后面的代码中当前结点的next会被重置
116  tNode *currentNode = nextNode;
117  nextNode = nextNode->nextNode;
118 
119  // 重置结点自己的信息
120  currentNode->nextNode = currentNode;
121  currentNode->preNode = currentNode;
122  }
123 
124  list->firstNode = &(list->headNode);
125  list->lastNode = &(list->headNode);
126  list->nodeCount = 0;
127 }
128 
134 void tListAddFirst (tList *list, tNode *node) {
135  node->preNode = list->firstNode->preNode;
136  node->nextNode = list->firstNode;
137 
138  list->firstNode->preNode = node;
139  list->firstNode = node;
140  list->nodeCount++;
141 }
142 
148 void tListAddLast (tList *list, tNode *node) {
149  node->nextNode = &(list->headNode);
150  node->preNode = list->lastNode;
151 
152  list->lastNode->nextNode = node;
153  list->lastNode = node;
154  list->nodeCount++;
155 }
156 
163  tNode *node = (tNode *) 0;
164 
165  if (list->nodeCount != 0) {
166  node = list->firstNode;
167 
168  node->nextNode->preNode = &(list->headNode);
169  list->firstNode = node->nextNode;
170  list->nodeCount--;
171 
172  // 重置结点自己的信息
173  node->nextNode = node;
174  node->preNode = node;
175  }
176  return node;
177 }
178 
185 void tListInsertAfter (tList *list, tNode *nodeAfter, tNode *nodeToInsert) {
186  nodeToInsert->preNode = nodeAfter;
187  nodeToInsert->nextNode = nodeAfter->nextNode;
188 
189  nodeAfter->nextNode->preNode = nodeToInsert;
190  nodeAfter->nextNode = nodeToInsert;
191 
192  list->nodeCount++;
193 }
194 
200 void tListRemove (tList *list, tNode *node) {
201  node->preNode->nextNode = node->nextNode;
202  node->nextNode->preNode = node->preNode;
203  list->nodeCount--;
204 
205  // 重置结点自己的信息
206  node->nextNode = node;
207  node->preNode = node;
208 }
209 
tNode * tListLast(tList *list)
Definition: tList.c:66
struct _tNode * preNode
Definition: tLib.h:36
void tListRemoveAll(tList *list)
Definition: tList.c:107
void tListRemove(tList *list, tNode *node)
Definition: tList.c:200
tNode * tListFirst(tList *list)
Definition: tList.c:52
void tNodeInit(tNode *node)
Definition: tList.c:19
tNode * tListRemoveFirst(tList *list)
Definition: tList.c:162
Definition: tLib.h:35
void tListAddLast(tList *list, tNode *node)
Definition: tList.c:148
struct _tNode * nextNode
Definition: tLib.h:37
void tListInsertAfter(tList *list, tNode *nodeAfter, tNode *nodeToInsert)
Definition: tList.c:185
void tListAddFirst(tList *list, tNode *node)
Definition: tList.c:134
tNode * tListNext(tList *list, tNode *node)
Definition: tList.c:95
uint32_t nodeCount
Definition: tLib.h:45
tNode headNode
Definition: tLib.h:44
tNode * tListPre(tList *list, tNode *node)
Definition: tList.c:81
uint32_t tListCount(tList *list)
Definition: tList.c:43
Definition: tLib.h:43
void tListInit(tList *list)
Definition: tList.c:32