#列表的插入、查找、删除实现
class TestArray:
def __init__(self, capacity) ->None:
# 由于python的list是动态扩展的,实现底层具有固定容量、占用一段连续的内存空间的数组,所以用-1来作为无效元素的标识
self.data = [-1]*capacity
#列表实际存储的值的个数
self.count = 0
#列表的大小
self.n = capacity
def addValue(self, index, value):
"""
列表中插入元素
:param index: 插入位置
:param value: 插入的数据
:return:
"""
if self.n == self.count:
return False
if index<0 or index>self.n:
return False
for i in range(self.count, index,-1):
self.data[i] = self.data[i-1]
self.data[index] = value
self.count += 1
return True
def find(self, index):
"""
查找元素值
:param index: 元素所在位置
:return:
"""
if index<0 or index>=self.count:
retu
python实现列表插入、查找、删除
最新推荐文章于 2023-02-21 16:29:57 发布