Python顺序链表
顺序链表
#encoding=UTF-8
class SXlist():
def __init__(self,max):
self.maxlenth=max
self.data=[]
self.lenth=len(self.data)
def add(self,key):
self.lent=len(self.data)
if self.lent<=(self.maxlenth-1):
self.data=self.data+[key]
self.lenth=self.lenth+1
else :
print("数组已满")
def isempty(self):
if self.lenth==0:
print("列表为空")
return True
else :
print("列表不为空")
return False
def isfull(self):
if self.lenth==max:
print("列表满了")
return True
else :
print("列表不满")
return False
def getByelem(self,elem):
res=[]
for i in range(self.lenth):
if self.data[i]==elem:
res=res+[i]
if len(res)==0:
print("未找到")
return False
else:
print("找到了,下标如下")
print(res)
return res
def deleteIndex(self,num):
if self.isempty():
print("空表")
else:
if num>self.lenth-1:
print("删除的索引项超过范围")
return False
else:
for i in range(self.lenth):
if i>num:
p=self.data[i]
self.data[i-1]=p
self.data=self.data[:-1]
self.lenth=self.lenth-1
print("删除OK")
return True
def insert(self,key,num):
if num<0 and num>self.lenth+1:
print("过界")
elif num==self.lenth:
self.add(key)
else:
p=self.data[self.lenth-1]
for i in range(self.lenth):
if i>num and i<self.lenth :
self.data[self.lenth-i]=self.data[self.lenth-i-1]
self.data[num]=key
self.add(p)
print(self.data)
print(self.lenth)
def getByIndex(self,num):
if num<0 and num>self.lenth:
print("过界")
else:
res=self.data[num]
print(res)
def count(self):
print(self.lenth)
return self.lenth
sx=SXlist(10)
print(sx.maxlenth)
print(sx.data)
print(sx.lenth)
sx.isempty()
sx.isfull()
sx.add(3)
print(sx.maxlenth)
print(sx.data)
print(sx.lenth)
sx.isempty()
sx.isfull()
sx.add(4)
sx.add(5)
sx.add(6)
sx.add(7)
sx.add(8)
print(sx.data)
sx.deleteIndex(3)
sx.insert(9, 0)
sx.getByIndex(3)
sx.insert(33, 6)
sx.getByelem(33)
sx.count()