C语言中,为给定数据结构设计迭代器的思路。

迭代器

C语言中的迭代器中,迭代器的实现是这样

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
typedef struct listIter {
Node * next;
} listIter;

listIter * makeIterator(list * list) {
listIter * iter;
iter = malloc(sizeof(* iter));
iter->next = list->head;
return iter;
}

Node * next(listIter * iter) {
Node * current = iter->next;
if (current != NULL) {
iter->next = current->next;
}
return current;
}

以链表为例

假定我们设计如下链表数据结构,

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
typedef struct Node {
struct Node * prev;
struct Node * next;
void * value;
} Node;

typedef struct list {
Node * head;
Node * tail;
unsigned long len;
} list;

list * listCreate(void) {
struct list * list;
list = malloc(sizeof(* list));
list->head = list->tail = NULL;
list->len = 0;
return list;
}

void listDelete(list * list) {
unsigned long len;
Node * current, * next;
current = list->head;
len = list->len;
while(len--) {
next = current->next;
free(current);
current = next;
}
list->head = list->tail = NULL;
list->len = 0;
free(list);
}

list * addToHead(list * list, void * value) {
Node * node;
node = malloc(sizeof(* node));
node->value = value;
if (list->len == 0) {
list->head = list->tail = node;
node->prev = node->next = NULL;
} else {
node->prev = NULL;
node->next = list->head;
list->head->prev = node;
list->head = node;
}
list->len++;
return list;
}

list * addToTail(list * list, void * value) {
Node * node;
node = malloc(sizeof(* node));
node->value = value;
if (list->len == 0) {
list->head = list->tail = node;
node->prev = node->next = NULL;
} else {
node->prev = list->tail;
node->next = NULL;
list->tail->next = node;
list->tail = node;
}
list->len++;
return list;
}

list * insertIntoList(list * list, Node * old, void * value) {
Node * node;
node = malloc(sizeof(* node));
node->value = value;

node->prev = old;
node->next = old->next;
if (list->tail == old) {
list->tail = node;
}

if (node->prev != NULL) {
node->prev->next = node;
}
if (node->next != NULL) {
node->next->prev = node;
}
list->len++;
return list;
}
void deleteNode(list * list, Node * node) {
if (node->prev)
node->prev->next = node->next;
else
list->head = node->next;
if (node->next)
node->next->prev = node->prev;
else
list->tail = node->prev;
free(node);
list->len--;
}

容易理解,不同数据结构需要不同的迭代器,不存在通用的适合任何数据结构的迭代器。因为每个迭代器都需要知道给定数据结构的构造从而定制遍历方式。

使用迭代器

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
int main(int argc, char ** argv) {
list * list = listCreate();
int e1 = 1;
int e2 = 2;
int e3 = 3;
int e4 = 0;
int e5 = -1;
addToTail(list, &e1);
addToTail(list, &e2);
addToTail(list, &e3);
addToHead(list, &e4);
addToHead(list, &e5);

Node * node;

listIter * iter = makeIterator(list);
while ((node = next(iter)) != NULL) {
printf("%d\n", * (int* )(node->value));
}

listDelete(list);
free(iter);

return 0;
}

运行如下

1
2
3
4
5
6
$ gcc list.c ;./a.out
-1
0
1
2
3

转载请包括本文地址:https://allenwind.github.io/blog/4411
更多文章请参考:https://allenwind.github.io/blog/archives/