notebooks
  • notebooks
  • _planning
    • 2022 OKR
    • basketball
    • swimming
  • communication
    • Dubbo
    • Kafka
    • Messaging
    • RPC
    • Thrift
  • computation
    • map-reduce
  • cs-basic-knowledge
    • computer-architecture
    • data-structure-and-algorithms
    • networks
    • os
  • devops
    • Docker
    • Linux
    • Prometheus
    • operations
    • security
    • trouble-shooting
  • distributed-knowledge
    • Zookeeper_CMD
    • distributed-system
  • game-engine
    • Unity
  • others
    • appium使用
  • protocols
    • http(s)协议
    • 官方链接
    • sip
  • storage
    • Elasticsearch
    • GuavaCache
    • MySQL
    • MySQL_CMD
    • NoSQL
    • Redis
    • Redis_CMD
  • system-design
    • system-design
  • tools
    • Git
    • IDEA
    • Mac
    • VScode
    • Vim
  • _working
    • doc-template
      • backend-design-review
      • correction-of-error
      • service-review
    • process
      • domain-backup
      • oncall
  • blogs
    • history
      • 8088/8086微处理器
      • 8088/8086指令系统
      • CSS-DOM
      • CSS定位
      • CSS工作原理
      • CSS控制背景
      • CSS浮动布局
      • CSS盒模型
      • Chrome开发者工具使用方法
      • DOM
      • Django Model模型层学习
      • Django-REST-framework Serializers学习
      • Django-REST-framework Views和ViewSets学习
      • Django View视图层学习
      • Gvim下Emmet安装及使用教程
      • HTTP协议简介
      • HashMap原理初探
      • JavaScript简史
      • JavaScript语法
      • Java内存模型和GC机制
      • Java基础——Lambda学习
      • Java基础——方法引用
      • Java基础——枚举类型
      • Java类加载机制
      • KMP算法
      • Kafka学习
      • Linux下用命令行编译Java程序
      • MathJax简介和基本用法
      • Python实现常见数据结构
      • Python装饰器总结
      • TCP协议的三次握手和四次挥手
      • Thrift学习
      • asyncio学习
      • markdown的常用语法
      • 修改hosts文件实现翻墙
      • 充实文档的内容
      • 关系数据库
      • 关系数据库标准语言SQL(一)
      • 关系数据库标准语言SQL(二)
      • 关系数据理论
      • 关系查询处理和查询优化
      • 内联元素和块级元素
      • 剑指offer算法题练习
      • 动态创建标记
      • 图形化用户界面
      • 在Eclipse中使用Maven构建Java Web项目
      • 增加微博秀遇到的一些问题
      • 处理机调度
      • 如何用github和hexo搭建个人博客
      • 存储管理
      • 存储系统的层次结构
      • 学习模仿lionhit网站首页的过程总结
      • 实用的GitHub小技巧
      • 并发控制
      • 循环与分支程序设计
      • 指令系统的设计
      • 指令级并行及其开发——硬件方法
      • 搭建自己的VPN服务器
      • 操作系统用户界面
      • 数据库安全性
      • 数据库完整性
      • 数据库恢复技术
      • 数据库绪论
      • 数据库编程
      • 数据库设计
      • 数据抽象
      • 文件系统
      • 文法和语言
      • 最佳实践
      • 案例研究:JavaScript图片库
      • 案例研究:图片库改进版
      • 汇编语言程序格式
      • 汇编语言程序设计基础知识
      • 流水线技术
      • 深度优先搜索和广度优先搜索
      • 牛客网——网易2017秋招编程题集合
      • 用JavaScript实现动画效果
      • 第一篇博客
      • 经典排序算法总结(Java实现)
      • 经典查找算法总结(Java实现)
      • 综合示例
      • 编译原理引论
      • 背包、队列和栈
      • 虚拟机安装Linux系统及常用软件
      • 计算机操作系统绪论
      • 计算机系统结构的基础知识
      • 设备管理
      • 设计模式之代理模式
      • 设计模式之单例模式
      • 设计模式之工厂模式
      • 设计模式之策略模式
      • 设计模式之观察者模式
      • 词法分析
      • 进程管理
      • 闭包
      • 阻止Google自动跳转到香港服务器的方法
      • 项目部署过程
  • programming-language
    • C#
      • C#
    • C&C++
      • C
    • C&C++
      • C++
    • Java
      • GoogleGuice
    • Java
      • JVM
    • Java
      • Java
    • Java
      • Maven
    • Java
      • Mybatis
    • Java
      • Spring知识
    • Java
      • SpringBoot
    • Java
      • Tomcat
    • Python
      • Python
    • Shell
      • Shell
  • wheels
    • dcc
      • 产品调研
      • 方案设计
    • red-envelope
      • 方案设计
    • short-url
      • 短链接服务
    • sso
      • 方案设计
Powered by GitBook
On this page
  • 栈
  • 队列
  • 链表
  • 二叉树
  • 图
  1. blogs
  2. history

Python实现常见数据结构

使用Python实现常见的数据结构,如栈、队列、链表、二叉树、图等。

栈

class Stack():
    def __init__(st, size):
        st.stack = []
        st.size = size
        st.top = 0
    
    def push(st, content):
        if st.isFull():
            print('Stack is full!')
        else:
            st.stack.append(content)
            st.top = st.top + 1

    def pop(st):
        if st.isEmpty():
            print('Stack is empty!')
        else:
            st.top = st.top - 1
            return st.stack[st.top]

    def isFull(st):
        if st.top == st.size:
            return True
        else:
            return False

    def isEmpty(st):
        if st.top == 0:
            return True
        else:
            return False

队列

class Queue():
    def __init__(qu, size):
        qu.queue = []
        qu.size = size
        qu.head = 0
        qu.tail = 0
    
    def isEmpty(qu):
        if qu.head == qu.tail:
            return True
        else:
            return False

    def isFull(qu):
        if qu.tail - qu.head == qu.size:
            return True
        else:
            return False

    def enqueue(qu, content):
        if qu.isFull():
            print('Queue is full!')
        else:
            qu.queue.append(content)
            qu.tail = qu.tail + 1

    def dequeue(qu):
        if qu.isEmpty():
            print('Queue is empty!')
        else:
            qu.head = qu.head + 1
            return qu.queue[qu.head - 1]

链表

class ListNode():
    def __init__(self, val = None, next = None):
        self.val = val
        self.next = next

class LinkedList():
    #列表转链表
    def listToLinkedList(self, list):
        head = ListNode(0)
        p = head
        for each in list:
            p.next = ListNode(each)
            p = p.next
        return head.next
    #打印链表
    def printLinkedList(self, head):
        p = head
        while p != None:
            print(p.val)
            p = p.next

二叉树

class Tree():
    def __init__(self, leftnode = None, rightnode = None, data = None):
        self.leftnode = leftnode
        self.rightnode = rightnode
        self.data = data

class BTree():
    def __init__(self, base = None):
        self.base = base
    def isEmpty(self):
        if self.base == None:
            return True
        else:
            return False
    #前序遍历,NLR,根左右
    def preorder(self, node):
        if node == None:
            return
        print(node.data)
        self.preorder(node.leftnode)
        self.preorder(node.rightnode)
    #中序遍历,LNR,左根右
    def order(self, node):
        if node == None:
            return
        self.order(node.leftnode)
        print(node.data)
        self.order(node.rightnode)
    #后序遍历,LRN,左右根
    def postorder(self, node):
        if node == None:
            return
        self.postorder(node.leftnode)
        self.postorder(node.rightnode)
        print(node.data)

图

'''
使用dict和list构造图
例如:
A -> B
A -> C
B -> C
B -> D
C -> D
D -> C
E -> F
F -> C
'''

graph = {'A': ['B', 'C'],
         'B': ['C', 'D'],
         'C': ['D'],
         'D': ['C'],
         'E': ['F'],
         'F': ['C']}

#寻找一条路径
def findPath(graph, start, end, path = []):
    path = path + [start]
    if start == end:
        return path
    if start not in graph:
        return None
    for node in graph[start]:
        newPath = findPath(graph, node, end, path)
        if newPath:
            return newPath
    return path



#寻找所有路径
def findAllPaths(graph, start, end, path = []):
    path = path + [start]
    if start == end:
        return [path]
    if start not in graph:
        return []
    paths = []
    for node in graph[start]:
        if node not in path:
            newPaths = findAllPaths(graph, node, end, path)
            for each in newPaths:
                paths.append(each)
    return paths



#寻找最短路径
def findShortestPath(graph, start, end, path = []):
    path = path + [start]
    if start == end:
        return path
    if start not in graph:
        return None
    shortestPath = None
    for node in graph[start]:
        if node not in path:
            newPath = findShortestPath(graph, node, end, path)
            if newPath:
                if not shortestPath or len(newPath) < len(shortestPath):
                    shortestPath = newPath
    return shortestPath
PreviousMathJax简介和基本用法NextPython装饰器总结

Last updated 3 years ago