网站LOGO
公爵书房 | 技术分享
页面加载中
10月4日
网站LOGO 公爵书房 | 技术分享
以指键之轻,承载知识之重
菜单
  • 公爵书房 | 技术分享
    以指键之轻,承载知识之重
    用户的头像
    首次访问
    上次留言
    累计留言
    我的等级
    我的角色
    打赏二维码
    打赏博主
    Python数据结构(一)·顺序表
    点击复制本页地址
    微信扫一扫
    文章二维码
    文章图片 文章标题
    创建时间
  • 一 言
    确认删除此评论么? 确认
  • 本弹窗介绍内容来自,本网站不对其中内容负责。

    Python数据结构(一)·顺序表

    公爵 · 原创 ·
    笔记 · python数据结构顺序表
    共 3070 字 · 约 1 分钟 · 9
    本文最后更新于2023年09月02日,已经过了31天没有更新,若内容或图片失效,请留言反馈
    list 是一种元素个数可变的线性表,采用了分离式技术实现的动态顺序表。可以加入和删除元素,并在各种操作中维持已有元素的顺序(即保序)。

    1.1 创建顺序表

    python 代码:
    # 创建顺序表
    def CreateSeqList(self):
        element = input("please enter(input #:end):")
        while element != '#':
            self.seqList.append(int(element))
            element = input("please enter(input #:end):")

    1.2 查找元素

    python 代码:
    # 查找顺序表中某一个元素
    def FindElement(self):
        key = int(input("please enter what you want to find:"))
        if key in self.seqList:
            keyPosition = self.seqList.index(key)
            result = keyPosition
        else:
            result = "none"
        return result

    1.3 插入元素

    python 代码:
    # 在指定位置上插入元素
    def InsertElement(self):
        postion = int(input("请输入要插入的位置:"))
        key = int(input("请输入要插入的值:"))
        print("插入前顺序:", self.seqList)
        self.seqList.insert(postion, key)
        print("插入后顺序:", self.seqList)

    1.4 删除元素

    python 代码:
    # 在指定位置上删除元素
    def DeleteElement(self):
        postion = int(input("请输入要删除元素的位置:"))
        print("删除前的顺序表:", self.seqList)
        if postion < len(self.seqList) and postion >= 0:
            del self.seqList[postion]
            return self.seqList
        else:
            print("不存在啊老铁,删除个毛线啊")

    1.5 完整代码

    python 代码:
    class SequenceList:
    
        # 初始化
        def __init__(self):
            self.seqList = [] # 初始化一个空表
    
        # 创建顺序表
        def CreateSeqList(self):
            element = input("please enter(input #:end):")
            while element != '#':
                self.seqList.append(int(element))
                element = input("please enter(input #:end):")
    
        # 查找顺序表中某一个元素
        def FindElement(self):
            key = int(input("please enter what you want to find:"))
            if key in self.seqList:
                keyPosition = self.seqList.index(key)
                result = keyPosition
            else:
                result = "none"
            return result
    
        # 在指定位置上插入元素
        def InsertElement(self):
            postion = int(input("请输入要插入的位置:"))
            key = int(input("请输入要插入的值:"))
            print("插入前顺序:", self.seqList)
            self.seqList.insert(postion, key)
            print("插入后顺序:", self.seqList)
    
        # 在指定位置上删除元素
        def DeleteElement(self):
            postion = int(input("请输入要删除元素的位置:"))
            print("删除前的顺序表:", self.seqList)
            if postion < len(self.seqList) and postion >= 0:
                del self.seqList[postion]
                return self.seqList
            else:
                print("不存在啊老铁,删除个毛线啊")
    
        # 遍历顺序表元素
        def TraverseElement(self):
            for i in range(len(self.seqList)):
                print(self.seqList[i], end =' ')
    
        # 判空
        def IsEmpty(self):
            return len(self.seqList) == 0
    
        # 销毁顺序表
        def DestorySeqList(self):
            print("销毁前顺序表:", self.seqList)
            self.__init__()
            print("销毁后顺序表:", self.seqList)
    
    
    # 测试
    if __name__ == '__main__':
        list = SequenceList()
        list.CreateSeqList()
        print(list.seqList)
        print(list.FindElement())
        list.InsertElement()
        list.DeleteElement()
        list.TraverseElement()
        list.IsEmpty()
        list.DestorySeqList()
    声明:本文由 公爵(博主)原创,依据 CC-BY-NC-SA 4.0 许可协议 授权,转载请注明出处。

    还没有人喜爱这篇文章呢

    发一条! 发一条!
    博客logo 公爵书房 | 技术分享 以指键之轻,承载知识之重 51统计 百度统计
    MOEICP 萌ICP备20226257号 ICP 赣ICP备2022001242号-1 ICP 闽公网安备35020502000606号 又拍云 本站由又拍云提供CDN加速/云存储服务

    🕛

    本站已运行 1 年 257 天 7 小时 23 分

    🌳

    自豪地使用 Typecho 建站,并搭配 MyLife 主题
    公爵书房 | 技术分享. © 2022 ~ 2023.
    网站logo

    公爵书房 | 技术分享 以指键之轻,承载知识之重
     
     
     
     
    壁纸