分享一段代碼,一個靜態(tài)鏈表的C語言實現,其中包含著一種簡單的內存管理策略,固定大小的鏈式管理,需要的朋友可以參考借鑒,下面來一起看看吧。
在動手之前我一直以為靜態(tài)鏈表和動態(tài)鏈表沒有什么差別,細細一想才發(fā)現,原來靜態(tài)鏈表之中隱藏著一個非常值得討論的話題——內存管理。
靜態(tài)鏈表的“靜態(tài)”二字是指內存的來源為靜態(tài)內存(通常用全局數組)。與動態(tài)鏈表不同,在靜態(tài)鏈表中節(jié)點內存的申請與釋放都需要自行維護,由于這里是鏈表,也很容易想到將空余的節(jié)點鏈接起來形成一個free_list,每次需要時從free_list頭部取出一個節(jié)點,釋放時再將節(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().
一個靜態(tài)鏈表的C語言實現就完成了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。?
新聞熱點
疑難解答
圖片精選