链表实现python list数据类型
#1.<--用单链表的数据结构实现列表
class error(Exception):
def __init__(self,msg):
super(error,self).__init__(self)
self.mes=msg
def __str__(self):
return self.mes class Node():
def __init__(self,val=None):
self.val=val
self.next=None
class MyList():
def __init__(self,ele=None):
self._head=ele
#空数组
def empty(self):
return self._head==None
#在头部添加
def add(self,ele):
node=Node(ele)
node.next=self._head
self._head=node
#测数组的长度
def length(self):
num=0
cursor=self._head
while cursor!=None:
cursor = cursor.next
num+=1
return num
#获取指定下标的元素
def get(self,index):
if index<0 or index>self.length()-1:
raise error("Index is outside the range of range")
else:
cursor = self._head
num=0
while num<index+1:
if num==index:
return cursor.val
cursor = cursor.next
num+=1
#在最后添加一个元素
def append(self,ele):
node = Node(ele)
if self.empty():
self._head=node
else:
cursor = self._head
while cursor.next!=None:
cursor = cursor.next
cursor.next=node
#在指定位置插入一个元素
def insert(self,index,ele):
if index<0 or index>self.length():
raise error("Index is outside the range of range")
else:
if index==0:
self.add(ele)
elif index==self.length():
self.append(ele)
else:
node = Node(ele)
cursor = self._head
num=0
while num<index:
if num==index-1:
node.next = cursor.next
cursor.next = node
cursor = cursor.next
num+=1
#弹出最后一个元素
def pop(self):
if self.empty():
return None
else:
if self.length()==1:
item=self._head.val
self._head=None
return item
else:
cursor=self._head
pre=None
while cursor.next!=None:
pre=cursor
cursor=cursor.next
ele=cursor.val
pre.next=None
return ele
#删除第一个匹配内容的元素
def remove(self,num):
if self.empty():
return None
elif self.length()==1:
pre=self._head
self._head=None
return pre.val
else:
cursor=self._head
pre=None
while cursor.val!=num:
pre=cursor
cursor=cursor.next
if pre==None:
pre=self._head
self._head=pre.next
return pre.val
elif cursor.next!=None:
pre.next=cursor.next
return cursor.val
else:
pre.next=None
return cursor.val
#删除指定下标的元素
def delete(self,index):
if self.length()==1:
pre=self._head
self._head=None
return pre.val
else:
cursor=self._head
num=0
pre=None
while num<index and cursor.next!=None:
pre=cursor
cursor=cursor.next
num+=1
if pre==None:
pre = self._head
self._head = pre.next
return pre.val
elif cursor.next!=None:
pre.next=cursor.next
return cursor.val
else:
pre.next=None
return cursor.val
#下两个函数时制作迭代器的
def __iter__(self):
return self
def __next__(self):
num=0
if self._head==None:
raise StopIteration
else:
pre=self._head.val
self._head=self._head.next
return pre list2=MyList()
list2.add("a")
list2.append("b")
list2.insert(1,"h")
list2.insert(1,"11") for item in list2:
print(item,end=" ")
#2.<--用单向循环链表的数据结构实现列表
class error(Exception):
def __init__(self,msg):
super(error,self).__init__()
self.err=msg
def __str__(self):
return self.err
class Node():
def __init__(self,val=None):
self.val=val
self.next=None class MyList():
def __init__(self,ele=None):
self._head=ele
self.count = 0
def empty(self):
return self._head==None
def add(self,ele):
if self.empty():
node = Node(ele)
self._head=node
self._head.next=self._head
else:
node=Node(ele)
cursor=self._head
while cursor.next!=self._head:
cursor=cursor.next
node.next=self._head
self._head=node
cursor.next=self._head
def length(self):
if self.empty():
return 0
else:
cursor=self._head
num=1
while cursor.next!=self._head:
num+=1
cursor=cursor.next
return num
def get(self,index):
if index<0 or index>self.length()-1:
raise error("Index exceeds the length of rang")
else:
cursor = self._head
num=0
while num<index+1:
if num==index:
return cursor.val
cursor=cursor.next
num+=1
def appen(self,ele):
if self.empty():
node=Node(ele)
self._head=next()
self._head.next=self._head
else:
node = Node(ele)
cursor=self._head
while cursor.next!=self._head:
cursor=cursor.next
cursor.next=node
node.next=self._head
def pop(self):
if self.length()==1:
pre=self._head
self._head=None
return pre.val
else:
cursor=self._head
pre=None
while cursor.next!=self._head:
pre=cursor
cursor=cursor.next
pre.next=cursor.next
return cursor.val
def __iter__(self):
return self
def __next__(self):
cursor=self._head
if self.count<self.length():
num=0
while num<self.count:
num+=1
cursor=cursor.next
self.count+=1
return cursor.val
else:
raise StopIteration mlist=MyList()
mlist.add("a")
mlist.add("b")
mlist.add("c")
mlist.appen(1)
mlist.appen("h") #print(mlist.empty())
print(mlist.length())
# print(mlist.get(0),end=" ")
# print(mlist.get(1),end=" ")
# print(mlist.get(2),end=" ")
# print(mlist.get(3),end=" ")
# print(mlist.get(4),end=" ")
for item in mlist:
print(item,end=" ")
#3.<--用双向链表的数据结构实现列表
class error(Exception):
def __init__(self,msg):
super(error,self).__init__()
self.err=msg
def __str__(self):
return self.err
class Node():
def __init__(self,val=None):
self.val=val
self.next=None
self.last=None
class MyList():
def __init__(self,ele=None):
self._head=ele
self.count=0
def empty(self):
return self._head==None
def add(self,ele):
if self.empty():
node=Node(ele)
self._head=node
else:
node = Node(ele)
pre=self._head
self._head=node
node.next=pre
pre.last=self._head
def length(self):
if self.empty():
return 0
else:
cursor=self._head
num=1
while cursor.next!=None:
cursor=cursor.next
num += 1
return num
def get(self,index):
if index<0 or index>self.length()-1:
raise error("Index exceeds the length of rang")
else:
cursor=self._head
num=0
while num<index+1:
if num==index:
return cursor.val
cursor=cursor.next
num+=1
return num
def append(self,ele):
if self.empty():
node=Node(ele)
self._head=node
else:
node = Node(ele)
cursor = self._head
while cursor.next !=None:
cursor=cursor.next
cursor.next=node
node.last=cursor
def pop(self):
if self.empty():
return None
else:
if self.length()==1:
pre=self._head
self._head=None
return pre.val
else:
cursor=self._head
pre=None
while cursor.next!=None:
pre=cursor
cursor=cursor.next
pre.next=None
def __iter__(self):
return self
def __next__(self):
cursor = self._head
if self.count < self.length():
num = 0
while num < self.count:
num += 1
cursor = cursor.next
self.count += 1
return cursor.val
else:
raise StopIteration # if self._head.next==None:
# raise StopIteration
# else:
# val=self._head.val
# self._head=self._head.next
# self._head.last=None
# return val arr=MyList()
arr.add("a")
arr.add("b")
arr.add("c")
arr.append("1")
arr.append("d")
arr.append("f")
arr.pop()
arr.pop()
arr.pop() # print(arr.empty())
# print(arr.length())
# print(arr.get(0),end=" ")
# print(arr.get(1),end=" ")
# print(arr.get(2),end=" ")
# print(arr.get(3),end=" ")
# print(arr.get(4),end=" ")
# print(arr.get(5),end=" ")
for item in arr:
print(item,end=" ")
链表实现python list数据类型的更多相关文章
- python 基本数据类型分析
在python中,一切都是对象!对象由类创建而来,对象所拥有的功能都来自于类.在本节中,我们了解一下python基本数据类型对象具有哪些功能,我们平常是怎么使用的. 对于python,一切事物都是对象 ...
- python常用数据类型内置方法介绍
熟练掌握python常用数据类型内置方法是每个初学者必须具备的内功. 下面介绍了python常用的集中数据类型及其方法,点开源代码,其中对主要方法都进行了中文注释. 一.整型 a = 100 a.xx ...
- 闲聊之Python的数据类型 - 零基础入门学习Python005
闲聊之Python的数据类型 让编程改变世界 Change the world by program Python的数据类型 闲聊之Python的数据类型所谓闲聊,goosip,就是屁大点事可以咱聊上 ...
- python自学笔记(二)python基本数据类型之字符串处理
一.数据类型的组成分3部分:身份.类型.值 身份:id方法来看它的唯一标识符,内存地址靠这个查看 类型:type方法查看 值:数据项 二.常用基本数据类型 int 整型 boolean 布尔型 str ...
- Python入门-数据类型
一.变量 1)变量定义 name = 100(name是变量名 = 号是赋值号100是变量的值) 2)变量赋值 直接赋值 a=1 链式赋值 a=b=c=1 序列解包赋值 a,b,c = 1,2,3 ...
- Python基础:八、python基本数据类型
一.什么是数据类型? 我们人类可以很容易的分清数字与字符的区别,但是计算机并不能,计算机虽然很强大,但从某种角度上来看又很傻,除非你明确告诉它,"1"是数字,"壹&quo ...
- python之数据类型详解
python之数据类型详解 二.列表list (可以存储多个值)(列表内数字不需要加引号) sort s1=[','!'] # s1.sort() # print(s1) -->['!', ' ...
- Python特色数据类型(列表)(上)
Python从零开始系列连载(9)——Python特色数据类型(列表)(上) 原创 2017-10-07 王大伟 Python爱好者社区 列表 列表,可以是这样的: 分享了一波我的网易云音乐列表 今天 ...
- 【Python】-NO.97.Note.2.Python -【Python 基本数据类型】
1.0.0 Summary Tittle:[Python]-NO.97.Note.2.Python -[Python 基本数据类型] Style:Python Series:Python Since: ...
随机推荐
- mysql的事务和数据库锁的关系
数据库加事务并不是数据就安全来了,事务和锁要分析清楚和配合使用 问题背景处于对高并发的秒杀环节的理解整理如下: 秒杀的时候高并发主要注意1.在秒杀的情况下,肯定不能如此高频率的去读写数据库,会严重造成 ...
- linux的基本操作(一)
一.Linux组成 1.1:Linux各模块介绍 内核:是系统的心脏,是运行程序和管理像磁盘和打印机等硬件设备的核心程序. Shell:是系统的用户界面,提供了用户和内核进行交互操作的一种接口.它接收 ...
- tomcat 闪退问题排查
由于启动tomcat回出现闪退情况,看不到异常 解决方法: 一. 打开startup.bat文件,在最下面 在文本的最后敲上pause,保存后重新运行startup.bat,这时候窗口会留在桌面上(调 ...
- 记录一个源码安装mysql5.6的方法
https://www.jb51.net/article/118853.htm 如果之前源码安装过mysql5.6的话,卸载方法如下:rm -rf /var/lib/mysql/rm -rf /usr ...
- Linux中安装nodejs及插件
Linux中安装nodejs及插件 1.去官网下载安装包 英文网址:https://nodejs.org/en/download/ 中文网址:http://nodejs.cn/download/ 通过 ...
- 2018总结-->2019新目标
2018完成的事情: ①考到了驾照: ②刷了很多题,春季找到了实习,赚到了去日本旅游的经费和2019毕业租房的预算,最后签了offer: ③去了西安.天津.山西,看到了不一样的人和事: ④发了小论文, ...
- 【C++】ubuntu中读取指定目录中的所有文件
摘要:ubuntu系统下,C++程序读取指定文件夹中多个文件,保存文件名列表.文件名没有规律且不考虑读取子文件夹中的文件. 系统配置:ubuntu16.04, cmake编译 首先安利一个函数,输入s ...
- 批量删除ppt动画
alt+F11打开宏编辑窗口,输入以下代码,运行即可: Sub removeALL() Dim I As Integer: Dim J As Integer Dim oActivePres As Ob ...
- RTP实时传输协议
RTP协议是包括一对协议:RTP和RTCP. RTP传输数据,RTCP传输控制信息. 一般基于UDP,RTP使用偶数端口,RTCP使用下一个奇数端口. 层次关系: APP -> RTP -> ...
- hive 的map数和reduce如何确定(转)
转自博客:https://blog.csdn.net/u013385925/article/details/78245011(没找到原创者,该博客也是转发) 一. 控制hive任务中的map ...