欢迎!

Python 中的数据结构与算法

Miriam Antona

Software Engineer

算法与数据结构的重要性

  • 通过数据结构算法
    • 高效代码解决日常问题
  • 课程可用任意编程语言讲授
Python 中的数据结构与算法

算法与数据结构

  • 算法:解决问题的一组指令

    1. 设计

      算法设计的示意图。

    2. 编码

      算法代码的示意图。

  • 数据结构: 在执行算法时存储和处理数据
    • 高级数据结构:链表、栈、队列…
Python 中的数据结构与算法

链表

包含制作面包步骤的链表示例。

  • 通过链接连接的数据序列
Python 中的数据结构与算法

链表——结构

链表中一个节点的表示。

Python 中的数据结构与算法

链表——结构

链表中一个节点的表示,第一部分为"DATA"。

Python 中的数据结构与算法

链表——结构

链表中一个节点的表示:第一部分为"DATA",第二部分为带指针的"NEXT"。

Python 中的数据结构与算法

链表——结构

两个链表节点通过链接相连的表示。

Python 中的数据结构与算法

链表——结构

多个链表节点通过链接相连的表示。

Python 中的数据结构与算法

链表——结构

链表的表示,最后一个节点指向 null。

Python 中的数据结构与算法

链表——结构

链表的表示,第一个节点标有"HEAD"。

Python 中的数据结构与算法

链表——结构

链表的表示,最后一个节点标有"TAIL"。

  • 数据无需存放在连续内存块中
  • 数据可位于任意可用内存地址
Python 中的数据结构与算法

单链表

单链表的表示。

  • 单个链接:单链表
Python 中的数据结构与算法

双向链表

双向链表的表示。

  • 双向两个链接:双向链表
Python 中的数据结构与算法

链表——实际用途

  • 实现其他数据结构:
    • 队列
  • 通过前后导航访问信息
    • 浏览器
    • 音乐播放列表
Python 中的数据结构与算法

链表——Node 类

class Node:
  def __init__(self, data):
    self.data = data
    self.next = None
Python 中的数据结构与算法

链表——LinkedList 类

class LinkedList:
  def __init__(self):
    self.head = None
    self.tail = None
Python 中的数据结构与算法

链表——方法

  • insert_at_beginning()
  • remove_at_beginning()
  • insert_at_end()
  • remove_at_end()
  • insert_at()
  • remove_at()
  • search()
Python 中的数据结构与算法

链表——insert_at_beginning

单链表的表示。

Python 中的数据结构与算法

链表——insert_at_beginning

单链表与一个新节点的表示,

 def insert_at_beginning(self, data):
    new_node = Node(data)

if self.head:
Python 中的数据结构与算法

链表——insert_at_beginning

单链表,已连接新节点的表示。

 def insert_at_beginning(self, data):
    new_node = Node(data)
    if self.head:
      new_node.next = self.head

Python 中的数据结构与算法

链表——insert_at_beginning

单链表,已连接新节点,且该节点标有"HEAD"。

 def insert_at_beginning(self, data):
    new_node = Node(data)
    if self.head:
      new_node.next = self.head
      self.head = new_node

Python 中的数据结构与算法

链表——insert_at_beginning

空链表中的新节点表示。

 def insert_at_beginning(self, data):
    new_node = Node(data)
    if self.head:
      new_node.next = self.head
      self.head = new_node
    else:

self.tail = new_node self.head = new_node
Python 中的数据结构与算法

链表——insert_at_end

  def insert_at_end(self, data):
    new_node = Node(data)
    if self.head:  

self.tail.next = new_node
self.tail = new_node
else:
self.head = new_node self.tail = new_node
Python 中的数据结构与算法

链表——search

def search(self, data):

current_node = self.head
while current_node:
if current_node.data == data:
return True

单链表的表示,"current_node"指向第一个节点。

Python 中的数据结构与算法

链表——search

def search(self, data):
  current_node = self.head
  while current_node:
    if current_node.data == data:
      return True
    else:
      current_node = current_node.next

单链表的表示,"current_node"指向第二个节点。

Python 中的数据结构与算法

链表——search

def search(self, data):
  current_node = self.head
  while current_node:
    if current_node.data == data:
      return True
    else:
      current_node = current_node.next

return False

单链表的表示,"current_node"指向第三个节点。

Python 中的数据结构与算法

链表——示例

空链表中新节点的表示。

sushi_preparation = LinkedList()

sushi_preparation.insert_at_end("prepare")
Python 中的数据结构与算法

链表——示例

在链表末尾添加新节点的表示。

sushi_preparation = LinkedList()  
sushi_preparation.insert_at_end("prepare")
sushi_preparation.insert_at_end("roll") 

Python 中的数据结构与算法

链表——示例

在链表开头添加新节点的表示。

sushi_preparation = LinkedList()  
sushi_preparation.insert_at_end("prepare")
sushi_preparation.insert_at_end("roll") 
sushi_preparation.insert_at_beginning("assemble")
Python 中的数据结构与算法

链表——示例

链表的表示。

sushi_preparation.search("roll")
True
sushi_preparation.search("mixing")
False
Python 中的数据结构与算法

Ayo berlatih!

Python 中的数据结构与算法

Preparing Video For Download...