
Python實(shí)現(xiàn)基本線性數(shù)據(jù)結(jié)構(gòu)
數(shù)組的設(shè)計(jì)
數(shù)組設(shè)計(jì)之初是在形式上依賴內(nèi)存分配而成的,所以必須在使用前預(yù)先請求空間。這使得數(shù)組有以下特性:
1、請求空間以后大小固定,不能再改變(數(shù)據(jù)溢出問題);
2、在內(nèi)存中有空間連續(xù)性的表現(xiàn),中間不會存在其他程序需要調(diào)用的數(shù)據(jù),為此數(shù)組的專用內(nèi)存空間;
3、在舊式編程語言中(如有中階語言之稱的C),程序不會對數(shù)組的操作做下界判斷,也就有潛在的越界操作的風(fēng)險(xiǎn)(比如會把數(shù)據(jù)寫在運(yùn)行中程序需要調(diào)用的核心部分的內(nèi)存上)。
因?yàn)楹唵螖?shù)組強(qiáng)烈倚賴電腦硬件之內(nèi)存,所以不適用于現(xiàn)代的程序設(shè)計(jì)。欲使用可變大小、硬件無關(guān)性的數(shù)據(jù)類型,Java等程序設(shè)計(jì)語言均提供了更高級的數(shù)據(jù)結(jié)構(gòu):ArrayList、Vector等動態(tài)數(shù)組。
Python的數(shù)組
從嚴(yán)格意義上來說:Python里沒有嚴(yán)格意義上的數(shù)組。
List可以說是Python里的數(shù)組,下面這段代碼是CPython的實(shí)現(xiàn)List的結(jié)構(gòu)體:
typedef struct {
PyObject_VAR_HEAD
/* Vector of pointers to list elements. list[0] is ob_item[0], etc. */
PyObject **ob_item;
/* ob_item contains space for 'allocated' elements. The number
* currently in use is ob_size.
* Invariants:
* 0 <= ob_size <= allocated
* len(list) == ob_size
* ob_item == NULL implies ob_size == allocated == 0
* list.sort() temporarily sets allocated to -1 to detect mutations.
*
* Items must normally not be NULL, except during construction when
* the list is not yet visible outside the function that builds it.
*/
Py_ssize_t allocated;
} PyListObject;
當(dāng)然,在Python里它就是數(shù)組。
后面的一些結(jié)構(gòu)也將用List來實(shí)現(xiàn)。
堆棧
什么是堆棧
堆棧(英語:stack),也可直接稱棧,在計(jì)算機(jī)科學(xué)中,是一種特殊的串列形式的數(shù)據(jù)結(jié)構(gòu),它的特殊之處在于只能允許在鏈接串列或陣列的一端(稱為堆疊頂端指標(biāo),英語:top)進(jìn)行加入資料(英語:push)和輸出資料(英語:pop)的運(yùn)算。另外堆疊也可以用一維陣列或連結(jié)串列的形式來完成。堆疊的另外一個相對的操作方式稱為佇列。
由于堆疊數(shù)據(jù)結(jié)構(gòu)只允許在一端進(jìn)行操作,因而按照后進(jìn)先出(LIFO, Last In First Out)的原理運(yùn)作。
特點(diǎn)
1、先入后出,后入先出。
2、除頭尾節(jié)點(diǎn)之外,每個元素有一個前驅(qū),一個后繼。
操作
從原理可知,對堆棧(棧)可以進(jìn)行的操作有:
1、top() :獲取堆棧頂端對象
2、push() :向棧里添加一個對象
3、pop() :從棧里推出一個對象
實(shí)現(xiàn)
class my_stack(object):
def __init__(self, value):
self.value = value
# 前驅(qū)
self.before = None
# 后繼
self.behind = None
def __str__(self):
return str(self.value)
def top(stack):
if isinstance(stack, my_stack):
if stack.behind is not None:
return top(stack.behind)
else:
return stack
def push(stack, ele):
push_ele = my_stack(ele)
if isinstance(stack, my_stack):
stack_top = top(stack)
push_ele.before = stack_top
push_ele.before.behind = push_ele
else:
raise Exception('不要亂扔?xùn)|西進(jìn)來好么')
def pop(stack):
if isinstance(stack, my_stack):
stack_top = top(stack)
if stack_top.before is not None:
stack_top.before.behind = None
stack_top.behind = None
return stack_top
else:
print('已經(jīng)是棧頂了')
隊(duì)列
什么是隊(duì)列
和堆棧類似,唯一的區(qū)別是隊(duì)列只能在隊(duì)頭進(jìn)行出隊(duì)操作,所以隊(duì)列是是先進(jìn)先出(FIFO, First-In-First-Out)的線性表
特點(diǎn)
1、先入先出,后入后出
2、除尾節(jié)點(diǎn)外,每個節(jié)點(diǎn)有一個后繼
3、(可選)除頭節(jié)點(diǎn)外,每個節(jié)點(diǎn)有一個前驅(qū)
操作
1、push() :入隊(duì)
2、pop() :出隊(duì)
實(shí)現(xiàn)
普通隊(duì)列
class MyQueue():
def __init__(self, value=None):
self.value = value
# 前驅(qū)
# self.before = None
# 后繼
self.behind = None
def __str__(self):
if self.value is not None:
return str(self.value)
else:
return 'None'
def create_queue():
"""僅有隊(duì)頭"""
return MyQueue()
def last(queue):
if isinstance(queue, MyQueue):
if queue.behind is not None:
return last(queue.behind)
else:
return queue
def push(queue, ele):
if isinstance(queue, MyQueue):
last_queue = last(queue)
new_queue = MyQueue(ele)
last_queue.behind = new_queue
def pop(queue):
if queue.behind is not None:
get_queue = queue.behind
queue.behind = queue.behind.behind
return get_queue
else:
print('隊(duì)列里已經(jīng)沒有元素了')
def print_queue(queue):
print(queue)
if queue.behind is not None:
print_queue(queue.behind)
鏈表
什么是鏈表
鏈表(Linked list)是一種常見的基礎(chǔ)數(shù)據(jù)結(jié)構(gòu),是一種線性表,但是并不會按線性的順序存儲數(shù)據(jù),而是在每一個節(jié)點(diǎn)里存到下一個節(jié)點(diǎn)的指針(Pointer)。由于不必須按順序存儲,鏈表在插入的時(shí)候可以達(dá)到O(1)的復(fù)雜度,比另一種線性表順序表快得多,但是查找一個節(jié)點(diǎn)或者訪問特定編號的節(jié)點(diǎn)則需要O(n)的時(shí)間,而順序表相應(yīng)的時(shí)間復(fù)雜度分別是O(logn)和O(1)。
特點(diǎn)
使用鏈表結(jié)構(gòu)可以克服數(shù)組鏈表需要預(yù)先知道數(shù)據(jù)大小的缺點(diǎn),鏈表結(jié)構(gòu)可以充分利用計(jì)算機(jī)內(nèi)存空間,實(shí)現(xiàn)靈活的內(nèi)存動態(tài)管理。但是鏈表失去了數(shù)組隨機(jī)讀取的優(yōu)點(diǎn),同時(shí)鏈表由于增加了結(jié)點(diǎn)的指針域,空間開銷比較大。
操作
1、init() :初始化
2、insert() : 插入
3、trave() : 遍歷
4、delete() : 刪除
5、find() : 查找
實(shí)現(xiàn)
此處僅實(shí)現(xiàn)雙向列表
class LinkedList():
def __init__(self, value=None):
self.value = value
# 前驅(qū)
self.before = None
# 后繼
self.behind = None
def __str__(self):
if self.value is not None:
return str(self.value)
else:
return 'None'
def init():
return LinkedList('HEAD')
def delete(linked_list):
if isinstance(linked_list, LinkedList):
if linked_list.behind is not None:
delete(linked_list.behind)
linked_list.behind = None
linked_list.before = None
linked_list.value = None
總結(jié)
以上就是利用Python實(shí)現(xiàn)基本線性數(shù)據(jù)結(jié)構(gòu)的全部內(nèi)容
數(shù)據(jù)分析咨詢請掃描二維碼
若不方便掃碼,搜微信號:CDAshujufenxi
訓(xùn)練與驗(yàn)證損失驟升:機(jī)器學(xué)習(xí)訓(xùn)練中的異常診斷與解決方案 在機(jī)器學(xué)習(xí)模型訓(xùn)練過程中,“損失曲線” 是反映模型學(xué)習(xí)狀態(tài)的核心指 ...
2025-09-19解析 DataHub 與 Kafka:數(shù)據(jù)生態(tài)中兩類核心工具的差異與協(xié)同 在數(shù)字化轉(zhuǎn)型加速的今天,企業(yè)對數(shù)據(jù)的需求已從 “存儲” 轉(zhuǎn)向 “ ...
2025-09-19CDA 數(shù)據(jù)分析師:讓統(tǒng)計(jì)基本概念成為業(yè)務(wù)決策的底層邏輯 統(tǒng)計(jì)基本概念是商業(yè)數(shù)據(jù)分析的 “基礎(chǔ)語言”—— 從描述數(shù)據(jù)分布的 “均 ...
2025-09-19CDA 數(shù)據(jù)分析師:表結(jié)構(gòu)數(shù)據(jù) “獲取 - 加工 - 使用” 全流程的賦能者 表結(jié)構(gòu)數(shù)據(jù)(如數(shù)據(jù)庫表、Excel 表、CSV 文件)是企業(yè)數(shù)字 ...
2025-09-19SQL Server 中 CONVERT 函數(shù)的日期轉(zhuǎn)換:從基礎(chǔ)用法到實(shí)戰(zhàn)優(yōu)化 在 SQL Server 的數(shù)據(jù)處理中,日期格式轉(zhuǎn)換是高頻需求 —— 無論 ...
2025-09-18MySQL 大表拆分與關(guān)聯(lián)查詢效率:打破 “拆分必慢” 的認(rèn)知誤區(qū) 在 MySQL 數(shù)據(jù)庫管理中,“大表” 始終是性能優(yōu)化繞不開的話題。 ...
2025-09-18DSGE 模型中的 Et:理性預(yù)期算子的內(nèi)涵、作用與應(yīng)用解析 動態(tài)隨機(jī)一般均衡(Dynamic Stochastic General Equilibrium, DSGE)模 ...
2025-09-17Python 提取 TIF 中地名的完整指南 一、先明確:TIF 中的地名有哪兩種存在形式? 在開始提取前,需先判斷 TIF 文件的類型 —— ...
2025-09-17CDA 數(shù)據(jù)分析師:解鎖表結(jié)構(gòu)數(shù)據(jù)特征價(jià)值的專業(yè)核心 表結(jié)構(gòu)數(shù)據(jù)(以 “行 - 列” 規(guī)范存儲的結(jié)構(gòu)化數(shù)據(jù),如數(shù)據(jù)庫表、Excel 表、 ...
2025-09-17Excel 導(dǎo)入數(shù)據(jù)含缺失值?詳解 dropna 函數(shù)的功能與實(shí)戰(zhàn)應(yīng)用 在用 Python(如 pandas 庫)處理 Excel 數(shù)據(jù)時(shí),“缺失值” 是高頻 ...
2025-09-16深入解析卡方檢驗(yàn)與 t 檢驗(yàn):差異、適用場景與實(shí)踐應(yīng)用 在數(shù)據(jù)分析與統(tǒng)計(jì)學(xué)領(lǐng)域,假設(shè)檢驗(yàn)是驗(yàn)證研究假設(shè)、判斷數(shù)據(jù)差異是否 “ ...
2025-09-16CDA 數(shù)據(jù)分析師:掌控表格結(jié)構(gòu)數(shù)據(jù)全功能周期的專業(yè)操盤手 表格結(jié)構(gòu)數(shù)據(jù)(以 “行 - 列” 存儲的結(jié)構(gòu)化數(shù)據(jù),如 Excel 表、數(shù)據(jù) ...
2025-09-16MySQL 執(zhí)行計(jì)劃中 rows 數(shù)量的準(zhǔn)確性解析:原理、影響因素與優(yōu)化 在 MySQL SQL 調(diào)優(yōu)中,EXPLAIN執(zhí)行計(jì)劃是核心工具,而其中的row ...
2025-09-15解析 Python 中 Response 對象的 text 與 content:區(qū)別、場景與實(shí)踐指南 在 Python 進(jìn)行 HTTP 網(wǎng)絡(luò)請求開發(fā)時(shí)(如使用requests ...
2025-09-15CDA 數(shù)據(jù)分析師:激活表格結(jié)構(gòu)數(shù)據(jù)價(jià)值的核心操盤手 表格結(jié)構(gòu)數(shù)據(jù)(如 Excel 表格、數(shù)據(jù)庫表)是企業(yè)最基礎(chǔ)、最核心的數(shù)據(jù)形態(tài) ...
2025-09-15Python HTTP 請求工具對比:urllib.request 與 requests 的核心差異與選擇指南 在 Python 處理 HTTP 請求(如接口調(diào)用、數(shù)據(jù)爬取 ...
2025-09-12解決 pd.read_csv 讀取長浮點(diǎn)數(shù)據(jù)的科學(xué)計(jì)數(shù)法問題 為幫助 Python 數(shù)據(jù)從業(yè)者解決pd.read_csv讀取長浮點(diǎn)數(shù)據(jù)時(shí)的科學(xué)計(jì)數(shù)法問題 ...
2025-09-12CDA 數(shù)據(jù)分析師:業(yè)務(wù)數(shù)據(jù)分析步驟的落地者與價(jià)值優(yōu)化者 業(yè)務(wù)數(shù)據(jù)分析是企業(yè)解決日常運(yùn)營問題、提升執(zhí)行效率的核心手段,其價(jià)值 ...
2025-09-12用 SQL 驗(yàn)證業(yè)務(wù)邏輯:從規(guī)則拆解到數(shù)據(jù)把關(guān)的實(shí)戰(zhàn)指南 在業(yè)務(wù)系統(tǒng)落地過程中,“業(yè)務(wù)邏輯” 是連接 “需求設(shè)計(jì)” 與 “用戶體驗(yàn) ...
2025-09-11塔吉特百貨孕婦營銷案例:數(shù)據(jù)驅(qū)動下的精準(zhǔn)零售革命與啟示 在零售行業(yè) “流量紅利見頂” 的當(dāng)下,精準(zhǔn)營銷成為企業(yè)突圍的核心方 ...
2025-09-11