"""
单链表
1、节点:
data: 数据
next: 下一个节点链接
2、操作:
is_empty() 链表是否为空
length() 链表长度
travel() 遍历整个链表
add(item) 链表头部添加元素
append(item) 链表尾部添加元素
insert(pos, item) 指定位置添加元素
remove(item) 删除节点
search(item) 查找节点是否存在
index(pos) 获取指定位置上元素
"""
class SingleNode(object):
"""单链表节点"""
def __init__(self, data):
"""
节点具有两部分内容,因此具有两个属性
:param data: 接收创建节点时的数据内容
"""
self.data = data
self.next = None
class SingleLinkList(object):
"""单链表"""
def __init__(self, node=None):
"""
可以创建一个空链表,也可以创建带有一个节点的链表
:param node: 当创建带有节点的列表时,node用来接收节点数据,使用默认值用来创建空链表
"""
self.__head = node
def is_empty(self):
"""
判断链表是否为空
:return: False 不为空, True 空
"""
if self.__head is None:
return True
else:
return False
def length(self):
"""
链表长度:存在两种情况,空表和非空表
:return: 空表长度为0, 非空时,长度为节点个数(count)
"""
count = 0
cur = self.__head
while cur is not None:
count += 1
cur = cur.next
return count
def travel(self):
"""
遍历整个链表
:return:
"""
cur = self.__head
while cur is not None:
print(cur.data, end=" ")
cur = cur.next
print("")
def add(self, data):
"""
链表头部添加元素:存在空表和非空表两种情况
:param data: 接收创建节点的数据
:return:
"""
newNode = SingleNode(data)
newNode.next = self.__head
self.__head = newNode
def append(self, data):
"""
链表尾部添加元素:同样存在空表、单个节点链表和多个节点链表情况
:param data: 接收创建节点的数据
:return:
"""
newNode = SingleNode(data)
if self.is_empty():
self.__head = newNode
return
cur = self.__head
while cur.next is not None:
cur = cur.next
cur.next = newNode
def insert(self, pos, data):
"""
指定位置添加元素:
--插入的位置pos存在三种情况 1)链表头部位置pos=0 2)链表尾部位置pos=length() 3)链表中间位置pos=[1,length()-1]
--链表存在空表、非空表(就上面3中情况)
--用户输入pos也存在<0或>length()情况
:param pos: 新节点插入的位置,于列表一样,此处同样以0下标开始
:param data: 新节点数据
:return:
"""
if pos < 0 or pos > self.length():
print("索引位置越界!")
elif pos == 0:
self.add(data)
elif pos == self.length():
self.append(data)
else:
newNode = SingleNode(data)
cur = self.__head
index = 0
while index < pos-1:
index += 1
cur = cur.next
newNode.next = cur.next
cur.next = newNode
def remove(self, data):
"""
删除节点: 存在情况 1)空表 2)未找到指定节点 3)单节点链表 4)删除节点在链表头部 5)删除节点在链表中间 6)删除节点在链表尾部
:param data: 要删除的数据
:return: 删除成功True, 未找到数据False
"""
cur = self.__head
pre = None
while cur is not None:
if cur.data == data:
if cur == self.__head:
self.__head = cur.next
else:
pre.next = cur.next
break
else:
pre = cur
cur = cur.next
return False
def search(self, data):
"""
查找节点是否存在:存在空表的特殊情况
:param data: 要查找的数据
:return: 返回查找到的位置索引,未查找到则返回-1
"""
index = 0
cur = self.__head
while cur is not None:
if cur.data == data:
return index
index += 1
cur = cur.next
return -1
def index(self, pos):
"""
获取指定位置上的数据:存在空表的特殊情况
:param pos: 要查找的位置, 索引位置从0开始
:return: 返回查找到的数据
"""
index = 0
cur = self.__head
while cur is not None:
if index != pos:
cur = cur.next
index += 1
else:
return cur.data
return None
if __name__ == '__main__':
sll = SingleLinkList()
print("空否:", sll.is_empty())
print("长度:", sll.length())
sll.add(1)
sll.append(3)
sll.insert(1, 2)
print("空否:", sll.is_empty())
print("长度:", sll.length())
sll.travel()
sll.insert(0, 'ooo')
sll.insert(-1, 'aaa')
sll.insert(4, 'bbbb')
sll.insert(7, 'cccc')
print("空否:", sll.is_empty())
print("长度:", sll.length())
sll.travel()
print(sll.search('bbbb'))
print(sll.search('aaaaa'))
sll.remove('ooo')
sll.travel()
sll.remove('bbbb')
sll.travel()
sll.remove('1')
sll.travel()
print("长度:", sll.length())
print('0', sll.index(0))
print('3', sll.index(3))
print('-1', sll.index(-1))
print('10', sll.index(10))
newNode = SingleNode('hhhhhhh')
sll2 = SingleLinkList(newNode)
print("空否:", sll2.is_empty())
print("长度:", sll2.length())
sll2.travel()
执行结果:
空否: True
长度: 0
空否: False
长度: 3
1 2 3
索引位置越界!
索引位置越界!
空否: False
长度: 5
ooo 1 2 3 bbbb
4
-1
1 2 3 bbbb
1 2 3
1 2 3
长度: 3
0 1
3 None
-1 None
10 None
空否: False
长度: 1
hhhhhhh