class Ordlist():
def __init__(self,capacity,num):
self._cap = capacity # 表容量
self.num = num # 元素数量
self.list = [None]*self._cap # 初始化顺序表
def print_list(self):
if self.is_empty():
mylist=[]
else:
mylist = self.list[0:self.num]
print(mylist)
def is_empty(self):
return self.num == 0
def is_full(self):
return self._cap == self.num
def append_elem(self,elem):
if not self.is_full():
self.list[self.num] = elem
self.num += 1
else:
return "list is full"
def prepend_elem(self,elem):
if not self.is_full():
self.list[1:] = self.list[0:-1]
self.list[0] = elem
self.num += 1
else:
return "list is full"
def insert(self,index,elem):
if not self.is_full():
list_end = self.list[index:]
self.list[index] =elem
self.list = self.list[0:index+1]+list_end
self.num += 1
else:
return "list is full"
def del_end(self):
if not self.is_empty():
self.list[self.num-1] = None
self.num -= 1
else:
print("list is empty")
def del_index(self,i):
if not self.is_empty():
self.list=self.list[0:i]+self.list[i+1:]+[None]
self.num -= 1
else:
print("list is empty")
'''一个表的操作:
1.是否空
2.是否满
3.后端添加
4.前端添加
5.中间添加
6.后端删除
7.前端删除
8.任意位置删除
9.遍历
10.查找某元素第一次出现的位置'''