python 实现链表,单链表、双链表
内容
"""单链表、双链表、单向循环列表"""
class Node(object):
"""节点"""
def __init__(self, item):
self.item = item
self.next = None
self.prev = None
class SingleLinkList(object):
"""单链表"""
def __init__(self, node=None):
self._head = node
@property
def is_empty(self) -> bool:
"""链表是否为空"""
return self._head is None
@property
def length(self) -> int:
"""链表长度"""
cur = self._head
count = 0
while cur is not None:
count += 1
cur = cur.next
return count
@property
def travel(self) -> list:
"""遍历整个链表"""
cur = self._head
_items = []
while cur is not None:
_items.append(cur.item)
cur = cur.next
return _items
def add(self, item) -> Node:
"""链表头部添加元素,头插法"""
node = Node(item)
node.next, self._head = self._head, node
return node
def append(self, item) -> Node:
"""链表尾部添加元素,尾插法"""
node = Node(item)
if self.is_empty:
self._head = node
else:
cur = self._head
while cur.next is not None:
cur = cur.next
cur.next = node
return node
def insert(self, pos, item) -> Node:
"""
指定位置添加元素
:param pos: 从 0 开始
:param item: 节点值
:return:
"""
if pos <= 0:
node = self.add(item)
elif pos > self.length - 1:
node = self.append(item)
else:
cur = self._head
count = 0
while count < pos - 1:
count += 1
cur = cur.next
node = Node(item)
node.next, cur.next = cur.next, node
return node
def remove(self, pos: int = None, item=None) -> Node:
"""
删除节点
:param pos: 需要删除节点的索引,从 0 开始
:param item: 根据节点元素删除
:return:
"""
node = cur = self._head
pre = None
if pos is not None:
count = 0
while cur is not None:
if count == pos:
node = cur
if cur == self._head:
self._head = cur.next
else:
pre.next = cur.next
break
else:
pre, cur = cur, cur.next
count += 1
else:
while cur is not None:
if cur.item == item:
node = cur
if cur == self._head:
self._head = cur.next
else:
pre.next = cur.next
break
else:
pre, cur = cur, cur.next
return node
def search(self, pos: int = None, item=None) -> Node:
"""
查找节点是否存在,存在则返回节点的索引列表,否则返回空列表
:param pos: 根据索引查找节点,返回节点对象
:param item: 根据元素查找节点,返回节点对象列表
:return:
"""
cur = self._head
if pos is not None:
count = 0
while cur is not None:
if pos == count:
return cur
cur = cur.next
count += 1
else:
while cur is not None:
if cur.item == item:
return cur
cur = cur.next
class DoubleLinkList(SingleLinkList):
"""双链表"""
def __init__(self, node=None):
super().__init__(node=node)
def add(self, item) -> Node:
"""链表头部添加元素,头插法"""
node = Node(item)
node.next, self._head, node.next.prev = self._head, node, node
return node
def append(self, item) -> Node:
"""链表尾部添加元素,尾插法"""
node = Node(item)
if self.is_empty:
self._head = node
else:
cur = self._head
while cur.next is not None:
cur = cur.next
cur.next, node.prev = node, cur
return node
def insert(self, pos, item) -> Node:
"""
指定位置添加元素
:param pos: 从 0 开始
:param item: 节点值
:return:
"""
if pos <= 0:
node = self.add(item)
elif pos > self.length - 1:
node = self.append(item)
else:
cur = self._head
count = 0
while count < pos:
count += 1
cur = cur.next
node = Node(item)
node.next, node.prev, cur.prev.next, cur.prev = cur, cur.prev, node, node
return node
def remove(self, pos: int = None, item=None) -> Node:
"""
删除节点
:param pos: 需要删除节点的索引,从 0 开始
:param item: 根据节点元素删除
:return:
"""
node = cur = self._head
if pos is not None:
count = 0
while cur is not None:
if count == pos:
node = cur
if cur == self._head:
self._head = cur.next
if cur.next:
cur.next.prev = None
else:
cur.prev.next = cur.next
if cur.next:
cur.next.prev = cur.prev
break
else:
cur = cur.next
count += 1
else:
while cur is not None:
if cur.item == item:
node = cur
if cur == self._head:
self._head = cur.next
if cur.next:
cur.next.prev = None
else:
cur.prev.next = cur.next
if cur.next:
cur.next.prev = cur.prev
break
else:
cur = cur.next
return node
if __name__ == '__main__':
ll = DoubleLinkList()
print(ll.is_empty)
print(ll.travel)
ll.append(1)
print(ll.is_empty)
print(ll.travel)
ll.append(2)
print(ll.travel)
ll.add(8)
print(ll.travel)
ll.append(3)
print(ll.travel)
ll.append(4)
print(ll.travel)
ll.append(5)
print(ll.travel)
ll.append(6)
print(ll.travel)
ll.insert(-1, 9)
print(ll.travel)
ll.insert(2, 100)
print(ll.travel)
ll.insert(10, 200)
print(ll.travel)
print(ll.length)
ll.remove(0)
print(ll.travel)
ll.remove(2)
print(ll.travel)
ll.remove(ll.length-1)
print(ll.travel)
ll.remove(item=8)
print(ll.travel)
ll.remove(item=3)
print(ll.travel)
ll.remove(item=6)
print(ll.travel)
print(ll.length)
单链表测试
双链表测试运行截图