之前我们用c语言实现顺序表,今天就然我们用python来复习一遍
一、创建顺序表
python的类可以代替c中的结构体作为复合数据类型。
创建学生类,并用装饰器将方法属性化,便于之后的修改。属性装饰器,可以在保护私有属性,可以便捷调用方法
class Student:def __init__(self,sid,name,score):self.__id = sidself.__s_name = nameself.__score = score@propertydef s_name(self):return self.__s_name@s_name.setterdef s_name(self,name):self.__s_name = name@propertydef id(self):return self.__id@id.setterdef id(self, sid):self.__id = sid@propertydef score(self):return self.__score
创建顺序表类,并初始化
class SeqList:def __init__(self,capacity=10):self.size = 0self.capacity = capacityself.data = [None] * self.capacity
设置添加方法,将学生对象装入顺序表
# 插入元素def insert(self,value,sub):# 判断是否符合插入条件if self.is_full() or sub < 0 or sub > self.size:print("添加失败")return Falseelse:i = 0while i<self.size-sub:self.data[self.size-i] = self.data[self.size-i-1]i += 1self.data[sub] = valueself.size += 1
删除指定元素
#删除元素def delete(self,sub):if self.is_empty() or sub < 0 or sub > self.size -1:print("删除失败")return Falseelse:i = subwhile i < self.size-1:self.data[i] = self.data[i+1]i += 1self.size -= 1# 遍历元素
根据学号查找
# 按学号查找返回下标def find(self,s_id):if self.is_empty():print("查找失败")return -1else:i = 0while i<self.size:if self.data[i].id == s_id:return ii += 1else:print("查找失败")return -1
遍历顺序表
# 遍历元素def show(self):i = 0while i<self.size:print(self.data[i])i += 1
去重
# 去重def ded(self):# 判断是否为空if self.is_empty():print("去重失败")else:i = 0# 循环遍历每一个元素while i<self.size-1:j = i + 1# 将元素与顺序表中其他元素比较while j<self.size:if self.data[i].id == self.data[j].id:# 调用删除函数删除数值相同的元素self.delete(j)# 删除后比较元素下标回退continuej += 1i += 1return
运行模块
s1 = Student(1001, "小米", 98)s2 = Student(1002, "小红", 98)s3 = Student(1003, "小华", 98)s4 = Student(1004, "小路", 98)s5 = Student(1005, "小王", 98)s6 = Student(1001, "小米", 98)S = SeqList()S.insert(s1, 0)S.insert(s2, 1)S.insert(s3, 1)S.insert(s4, 1)S.insert(s5, 1)S.insert(s6, 1)S.show()a = S.find(1002)S.delete(a)S.show()S.ded()S.show()
类模块全部代码
class Student:def __init__(self,sid,name,score):self.__id = sidself.__s_name = nameself.__score = scoredef __str__(self):return f"{self.s_name} {self.id} {self.score}"@propertydef s_name(self):return self.__s_name@s_name.setterdef s_name(self,name):self.__s_name = name@propertydef id(self):return self.__id@id.setterdef id(self, sid):self.__id = sid@propertydef score(self):return self.__score@score.setterdef score(self, score):self.__score = scoreclass SeqList:def __init__(self,capacity=30):self.size = 0 # 设置顺序表元素个数,并初始化归零self.capacity = capacity # 置顺序表容量,默认为30self.data = [None] * self.capacity # 设置空顺序表# 判断表满def is_full(self):return self.size == self.capacity# 判断表空def is_empty(self):return self.size == 0# 插入元素def insert(self,value,sub):# 判断是否符合插入条件if self.is_full() or sub < 0 or sub > self.size:print("添加失败")return Falseelse:i = 0while i<self.size-sub:self.data[self.size-i] = self.data[self.size-i-1]i += 1self.data[sub] = valueself.size += 1#删除元素def delete(self,sub):if self.is_empty() or sub < 0 or sub > self.size -1:print("删除失败")return Falseelse:i = subwhile i < self.size-1:self.data[i] = self.data[i+1]i += 1self.size -= 1# 遍历元素def show(self):i = 0while i<self.size:print(self.data[i],end="")i += 1print()# 按学号查找返回下标def find(self,s_id):if self.is_empty():print("查找失败")return -1else:i = 0while i<self.size:if self.data[i].id == s_id:return ii += 1else:print("查找失败")return -1# 去重def ded(self):# 判断是否为空if self.is_empty():print("去重失败")else:i = 0# 循环遍历每一个元素while i<self.size-1:j = i + 1# 将元素与顺序表中其他元素比较while j<self.size:if self.data[i].id == self.data[j].id:# 调用删除函数删除数值相同的元素self.delete(j)# 删除后比较元素下标回退continuej += 1i += 1returnif __name__ == '__main__':s1 = Student(1001, "小米", 98)s2 = Student(1002, "小红", 98)s3 = Student(1003, "小华", 98)s4 = Student(1004, "小路", 98)s5 = Student(1005, "小王", 98)s6 = Student(1001, "小米", 98)S = SeqList()S.insert(s1, 0)S.insert(s2, 1)S.insert(s3, 1)S.insert(s4, 1)S.insert(s5, 1)S.insert(s6, 1)S.show()a = S.find(1002)S.delete(a)S.show()S.ded()S.show()