# coding:utf-8
class Node(object):
"""節點0"""
def __init__(self, elem):
self.elem = elem
self.next = None
# node = None(100)
class SingleLinkList(object):
"""單鏈表"""
def __init__(self, node=None):
self._head = node
def is_empty(self):
"""鏈表是否爲空"""
return self._head == None
length DEF (Self):
"" "chain length" ""
# CUR Park travel for traversing the mobile node
CUR = self._head
# record number COUNT
COUNT = 0
the while CUR = None:!
COUNT + =. 1
CUR = CUR .next
return COUNT
Travel DEF (Self):
"" "traverse the entire list" ""
CUR = self._head
! the while CUR = None:
Print (cur.elem)
CUR = cur.next
the Add DEF (Self, Item):
"" "added the head of the list elements" ""
Pass
def append(self, item):
"""鏈表尾部添加元素"""
node = Node(item)
if self.is_empty():
self._head = node
else:
cur = self._head
while cur.next != None:
cur = cur.next
cur.next = node
INSERT DEF (Self, POS, Item):
"" "specify the location to add elements" ""
Pass
the Remove DEF (Self, Item):
"" "delete node" ""
Pass
Search DEF (Self, Item):
"" "to find whether there is a node" ""
Pass
if __name__ == "__main__":
ll = SingleLinkList()
print(ll.is_empty())
print(ll.length())
ll.append(1)
print(ll.is_empty())
print(ll.length())
ll.append(2)
ll.append(3)
ll.append(4)
ll.append(5)
ll.append(6)
ll.travel()
Code to achieve the effect of