Python DataStructure
- 一、数组(array)
- 二、双向链表(linkedlist)
- 三、栈(stack)
- 四、队列(queue)
- 五、Python中的可变与不可变类型
一、数组(array)
class array:def __init__(self, capacity: int):self._data = []self._capacity = capacitydef __getitem__(self, position: int) -> object:return self._data[position]def __setitem__(self, index: int, value: object):self._data[index] = valuedef __len__(self):return len(self._data)def __iter__(self):for item in self._data:yield itemdef find(self,index: int):try:return self._data[index]except IndexError:return Nonedef delete(self,index: int):try:self._data.pop(index)return Trueexcept IndexError:return Nonedef insert(self,index: int,value: int):if len(self._data) >= self._capacity:return Falseelse:return self._data.insert(index,value)def printall(self):for item in self:print(item)if __name__ == "__main__":testarray = array(20)for i in range(10):testarray.insert(i,i)print(len(testarray))for i in range(3):testarray.delete(i)print(len(testarray))testarray.printall()
二、双向链表(linkedlist)
from typing import Optionalclass Node:def __init__(self,data: object,left = None,right = None):self._data = dataself._left = leftself._right = rightclass LinkedList:size = 0def __init__(self):self._head: Node = Noneself._tail: Node = Nonedef insert(self,value:object):newnode = Node(value)if not self._head:self._head = newnodeif self._tail:self._tail._right = newnodeself._tail = newnodeself.size += 1def get(self,index: int):try:current = self._headfor i in range(index):current = current._rightreturn current._dataexcept IndexError:return Nonedef remove(self,index: int):try:current = self._headfor i in range(index):current = current._rightif current._right is not None:current._left._right = current._rightcurrent._right._left = current._leftself.size -= 1except IndexError:Nonedef printall(self):current = self._headvalues = []while current:values.append(current._data)current = current._rightfor value in values:print(value)if __name__ == "__main__":testlist = LinkedList()testlist.insert(1)testlist.insert(5)testlist.insert(6)testlist.printall()testlist.remove(2)testlist.printall()
三、栈(stack)
from typing import Optional"""栈:先进后出,后进先出
"""
class Node:def __init__(self,data: int,next = None):self._data = dataself._next = nextclass stack:def __init__(self):self._top:Node = Nonedef push(self,value:int):newnode = Node(value)newnode._next = self._topself._top = newnodedef pop(self) -> Optional[int]:if self._top:value = self._top._dataself._top = self._top._nextreturn valuedef __repr__(self) -> str:current = self._topnums = []while current:nums.append(current._data)current = current._nextreturn " ".join(f"{num}" for num in nums)if __name__ == "__main__":teststack = stack()for i in range(9):teststack.push(i)print(teststack)for _ in range(3):teststack.pop()print(teststack)
四、队列(queue)
"""队列:先进先出,后进后出
"""
from typing import Optionalclass Node:def __init__(self, data: str, next =None):self._data = dataself._next = nextclass queue:def __init__(self):self._head: Node = Noneself._tail: Node = Nonedef enqueue(self,value: str):newnode = Node(value)if self._tail:self._tail._next = newnodeelse:self._head = newnodeself._tail = newnodedef dequeue(self) -> Optional[str]:if self._head:value = self._headself._head = self._head._nextif not self._head:self._tail = Nonereturn valuedef __repr__(self):values = []current = self._headwhile current:values.append(current._data)current = current._nextreturn "->".join(value for value in values)if __name__ == "__main__":testqueue = queue()for i in range(10):testqueue.enqueue(str(i))print(testqueue)for i in range(3):testqueue.dequeue()print(testqueue)
五、Python中的可变与不可变类型
所谓可变类型与不可变类型是指:数据能够直接进行修改,如果能直接修改那么就是可变,否则就是不可变
可变类型
- 列表
- 字典
- 集合
不可变类型
- 整型
- 浮点型
- 字符串
- 元组