A Little Bit of Everything about (Python) Data Structures

Data Structures

Linear

List

Python中list的实现[http://www.laurentluce.com/posts/python-list-implementation/] 和Java中是类似的.

Linked List

1
2
3
4
5
class ListNode:
def __init__(self, val):
self.val = val
self.next = None
# self.prev = None ## double linked list

Stack

Stack is a Last In First Out (LIFO) linear data structure.

1
2
3
4
stack = []
stack.append(1)
stack.append(2)
stack.pop()

Ref: https://www.cs.cmu.edu/~adamchik/15-121/lectures/Stacks%20and%20Queues/Stacks%20and%20Queues.html

Queue

Queue is a First In First Out (FIFO) linear data structure.

1
2
3
4
5
from collections import deque
queue = deque()
queue.append(1)
queue.append(2)
queue.popleft()

Heap (Priority Queue)

Heapq module provides an implementation of the heap queue algorithm, also known as the priority queue algorithm.

Heaps are binary trees for which every parent node has a value less than or equal to any of its children.

1
import heapq

Set

Dict

Tree

Graph

Reference

https://docs.python.org/3/library/heapq.html