Bootstrap

学习日志014--用python实现顺序表

之前我们用c语言实现顺序表,今天就然我们用python来复习一遍

一、创建顺序表

python的类可以代替c中的结构体作为复合数据类型。

创建学生类,并用装饰器将方法属性化,便于之后的修改。属性装饰器,可以在保护私有属性,可以便捷调用方法

class Student:
    def __init__(self,sid,name,score):
        self.__id = sid
        self.__s_name = name
        self.__score = score

    @property
    def s_name(self):
        return self.__s_name

    @s_name.setter
    def s_name(self,name):
        self.__s_name = name

    @property
    def id(self):
        return self.__id

    @id.setter
    def id(self, sid):
        self.__id = sid

    @property
    def score(self):
        return self.__score

创建顺序表类,并初始化

class SeqList:
    def __init__(self,capacity=10):
        self.size = 0
        self.capacity = capacity
        self.data = [None] * self.capacity

设置添加方法,将学生对象装入顺序表

# 插入元素
    def insert(self,value,sub):
        # 判断是否符合插入条件
        if self.is_full() or sub < 0 or sub > self.size:
            print("添加失败")
            return False
        else:
            i = 0
            while i<self.size-sub:
                self.data[self.size-i] = self.data[self.size-i-1]
                i += 1
            self.data[sub] = value
            self.size += 1

 删除指定元素

#删除元素
    def delete(self,sub):
        if self.is_empty() or sub < 0 or sub > self.size -1:
            print("删除失败")
            return False
        else:
            i = sub
            while i < self.size-1:
                self.data[i] = self.data[i+1]
                i += 1
            self.size -= 1
    # 遍历元素

根据学号查找

    # 按学号查找返回下标

    def find(self,s_id):
        if self.is_empty():
            print("查找失败")
            return -1
        else:
            i = 0
            while i<self.size:
                if self.data[i].id == s_id:
                    return i
                i += 1
            else:
                print("查找失败")
                return -1

遍历顺序表

    # 遍历元素

    def show(self):
        i = 0
        while 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)
                        # 删除后比较元素下标回退
                        continue
                    j += 1
                i += 1
            return

运行模块

    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 = sid
        self.__s_name = name
        self.__score = score

    def __str__(self):
        return f"{self.s_name} {self.id} {self.score}"

    @property
    def s_name(self):
        return self.__s_name

    @s_name.setter
    def s_name(self,name):
        self.__s_name = name

    @property
    def id(self):
        return self.__id

    @id.setter
    def id(self, sid):
        self.__id = sid

    @property
    def score(self):
        return self.__score

    @score.setter
    def score(self, score):
        self.__score = score

class SeqList:
    def __init__(self,capacity=30):
        self.size = 0 # 设置顺序表元素个数,并初始化归零
        self.capacity = capacity # 置顺序表容量,默认为30
        self.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 False
        else:
            i = 0
            while i<self.size-sub:
                self.data[self.size-i] = self.data[self.size-i-1]
                i += 1
            self.data[sub] = value
            self.size += 1

#删除元素
    def delete(self,sub):
        if self.is_empty() or sub < 0 or sub > self.size -1:
            print("删除失败")
            return False
        else:
            i = sub
            while i < self.size-1:
                self.data[i] = self.data[i+1]
                i += 1
            self.size -= 1
    # 遍历元素

    def show(self):
        i = 0
        while i<self.size:
            print(self.data[i],end="")
            i += 1
        print()


    # 按学号查找返回下标

    def find(self,s_id):
        if self.is_empty():
            print("查找失败")
            return -1
        else:
            i = 0
            while i<self.size:
                if self.data[i].id == s_id:
                    return i
                i += 1
            else:
                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)
                        # 删除后比较元素下标回退
                        continue
                    j += 1
                i += 1
            return

if __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()

;