python 一些基本数据结构
来源:互联网 发布:高新区行知小学 重点 编辑:程序博客网 时间:2024/05/16 07:55
一.数组:
1.顺序存储
2.长度不可变化
3.可以通过索引下标找到相关元素
4.不适合插入操作
注:python的列表像个动态的数组,长度不固定(可以使用插入等方法)
简易数组实现:
class Array(object): def __init__(self,data): self.__data = data def __str__(self): return str(self.__data) def __getitem__(self, item): return self.__data[item] def length(self): return self.__data.__len__() def __setitem__(self, key, value): self.__data[key] = valueif __name__ == '__main__': a = Array([2,3,4]) print(a[2]) print(a[::-1]) print(a.length()) a[2] = 99 print(a)
二.链表:
1.不按顺序存储,可以不连续
2.长度可以变化
3.无法通过索引找到相关元素
4.适合做插入操作
注:不像数组,插入时要动用别的元素的位置
单链表的优点是插入和删除,缺点是查询
#定义一个基于节点类的单链表对象类class LNode: def __init__(self,elem,_next=None): self.elem=elem self.next=_nextclass Myexception(Exception): def __init__(self,msg): self.msg = msg def __str__(self): return self.msgclass LList: def __init__(self): self._head=None def is_empty(self): return self._head is None def prepend(self,elem): self._head=LNode(elem,self._head) def pop(self): if self._head is None: raise Myexception('no head') e, self._head = self._head.elem,self._head.next return e def append(self,elem): if self._head is None: self._head=LNode(elem) return p=self._head while p.next is not None: p=p.next p.next=LNode(elem) def pop_last(self): if self._head is None: raise ('in pop_last') p=self._head if p.next is None: e=p.elem self._head=None return e while p.next.next is not None: p=p.next e=p.next.elem p.next=None return p def find(self,pred): p=self._head while p is not None: if pred(p.elem): return p.elem p=p.next def __str__(self): p=self._head while p is not None: print(p.elem) print('end') p=p.next
if __name__ =='__main__': mlist1=LList() print(mlist1.is_empty()) for i in range(11,20): mlist1.append(i) mlist1.pop() print(mlist1)
阅读全文
0 0
- python 一些基本数据结构
- python基本数据结构
- Python基本数据结构:列表
- 1、Python 基本数据结构
- Python基本数据结构总结
- 一些数据结构的基本准备
- Python数据类型 -- 基本数据结构习题
- python基础之基本数据结构
- Python——基本数据结构
- 7.python数据结构-基本数据类型
- Python基本的数据结构-序列
- python数据结构与算法 1 基本数据结构
- Python Thread一些基本函数
- Linux内核中的一些基本数据结构操作
- 数据结构之树的一些基本操作
- 基本数据结构的python实现---堆栈
- 基本数据结构的python实现---队列
- 基本数据结构(算法导论)与python
- 电感的作用
- JAXB的@XmlAccessorType和@XmlTransient
- C++中string类型 任性!
- IMX6q的mipi-csi摄像头
- 30个php操作redis常用方法代码例子
- python 一些基本数据结构
- codeforces Educational Codeforces Round 21 A
- enum usage
- OpenCv 图像的放大和缩小
- 多视图几何之SVD
- 【php基础班】第10天 函数、return详解、函数调用、递归函数、数组
- vue 单页项目教程--微信公众号(微信支付遇到的坑)
- 一个java高级工程师的进阶之路【转】
- servert 中如何调用spring管理的bean方法