栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

数据结构—线性表(python实现)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数据结构—线性表(python实现)

利用列表对象及其方法实现线性表操作
Object:SqList
Method:
init()初始化
EmptyList()判断列表是否为空
ClearList()清楚列表
GetElem()获取列表所以为i元素的值
LocateElem()定位元素位置(索引)
ListInsert()在表中插入元素
ListDelete()删除元素中位置为i的元素
ListLength()获取线性表的长度

#encoding:gbk
class SqList():
	def __init__(self):
		self.ls=[2,3,4,3,]
	def EmptyList(self):
		if self.ls==[]:
			return True
		else:
			return False
	def ClearList(self):
		try:
			self.ls.clear()
			return True
		except:
			return False
	def GetElem(self,i):
		try:
			e=self.ls[i]
			return e
		except:
			return False
	def LocateElem(self,e):
		try:
			if e not in self.ls:
				print("Doesn't exist!")
			else:
				return self.ls.index(e) 
		except:
			return False
	def ListInsert(self,i,e):
		try:
			self.ls.insert(i,e)
			return self.ls
		except:
			return False
	def ListDelete(self,i,e):
		try:
			e=self.pop(i)
			return e
		except:
			return False
	def ListLength(self):
		return len(self.ls)

test_ls=SqList()
#长度函数测试
print(test_ls.ListLength())
#插入函数测试
print(test_ls.ListInsert(2,2))#失败,待优化->False	#已优化,列表对象方法.insert(index,val)
#查找函数测试
print(test_ls.LocateElem(2))
#获取函数测试
print(test_ls.GetElem(1))
#清空函数测试
print(test_ls.ClearList())
#判断空列表测试
print(test_ls.EmptyList())

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/965640.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号