IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> python数据结构——(3)表及其应用 -> 正文阅读

[数据结构与算法]python数据结构——(3)表及其应用

表,分为无序表有序表

无序表

无序表概念:数据项按照相对位置存在的数据列表,跟数据的大小无关。
例如:[21, 44, 5, 65, 32]

在Python中,无序表的数据结构完全可以用Python本身的List表示。list的方法和功能如下:

方法功能
.append(item)将item插入到列表最后
.index(item)返回item在列表中的位置
.insert(pos, item)在列表中pos的位置插入item
.pop()移除列表中的最后一个元素
.pop(pos)移除列表中位置为pos的元素

这对于c/c++来说,几乎是作弊。为了学习表的精髓和灵魂,我们同样采用链表实现无序表和有序表。
链表示意图

最基本元素:节点Node。上图中一个蓝色和黄色部分组成一个节点。其至少包含两个基本信息:(1)数据项本身;(2)指向下一节点的引用信息。

# 链表实现:节点Node
class Node:
    def __init__(self, initdata):
        self.data = initdata
        self.next = None
        
    def getData(self):
        return self.data
    
    def getNext(self):
        return self.next
    
    def setData(self, newdata):
        self.data = newdata
        
    def setNext(self, newnext):
        self.next = newnext

测试代码:

temp = Node(93)
temp.getData()

输出结果:

93

无序表的实现:

 # 无序表
class UnorderedList:
    def __init__(self):
        self.head = None
        
    def isEmpty(self):
        return self.head == None
        
    # add方法实现
    def add(self, item):
        temp = Node(item)
        temp.setNext(self.head)
        self.head = temp
        
    # size实现
    def size(self):
        current = self.head
        count = 0
        while current != None:
            count += 1
            current = current.getNext()
        return count
    
    # search 实现
    def search(self, item):
        current = self.head
        found = False
        while current != None and not found:
            if current.getData() == item:
                found = True
            else:
                current = current.getNext()
        return found
    
    # remove(item)
    def remove(self, item):
        current = self.head
        previous = None
        found = False
        while not found:
            if current.getData() == item:
                found = True
            else:
                previous = current
                current = current.getNext()
                
        if previous == None:
            self.head = current.getNext()
        else:
            previous.setNext(current.getNext())
方法注意事项
add(item)链表中最容易添加元素的位置是“表头”,
size()没有方法可以直接读取列表长度,所以需要从头遍历整个链表得到size
search(item)寻找元素同样需要遍历整个链表
remove(item)移除之前,需要先找到元素。但是移除的时候,要将该移除节点的指向信息传递给前一个节点,然后删除该节点

有序表

有序表中的数据按照某种可比性质放入列表,例如数字大小、字母的先后顺序,这些性质决定了数据在列表中的位置。

有序表中方法和无序表基本类似,在添加元素add(item)和寻找元素search(item)有细微区别。

# 有序表OrderList实现
class OrderedList:
    def __init__(self):
        self.head = None
        
    def isEmpty(self):
        return self.head == None
        
    # size实现
    def size(self):
        current = self.head
        count = 0
        while current != None:
            count += 1
            current = current.getNext()
        return count
    
    # remove(item)
    def remove(self, item):
        current = self.head
        previous = None
        found = False
        while not found:
            if current.getData() == item:
                found = True
            else:
                previous = current
                current = current.getNext()
                
        if previous == None:
            self.head = current.getNext()
        else:
            previous.setNext(current.getNext())
            
        # search 方法 当前节点的数据大于找的数据,就False
        def search(self, item):
            current = self.head
            found = False
            stop = False
            while current != None and not found and not stop:
                if current.getData() == item:
                    found = True
                else:
                    if current.getData() > item:
                        stop = True
                    else:
                        current = current.getNext()
            return found
        
        # add 方法实现
        def add(self, item):
            current = self.head
            previous = None
            stop = False
            while current != None and not stop:
                if current.getData() > item:
                    stop = True
                else:
                    previous = current
                    current = current.getNext()
            temp = Node(item)
            
            # 插入在表头
            if previous == None:
                temp.setNext(self.head)
                self.head = temp
            # 插入在表中
            else:
                temp.setNext(current)
                previous.setNext(temp)
  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2021-07-10 11:38:31  更:2021-07-10 11:38:55 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年4日历 -2024/4/28 8:19:33-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码