分享一段代碼,一個(gè)靜態(tài)鏈表的C語(yǔ)言實(shí)現(xiàn),其中包含著一種簡(jiǎn)單的內(nèi)存管理策略,固定大小的鏈?zhǔn)焦芾恚枰呐笥芽梢詤⒖冀梃b,下面來(lái)一起看看吧。
在動(dòng)手之前我一直以為靜態(tài)鏈表和動(dòng)態(tài)鏈表沒(méi)有什么差別,細(xì)細(xì)一想才發(fā)現(xiàn),原來(lái)靜態(tài)鏈表之中隱藏著一個(gè)非常值得討論的話題——內(nèi)存管理。
靜態(tài)鏈表的“靜態(tài)”二字是指內(nèi)存的來(lái)源為靜態(tài)內(nèi)存(通常用全局?jǐn)?shù)組)。與動(dòng)態(tài)鏈表不同,在靜態(tài)鏈表中節(jié)點(diǎn)內(nèi)存的申請(qǐng)與釋放都需要自行維護(hù),由于這里是鏈表,也很容易想到將空余的節(jié)點(diǎn)鏈接起來(lái)形成一個(gè)free_list,每次需要時(shí)從free_list頭部取出一個(gè)節(jié)點(diǎn),釋放時(shí)再將節(jié)點(diǎn)加到頭部,這樣就能夠非常容易的實(shí)現(xiàn)鏈表的其他操作。
?
測(cè)試結(jié)果如下:
?
?
?
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().
一個(gè)靜態(tài)鏈表的C語(yǔ)言實(shí)現(xiàn)就完成了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流。?
|
新聞熱點(diǎn)
疑難解答
圖片精選