新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:python創(chuàng)建鏈表的兩種形式
說明

1、頭插法將結(jié)點插入頭結(jié)點后面,新加入的結(jié)點next指向原來head指向的結(jié)點。
head改為新的結(jié)點。
2、尾插法將結(jié)點插入尾點前,新節(jié)點的next指向tail,tail更新為新節(jié)點。
實例
class Node: def __init__(self,item): self.item = item self.next = None class HandleNode: def create_linklist_head(self,li): head = Node(li[0]) for element in li[1:]: node = Node(element) node.next = head head = node return head def create_linklist_tail(self,li): head = Node(li[0]) tail = head for element in li[1:]: node = Node(element) tail.next = node tail = node return head def print_linklist(self,head): while head: print(head.item,end=',') head=head.next
以上就是python創(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創(chuàng)建鏈表的兩種形式
URL網(wǎng)址:http://www.5511xx.com/article/ccisgso.html


咨詢
建站咨詢
