class Node(object):def __init__(self,data):self.data=dataself.next=None
class LinkList(object):def __init__(self):self.head&#61;Node(None)def IsEmpty(self):p&#61;self.headif p.next&#61;&#61;None:print(&#39;List is Empty&#39;)return Truereturn Falsedef PrintList(self):if self.IsEmpty():return Falsep&#61;self.headwhile p:print(p.data,end&#61;&#39; &#39;)p&#61;p.nextdef InitList(self,data):self.head&#61;Node(data[0])p&#61;self.headfor i in data[1:]:node&#61;Node(i)p.next&#61;nodep&#61;p.nextdef LengthList(self):if self.IsEmpty():return 0p&#61;self.headcnt&#61;0while p:cnt&#43;&#61;1p&#61;p.nextreturn cntdef InsertList(self,s,data):if self.IsEmpty() or s<0 or s>self.LengthList():print(&#39;Insert failed&#39;)returnp&#61;self.headindex&#61;1while index<s:p&#61;p.nextindex&#43;&#61;1node&#61;Node(data)node.next&#61;p.nextp.next&#61;nodedef DeleteList(self,s):if self.IsEmpty() or s<0 or s>self.LengthList():print(&#39;Delete failed!&#39;)returnp&#61;self.headindex&#61;1while index<s:pre&#61;pindex&#43;&#61;1p&#61;p.nextpre.next&#61;p.nextp&#61;Nonedef GetList(self,s):if self.IsEmpty() or s<0 or s>self.LengthList():print(&#39;Read failed!&#39;)returnp&#61;self.headindex&#61;1while index<s:index&#43;&#61;1p&#61;p.nextprint(f"第 {s} 个值为 {p.data}")
lst&#61;LinkList()
data&#61;[1,4,5,8,2,3]
lst.InitList(data)
lst.PrintList()
print(lst.LengthList())