新聞中心
說明

堅(jiān)守“ 做人真誠 · 做事靠譜 · 口碑至上 · 高效敬業(yè) ”的價(jià)值觀,專業(yè)網(wǎng)站建設(shè)服務(wù)10余年為成都成都混凝土泵車小微創(chuàng)業(yè)公司專業(yè)提供成都企業(yè)網(wǎng)站定制營銷網(wǎng)站建設(shè)商城網(wǎng)站建設(shè)手機(jī)網(wǎng)站建設(shè)小程序網(wǎng)站建設(shè)網(wǎng)站改版,從內(nèi)容策劃、視覺設(shè)計(jì)、底層架構(gòu)、網(wǎng)頁布局、功能開發(fā)迭代于一體的高端網(wǎng)站建設(shè)服務(wù)。
1、每個(gè)節(jié)點(diǎn)包括兩個(gè)域、一個(gè)信息域(元素域)和一個(gè)連接域,該鏈接指向鏈接表的下一個(gè)節(jié)點(diǎn),最后一個(gè)節(jié)點(diǎn)的鏈接指向空值。
2、表要素elem用于存儲(chǔ)具體數(shù)據(jù)。鏈接域next用于存管下一個(gè)節(jié)點(diǎn)的位置
變量P指向鏈表頭節(jié)點(diǎn)(首節(jié)點(diǎn))的位置,可以從P出發(fā)找到表中的任意節(jié)點(diǎn)。
實(shí)例
class Node(object): def __init__(self, elem): """ :param elem: 表元素域 next:下一結(jié)點(diǎn)鏈接域 cursor(cur):游標(biāo) """ self.elem = elem # 定義next指向空 self.next = None class SingleLinkList(object): """ 單向鏈表也叫單鏈表,是鏈表中最簡單的一種形式,它的每個(gè)節(jié)點(diǎn)包含兩個(gè)域,一個(gè)信息域(元素域)和一個(gè)鏈接域。這個(gè)鏈接指向鏈表中的下一一個(gè)節(jié)點(diǎn),而最后-個(gè)節(jié)點(diǎn)的鏈接域則指向一個(gè)空值。 表元素域elem用來存放具體的數(shù)據(jù)。 鏈接域next用來存放下一個(gè)節(jié)點(diǎn)的位置(python中的標(biāo)識(shí)) 變量p指向鏈表的頭節(jié)點(diǎn)(首節(jié)點(diǎn))的位置,從p出發(fā)能找到表中的任意節(jié)點(diǎn)。 """ def __init__(self, node=None): self.__head = node # node.elem node.next def is_empty(self): """鏈表是否為空 """ return self.__head is None def length(self): """鏈表長度""" # cur游標(biāo),用來移動(dòng)遍歷節(jié)點(diǎn) cur = self.__head count = 0 while cur is not None: count += 1 cur = cur.next # count 記錄數(shù)量 return count def travel(self): """遍歷整個(gè)鏈表""" cur = self.__head while cur is not None: print(cur.elem, end=' ') cur = cur.next def add(self, item): """鏈表頭部添加元素:頭插法""" node = Node(item) node.next = self.__head self.__head = node def append(self, item): """鏈表尾部添加元素:尾插法""" node = Node(item) # 下一結(jié)點(diǎn)鏈接域不為空 if self.is_empty(): self.__head = node else: cur = self.__head while cur.next is not None: cur = cur.next cur.next = node def insert(self, pos, item): """ pos: pos從0開始 pre:指定節(jié)點(diǎn)前一節(jié)點(diǎn),相當(dāng)于游標(biāo) node:插入的指定節(jié)點(diǎn) 指定位置添加元素 """ # if pos<=0 頭插法 if pos <= 0: self.add(item) # elif pos>(self.length()-1) 尾插法 elif pos > (self.length() - 1): self.append(item) # else 插入法 else: pre = self.__head count = 0 # 當(dāng)循環(huán)退出后,pre指向pos-1 while count < (pos - 1): count += 1 pre = pre.next node = Node(item) node.next = pre.next pre.next = node def remove(self, item): """刪除元素""" # 考慮刪除頭部、尾部、中間節(jié)點(diǎn) cur = self.__head pre = None while cur is not None: if cur.elem == item: # 先判斷是否是頭節(jié)點(diǎn) if cur == self.__head: self.__head = cur.next else: pre.next = cur.next break else: pre = cur cur = cur.next def search(self, item): """查找節(jié)點(diǎn)是否存在""" # 1. 創(chuàng)建游標(biāo) cur = self.__head # 2. 遍歷游標(biāo) while cur is not None: # 3. cur.elem = item if cur.elem == item: return True else: cur = cur.next return False if __name__ == '__main__': ll = SingleLinkList() ll.is_empty() l1 = ll.length() print(l1) ll.append(55) ll.is_empty() l2 = ll.length() print(l2) ll.append(2) ll.add(8) ll.append(3) ll.append(4) ll.append(5) # 55 1 8 2 3 4 ll.insert(-1, 9) # 9 8 55 2 1 8 2345 ll.insert(2, 100) # 9 8 100 55 2 1 8 2345 ll.travel()
以上就是python單向鏈表的實(shí)現(xiàn),希望對大家有所幫助。更多Python學(xué)習(xí)指路:創(chuàng)新互聯(lián)Python教程
本文教程操作環(huán)境:windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。
標(biāo)題名稱:創(chuàng)新互聯(lián)Python教程:python單向鏈表如何實(shí)現(xiàn)
URL網(wǎng)址:http://www.dlmjj.cn/article/ccoeeop.html


咨詢
建站咨詢
