Python中的用户定义数据结构
在计算机科学中,数据结构是一种在计算机内存中组织数据以便有效使用的逻辑方式。数据结构允许以结构化的方式添加、删除、存储和维护数据。 Python支持两种类型的数据结构:
- 非原始数据类型: Python将列表、集合和字典作为其非原始数据类型,也可以将其视为其内置数据结构。
- 用户定义的数据结构: Python不支持但可以使用Python支持的概念编程以反映相同功能的数据结构是用户定义的数据结构。有许多数据结构可以通过这种方式实现:
- 链表
- 堆
- 队列
- 树
- 图形
- 哈希图
链表
链表是一种线性数据结构,其中元素不存储在连续的内存位置。链表中的元素使用指针链接,如下图所示:
程序:
Python3
llist = ['first', 'second', 'third']
print(llist)
print()
# adding elements
llist.append('fourth')
llist.append('fifth')
llist.insert(3, 'sixth')
print(llist)
print()
llist.remove('second')
print(llist)
print()
Python3
stack = ['first', 'second', 'third']
print(stack)
print()
# pushing elements
stack.append('fourth')
stack.append('fifth')
print(stack)
print()
# printing top
n = len(stack)
print(stack[n-1])
print()
# poping element
stack.pop()
print(stack)
Python3
queue = ['first', 'second', 'third']
print(queue)
print()
# pushing elements
queue.append('fourth')
queue.append('fifth')
print(queue)
print()
# printing head
print(queue[0])
# printing tail
n = len(queue)
print(queue[n-1])
print()
# poping element
queue.remove(queue[0])
print(queue)
Python3
class node:
def __init__(self, ele):
self.ele = ele
self.left = None
self.right = None
def preorder(self):
if self:
print(self.ele)
preorder(self.left)
preorder(self.right)
n = node('first')
n.left = node('second')
n.right = node('third')
preorder(n)
Python3
class adjnode:
def __init__(self, val):
self.val = val
self.next = None
class graph:
def __init__(self, vertices):
self.v = vertices
self.ele = [None]*self.v
def edge(self, src, dest):
node = adjnode(dest)
node.next = self.ele[src]
self.ele[src] = node
node = adjnode(src)
node.next = self.ele[dest]
self.ele[dest] = node
def __repr__(self):
for i in range(self.v):
print("Adjacency list of vertex {}\n head".format(i), end="")
temp = self.ele[i]
while temp:
print(" -> {}".format(temp.val), end="")
temp = temp.next
g = graph(4)
g.edge(0, 2)
g.edge(1, 3)
g.edge(3, 2)
g.edge(0, 3)
g.__repr__()
Python3
def printdict(d):
for key in d:
print(key, "->", d[key])
hm = {0: 'first', 1: 'second', 2: 'third'}
printdict(hm)
print()
hm[3] = 'fourth'
printdict(hm)
print()
hm.popitem()
printdict(hm)
输出:
[‘first’, ‘second’, ‘third’]
[‘first’, ‘second’, ‘third’, ‘sixth’, ‘fourth’, ‘fifth’]
[‘first’, ‘third’, ‘sixth’, ‘fourth’, ‘fifth’]
堆
堆栈是一种线性结构,允许从同一端插入和删除数据,因此遵循后进先出 (LIFO) 系统。插入和删除分别称为 push() 和 pop()。
程序:
蟒蛇3
stack = ['first', 'second', 'third']
print(stack)
print()
# pushing elements
stack.append('fourth')
stack.append('fifth')
print(stack)
print()
# printing top
n = len(stack)
print(stack[n-1])
print()
# poping element
stack.pop()
print(stack)
输出:
[‘first’, ‘second’, ‘third’]
[‘first’, ‘second’, ‘third’, ‘fourth’, ‘fifth’]
fifth
[‘first’, ‘second’, ‘third’, ‘fourth’]
队列
队列是一种线性结构,允许从一端插入元素并从另一端删除元素。因此,它遵循先入先出(FIFO)方法。允许删除的一端称为队列前端,另一端称为队列后端。
程序:
蟒蛇3
queue = ['first', 'second', 'third']
print(queue)
print()
# pushing elements
queue.append('fourth')
queue.append('fifth')
print(queue)
print()
# printing head
print(queue[0])
# printing tail
n = len(queue)
print(queue[n-1])
print()
# poping element
queue.remove(queue[0])
print(queue)
输出:
[‘first’, ‘second’, ‘third’]
[‘first’, ‘second’, ‘third’, ‘fourth’, ‘fifth’]
first
fifth
[‘second’, ‘third’, ‘fourth’, ‘fifth’]
树
树是一种非线性但分层的数据结构。最顶层的元素被称为树的根,因为人们认为树是从根开始的。树末端的元素称为叶子。树适用于存储相互之间不是线性连接但形成层次结构的数据。
程序:
蟒蛇3
class node:
def __init__(self, ele):
self.ele = ele
self.left = None
self.right = None
def preorder(self):
if self:
print(self.ele)
preorder(self.left)
preorder(self.right)
n = node('first')
n.left = node('second')
n.right = node('third')
preorder(n)
输出:
first
second
third
图形
图是由节点和边组成的非线性数据结构。节点有时也称为顶点,边是连接图中任意两个节点的线或弧。图由一组有限的顶点(或节点)和一组连接一对节点的边组成。
程序:
蟒蛇3
class adjnode:
def __init__(self, val):
self.val = val
self.next = None
class graph:
def __init__(self, vertices):
self.v = vertices
self.ele = [None]*self.v
def edge(self, src, dest):
node = adjnode(dest)
node.next = self.ele[src]
self.ele[src] = node
node = adjnode(src)
node.next = self.ele[dest]
self.ele[dest] = node
def __repr__(self):
for i in range(self.v):
print("Adjacency list of vertex {}\n head".format(i), end="")
temp = self.ele[i]
while temp:
print(" -> {}".format(temp.val), end="")
temp = temp.next
g = graph(4)
g.edge(0, 2)
g.edge(1, 3)
g.edge(3, 2)
g.edge(0, 3)
g.__repr__()
输出:
Adjacency list of vertex 0
head -> 3 -> 2
Adjacency list of vertex 1
head -> 3
Adjacency list of vertex 2
head -> 3 -> 0
Adjacency list of vertex 3
head -> 0 -> 2 -> 1
哈希图
哈希映射是索引数据结构。散列映射使用散列函数来计算带有键的索引到桶或槽数组中。它的值被映射到具有相应索引的桶。密钥是唯一且不可变的。在Python中,字典是哈希映射的示例。
程序:
蟒蛇3
def printdict(d):
for key in d:
print(key, "->", d[key])
hm = {0: 'first', 1: 'second', 2: 'third'}
printdict(hm)
print()
hm[3] = 'fourth'
printdict(hm)
print()
hm.popitem()
printdict(hm)
输出:
0 -> first
1 -> second
2 -> third
0 -> first
1 -> second
2 -> third
3 -> fourth
0 -> first
1 -> second
2 -> third