class Node(object):
"""单链表节点的封装"""
def __init__(self, element):
self.element = element
self.next = None
class SingleLink(object):
"""单链表的封装"""
def __init__(self):
# 默认为空
self._head = None
def is_empty(self):
"""是否为空"""
return self._head == None
def __len__(self):
"""
求链表长度
1. 判断是否为空,为空直接返回0
2. 不为空时依次遍历,长度加1之后将下一个节点赋值给当前
"""
if self.is_empty():
return 0
else:
cur = self._head
length = 0
while cur != None:
length += 1
cur = cur.next
return length
def travel(self):
"""遍历链表"""
if self.is_empty():
print('空链表')
else:
cur = self._head
while cur.next != None:
print(cur.element, end=',')
cur = cur.next
print(cur.element)
def append(self, item):
"""
尾部添加元素
1. 先判断链表是否为空,若为空,将_head指向新节点
2. 若不为空,找到尾部,将尾节点next指向新节点
:param item:
:return:
"""
node = Node(item)
if self.is_empty():
self._head = node
else:
cur = self._head
while cur.next != None:
cur = cur.next
if cur.next == None:
cur.next = node
def add(self, item):
"""
头部添加元素:
1. 创建一个保存item值的节点
2. 将新节点的next指向头结点,即_head指向的位置
3. 将链表的头_head指向新节点
:param item:
:return:
"""
node = Node(item)
node.next = self._head
self._head = node
def insert(self, index, item):
"""
指定位置添加元素
1. 指定位置为头部之前,则头部添加元素
2. 指定位置为尾部之后,则尾部添加元素
3. 中间位置:需要找出指定位置的前一个元素,得到其尾部,插入的节点的尾部指向前面得到的尾部,
前一个元素新的尾部指向插入的头部
:param index:
:param item:
:return:
"""
if index <= 0:
self.add(item)
elif index >= len(self):
self.append(item)
else:
node = Node(item)
count = 0 # 当前节点的位置
cur = self._head
# 寻找插入节点的前一个节点
while count < index - 1:
count += 1
cur = cur.next
# 插入节点的前一个节点的尾部成为了插入节点的尾部指向
# 然后将插入节点的前一个节点的尾部指向更新为新插入节点的头部
node.next = cur.next
cur.next = node
def remove(self, item):
"""
删除指定元素的节点
1. 删除头部节点
2. 删除其他位置的节点
:param item:
:return:
"""
"""
既然要删除当前节点,首先想的就是遍历链表找到对应节点并且删除,
这里直接省去了遍历的操作,直接给出了对应的节点。
既然想删除当前节点那么将给定的节点的val和next全部转换成当前节点的下一个节点所对应的值,
那么当前节点在本链表中就相当于替换成了下一个节点。
"""
cur = self._head
pre = None
while cur != None:
if cur.element == item:
if not pre:
self._head = cur.next
else:
pre.next = cur.next
break
else:
pre = cur
cur = cur.next
def search(self, item):
"""
判断查找的元素在节点中是否存在,返回Bool类型
:param item:
:return:
"""
cur = self._head
while cur.next != None:
if cur.element == item:
return True
cur = cur.next
if cur.element == item:
return True
return False