新聞中心
說明

為青云譜等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及青云譜網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都網(wǎng)站制作、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、青云譜網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!
1、當(dāng)實(shí)例化一個(gè)單向循環(huán)鏈表時(shí),該鏈表是一個(gè)空鏈表,在將節(jié)點(diǎn)依次鏈接之后,鏈表中才會(huì)出現(xiàn)節(jié)點(diǎn)和數(shù)據(jù)。
2、在鏈表中,為了找到鏈表的某個(gè)節(jié)點(diǎn),需要從鏈表的頭節(jié)點(diǎn)開始,依次搜索。
因此,在實(shí)例單向循環(huán)鏈表中,必須定義鏈表的頭。當(dāng)添加頭節(jié)點(diǎn)時(shí),鏈表的頭指向頭節(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 SingleCircularLinkList(object): """ 單向循環(huán)鏈表:單鏈表的一個(gè)變形是單向循環(huán)鏈表,鏈表中最后一個(gè)節(jié)點(diǎn)的next域不再為none,而是指向鏈表的頭節(jié)點(diǎn) """ def __init__(self, node=None): self.__head = node # node.elem node.next if node: node.next = node def is_empty(self): """鏈表是否為空 """ return self.__head is None def length(self): """鏈表長度""" if self.is_empty(): return 0 # cur游標(biāo),用來移動(dòng)遍歷節(jié)點(diǎn) cur = self.__head count = 1 while cur.next != self.__head: count += 1 cur = cur.next # count 記錄數(shù)量 return count def travel(self): """遍歷整個(gè)鏈表""" if self.is_empty(): return cur = self.__head while cur.next != self.__head: print(cur.elem, end=' ') cur = cur.next # 退出循環(huán),cur指向尾結(jié)點(diǎn),但尾節(jié)點(diǎn)的元素未打印 print(cur.elem) def add(self, item): """鏈表頭部添加元素:頭插法""" node = Node(item) if self.is_empty(): self.__head = node node.next = node else: cur = self.__head while cur.next != self.__head: cur = cur.next # 退出循環(huán),cur指向尾結(jié)點(diǎn) node.next = self.__head self.__head = node # 方式1:cur.next = node cur.next = self.__head # 方式2 def append(self, item): """鏈表尾部添加元素:尾插法""" node = Node(item) # 下一結(jié)點(diǎn)鏈接域不為空 if self.is_empty(): self.__head = node node.next = node else: cur = self.__head while cur.next != self.__head: cur = cur.next # 方式1: # node.next = cur.next # cur.next = node # 方式2: cur.next = node node.next = self.__head 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) if self.is_empty(): return cur = self.__head pre = None while cur.next != self.__head: if cur.elem == item: # 先判斷是否是頭節(jié)點(diǎn) if cur == self.__head: # 找到尾節(jié)點(diǎn) rear = self.__head while rear.next != self.__head: rear = rear.next self.__head = cur.next rear.next = self.__head else: # 中間節(jié)點(diǎn) pre.next = cur.next return else: pre = cur cur = cur.next # 退出循環(huán),cur指向尾結(jié)點(diǎn) if cur.elem == item: if cur == self.__head: # 鏈表只有一個(gè)節(jié)點(diǎn) self.__head = None else: pre.next = cur.next def search(self, item): """查找節(jié)點(diǎn)是否存在""" if self.is_empty(): return False # 1. 創(chuàng)建游標(biāo) cur = self.__head # 2. 遍歷游標(biāo) while cur.next != self.__head: # 3. cur.elem = item if cur.elem == item: return True else: cur = cur.next # 對于最后一個(gè)元素或只有一個(gè)元素 if cur.elem == item: return True return False if __name__ == '__main__': ll = SingleCircularLinkList() 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單向循環(huán)鏈表的創(chuàng)建,希望對大家有所幫助。更多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單向循環(huán)鏈表的創(chuàng)建
文章路徑:http://fisionsoft.com.cn/article/coijpds.html


咨詢
建站咨詢
