新聞中心
說明

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


咨詢
建站咨詢
