数据结构 C#语言版 第2章 线性表(2)
Python实现一个顺序表的基本功能:
[cpp] "Algri.py"
class SeqList(object):
def __init__(self, length):
self.MaxLength=length
self.__data=range(length)
self.Last=-1;
"索引器get"
def __getitem__(self, key):
if key>self.Last or key<0:
print "The given key is Error!"
return
else:
return self.__data[key]
"索引器set"
def __setitem__(self, key, value):
if key>self.Last or key<0:
print "The given key is Error!"
return
else:
self.__data[key]=value
"顺序表长度"
def GetLength(self):
return self.Last+1
"清空顺序表"
def Clear(self):
last=-1
"判断顺序表是否为空"
def IsEmpty(self):
if self.Last==-1:
return True
else:
return False
"判断顺序表是否为满"
def IsFull(self):
if self.Last==self.MaxLength-1:
return True
else:
return False
"在顺序表的末尾添加新元素"
def Append(self,item):
if(self.IsFull()):
print "List is full"
else:
self.Last+=1
self.__data[self.Last]=item
"在第i个位置插入一个数据元素"
def Insert(self,item,i):
if(self.IsFull()):
print "List is full"
elif i<0 or i>self.Last+1:
print "Position is error"
elif i==self.Last+1:
self.Last+=1
self.__data[self.Last]=item
else:
for j in self.__data[self.Last:i-1:-1]:
self.__data[j+1]=self.__data[j]
self.__data[i]=item
self.Last+=1
"删除第i个位置的数据元素"
def Delete(self,i):
if self.IsEmpty():
print "List is empty"
elif i<0 or i>self.Last:
print "Position is error!"
elif i==self.Last:
self.Last-=1
else:
for j in self.__data[i:Last-1]:
self.__data[j]=self.__data[j+1]
self.Last-=1
"获得顺序表第i个数据元素"
def GetItem(self,i):
if(self.IsEmpty() or i<0 or i> self.Last):
print "List is empty or position is error!"
else:
return self.__data[i]
"在顺序表中查找为value的数据元素"
def Locate(self,value):
if(self.IsEmpty()):
print "List is empty!"
for i in range(0,self.Last):
if self.__data[i]==value:
return i
"Algri.py"
class SeqList(object):
def __init__(self, length):
self.MaxLength=length
self.__data=range(length)
self.Last=-1;
"索引器get"
def __getitem__(self, key):
if key>self.Last or key<0:
&nb
补充:软件开发 , C# ,