代码:
"""
实现单链表
"""
class Node:
"""
节点类:一个节点对象存储一个值和一个属性,其用于一个指向另一个节点
"""
def __init__(self, value, next=None):
self.next = next
self.value = value
@property
def next(self):
return self.__next
@next.setter
def next(self, next):
if next is None or isinstance(next, Node):
self.__next = next
else:
raise TypeError("next should be None or an instance of class Node")
class LinkList:
"""
单链表:生成对象可以进行增删改查操作,棘突操作通告调用具体方法完成
"""
def __init__(self):
"""初始化链表,产生一个链表开端, 以便获取后面的节点 """
self.__head = Node(-1)
@property
def head(self):
"""
获取链表对象中第一个数据节点
:return: 返回链表中第一个数据节点
"""
return self.__head.next
def init_list(self, lst):
"""
传入一个列表,将列表中每个元素生成一个节点,插入到链表对象中
:param lst:传入的链表
:return: 无
"""
if not isinstance(lst, list):
raise ValueError("lst should be an instance of list")
for elem in lst:
self.append(Node(elem))
def append(self, node):
"""
将传入节点,插入到链表对象的末尾
:param node: 掺入节点
:return: 无返回值
"""
if isinstance(node, Node):
temp = self.__head
while temp.next is not None:
temp = temp.next
temp.next = node
else:
raise ValueError("node sholud be an instance of class Node")
def remove(self, value):
"""
移除值为传入value的节点
:param value:
:return:True表示删除成功,False表示删除失败
"""
head = self.__head
node = head.next
while node is not None:
if node.value == value:
head.next = node.next
return True
else:
head = node
node = head.next
return False
def count(self):
"""
计算链表对象中数据节点数目
:return: 数据节点数目
"""
node = self.__head.next
count = 0
while node is not None:
count += 1
node = node.next
return count
def empty(self):
"""
判断链表是否为空
:return: True/False
"""
if self.__head.next is None:
return True
else:
return False
def get(self, seq):
"""
获取第seq个节点的值
:param seq: 序号
:return: (bool, 值)
"""
i = 1
node = self.__head.next
if seq > self.count():
return False, -1
else:
while i < seq:
node = node.next
i += 1
return (True, node.value)
def modify(self, seq, value):
"""
修改第seq个节点的值为传入的值
:param seq: 序号
:param value: 值
:return: (bool, 原来的值)
"""
if seq > self.count() or seq <= 0:
return False, -1
node = self.__head.next
i = 1
while i < seq:
node = node.next
i += 1
old = node.value
node.value = value
return True, old
def insert(self, seq, value):
"""
将传入value生成一个节点,插入链表对象的第seq节点,如果seq节点大于链表长度,则在插入到末尾
:param seq: 插入序号
:param value: 传入值
:return: 无返回
"""
if seq <= 0:
return
head = self.__head
node = head.next
newnode = Node(value)
i = 1
while i < seq and node is not None:
head = node
node = head.next
i += 1
newnode.next = head.next
head.next = newnode
def print_values(self):
"""
输出链表对象中所有节点的值
:return:无
"""
node = self.__head.next
while node is not None:
print(node.value, end=" ")
node = node.next
print()
# 测试代码
if __name__ == "__main__":
linklist = LinkList()
linklist.init_list([2, 5, 3, 8, 6])
print("节点数目:%d" % linklist.count())
linklist.print_values()
if linklist.remove(3):
print("节点数目:%d" % linklist.count())
linklist.print_values()
seq = 2
print("%d ---> %d" % (seq, linklist.get(seq)[1]))
ret, value = linklist.modify(2, 9)
if ret:
linklist.print_values()
linklist.insert(2, 7)
linklist.insert(1, 4)
linklist.insert(20, 5)
linklist.print_values()
测试结果: