时间:2021-06-15 09:15:50 | 栏目:C代码 | 点击:次
在动手之前我一直以为静态链表和动态链表没有什么差别,细细一想才发现,原来静态链表之中隐藏着一个非常值得讨论的话题――内存管理。
静态链表的“静态”二字是指内存的来源为静态内存(通常用全局数组)。与动态链表不同,在静态链表中节点内存的申请与释放都需要自行维护,由于这里是链表,也很容易想到将空余的节点链接起来形成一个free_list,每次需要时从free_list头部取出一个节点,释放时再将节点加到头部,这样就能够非常容易的实现链表的其他操作。
测试结果如下:
push_back test:
20
20 30
20 30 40
20 30 40 50
20 30 40 50 60
20 30 40 50 60 70
20 30 40 50 60 70 80
20 30 40 50 60 70 80 90
20 30 40 50 60 70 80 90 100
20 30 40 50 60 70 80 90 100 110
20 30 40 50 60 70 80 90 100 110 120
20 30 40 50 60 70 80 90 100 110 120 130
20 30 40 50 60 70 80 90 100 110 120 130 140
20 30 40 50 60 70 80 90 100 110 120 130 140 150
20 30 40 50 60 70 80 90 100 110 120 130 140 150 160
20 30 40 50 60 70 80 90 100 110 120 130 140 150 160
20 30 40 50 60 70 80 90 100 110 120 130 140 150 160
pop_back test:
20 30 40 50 60 70 80 90 100 110 120 130 140 150
20 30 40 50 60 70 80 90 100 110 120 130 140
20 30 40 50 60 70 80 90 100 110 120 130
20 30 40 50 60 70 80 90 100 110 120
20 30 40 50 60 70 80 90 100 110
20 30 40 50 60 70 80 90 100
20 30 40 50 60 70 80 90
20 30 40 50 60 70 80
20 30 40 50 60 70
20 30 40 50 60
20 30 40 50
20 30 40
20 30
20
insert test:
10
20 10
30 20 10
40 30 20 10
50 40 30 20 10
60 50 40 30 20 10
70 60 50 40 30 20 10
80 70 60 50 40 30 20 10
90 80 70 60 50 40 30 20 10
100 90 80 70 60 50 40 30 20 10
110 100 90 80 70 60 50 40 30 20 10
120 110 100 90 80 70 60 50 40 30 20 10
130 120 110 100 90 80 70 60 50 40 30 20 10
140 130 120 110 100 90 80 70 60 50 40 30 20 10
150 140 130 120 110 100 90 80 70 60 50 40 30 20 10
150 140 130 120 110 100 90 80 70 60 50 40 30 20 10
150 140 130 120 110 100 90 80 70 60 50 40 30 20 10
clear...
insert_after test:
-99 1
-99 2 1
-99 3 2 1
-99 4 3 2 1
-99 5 4 3 2 1
-99 6 5 4 3 2 1
-99 7 6 5 4 3 2 1
-99 8 7 6 5 4 3 2 1
-99 9 8 7 6 5 4 3 2 1
-99 10 9 8 7 6 5 4 3 2 1
-99 11 10 9 8 7 6 5 4 3 2 1
-99 12 11 10 9 8 7 6 5 4 3 2 1
-99 13 12 11 10 9 8 7 6 5 4 3 2 1
-99 14 13 12 11 10 9 8 7 6 5 4 3 2 1
-99 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
-99 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
-99 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
clear...
find test:
10 11 12 13 14 15 16 8 7 6 5 4 3 2 1
ifree: -1
idata: 14
====================
index data next
--------------------
16 1
8 3
15 0
7 5
14 2
6 7
13 4
5 9
12 6
4 11
11 8
3 13
10 10
2 15
9 12
1 -1
====================
9 found at 14
3 found at 11
not found
4 found at 9
1 found at 15
12 found at 8
not found
14 found at 4
not found
16 found at 0
9 found at 14
not found
not found
not found
9 found at 14
11 found at 10
find_prev test:
not found
6 found at 3's next.
not found
not found
7 found at 1's next.
12 found at 10's next.
not found
not found
4 found at 7's next.
not found
13 found at 8's next.
not found
6 found at 3's next.
not found
7 found at 1's next.
not found
find_prev and insert_after test:
2 3 4 5 6 7 8
insert -4 to front of 8: 1 2 3 4 5 6 7 -4 8
insert -5 to front of 3: 1 2 -5 3 4 5 6 7 -4 8
insert -8 to front of 6: 1 2 -5 3 4 5 -8 6 7 -4 8
find and insert test:
2 3 4 5 6 7 8
insert -2 to after of 3: 1 2 3 -2 4 5 6 7 8
insert -6 to after of 8: 1 2 3 -2 4 5 6 7 8 -6
insert -7 to after of 5: 1 2 3 -2 4 5 -7 6 7 8 -6
end of main().