文章目录
- 前言
- 一、利用python实现链表的基本操作
- 1.节点的定义使用类实现:
- 1.链表的定义使用类实现:
- 3.判断是否为空函数实现:
- 4.链表长度函数实现:
- 5.遍历链表函数实现:
- 6.头插法函数实现:
- 7.尾插法函数实现:
- 8.在链表指定位置插入值函数实现:
- 9.删除某一个值函数实现:
- 10查看某个节点是否存在函数实现:
前言
如何使用python实现链表的基本操作,包括结点的定义,链表的定义,求链表长度,链表是否为空等等一系列函数
提示:以下是本篇文章正文内容,下面案例可供参考
一、利用python实现链表的基本操作
链表中的节点应该包含数值和指向下一个元素的地址。
1.节点的定义使用类实现:
class Node():
def __init__(self,elem):
self.elem = elem
self.next = None
由于python万物皆对象,变量实际上是对象的引用,相当于就保存了对象的地址
1.链表的定义使用类实现:
链表的定义如下,也是使用类实现:
class Single_LinkList():
def __init__(self,node = None):
self.head = node
初始化时会自动生成一个空链表,其中head指向None
3.判断是否为空函数实现:
def is_empty(self):
"""链表是否为空"""
if self.head == None:
return True
else:
return False
4.链表长度函数实现:
def length(self):
"""链表长度"""
len = 0
cur = self.head
while cur != None:
len = len + 1
cur = cur.next
return len
5.遍历链表函数实现:
def travel(self):
"""遍历链表"""
cur = self.head
while cur != None:
print(cur.elem)
cur = cur.next
6.头插法函数实现:
def add(self,item):
"""链表头部进行添加元素"""
node = Node(item)
node.next = self.head
self.head = node
7.尾插法函数实现:
def append(self,item):
"""链表尾部进行添加元素"""
node = Node(item)
cur = self.head
if self.is_empty():
self.head = node
else:
while cur.next != None:
cur = cur.next
cur.next = node
node.next = None
8.在链表指定位置插入值函数实现:
def insert(self,pos,item):
"""
在指定位置上添加元素
:param pos: 从0开始
:param item:
:return:
"""
node = Node(item)
count = 0
cur = self.head
while cur != None:
if count == pos - 1:
node.next = cur.next
cur.next = node
break
cur = cur.next
count = count + 1
9.删除某一个值函数实现:
def remove(self,item):
"""删除某一个节点"""
cur = self.head
count = 0
if self.is_empty():
print("删除失败")
elif self.head.elem == item:
self.head = self.head.next
else:
while cur.next != None:
if cur.next.elem == item:
cur.next = cur.next.next
break
cur = cur.next
10查看某个节点是否存在函数实现:
def search(self,item):
"""查看某个节点是否存在"""
cur = self.head
while cur != None:
if cur.elem == item:
return True
cur = cur.next
return False
完整代码如下:
class Node():
def __init__(self,elem):
self.elem = elem
self.next = None
class Single_LinkList():
def __init__(self,node = None):
self.head = node
def is_empty(self):
"""链表是否为空"""
if self.head == None:
return True
else:
return False
def length(self):
"""链表长度"""
len = 0
cur = self.head
while cur != None:
len = len + 1
cur = cur.next
return len
def travel(self):
"""遍历链表"""
cur = self.head
while cur != None:
print(cur.elem)
cur = cur.next
def add(self,item):
"""链表头部进行添加元素"""
node = Node(item)
node.next = self.head
self.head = node
def append(self,item):
"""链表尾部进行添加元素"""
node = Node(item)
cur = self.head
if self.is_empty():
self.head = node
else:
while cur.next != None:
cur = cur.next
cur.next = node
node.next = None
def insert(self,pos,item):
"""
在指定位置上添加元素
:param pos: 从0开始
:param item:
:return:
"""
node = Node(item)
count = 0
cur = self.head
while cur != None:
if count == pos - 1:
node.next = cur.next
cur.next = node
break
cur = cur.next
count = count + 1
def remove(self,item):
"""删除某一个节点"""
cur = self.head
count = 0
if self.is_empty():
print("删除失败")
elif self.head.elem == item:
self.head = self.head.next
else:
while cur.next != None:
if cur.next.elem == item:
cur.next = cur.next.next
break
cur = cur.next
def search(self,item):
"""查看某个节点是否存在"""
cur = self.head
while cur != None:
if cur.elem == item:
return True
cur = cur.next
return False
测试代码如下:
if __name__ == "__main__":
Linklist = Single_LinkList()
print(Linklist.is_empty())
print(Linklist.length())
print(Linklist.append(1))
print(Linklist.is_empty())
print(Linklist.length())
print(Linklist.append(2))
print(Linklist.append(3))
print(Linklist.append(4))
print(Linklist.append(5))
Linklist.add(8)
Linklist.insert(1,10)
Linklist.remove(8)
# Linklist.remove(8)
Linklist.travel()
print(Linklist.search(1))
结果如下: