链表实现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: ...
随机推荐
- AutoCompleteExtender 使用示例
绑定 KeyValuePair,网上很多例子,没有找到绑定键值对的,msdn上有例子,备忘一下. using FirstElite.Verify.Entity; using System; using ...
- NFC 大电池 高性价比手机
NFC 大电池 高性价比手机三星 Galaxy A60元气版 黑瞳全视屏 3200万超广角拍照手机 骁龙675 6GB+64GB 丹宁黑 全网通4G 双卡双待 1499 https://item.jd ...
- 运维wiki
意识 1.责任心 要有 owner 意识.运维是线上产品的首要负责人,出现故障都默认是运维的故障,要推动改进. 2.细心 要有敏感的风险意识,稳定和安全是运维的最高责任 3.上进心 要善于学习,不断反 ...
- mac mongodb安装
1.前往官网下载.tgz文件 2.解压 tar zxf mongo压缩文件 3.配置环境变量:MAVEN_HOME & bin路径 4.创建data & log文件夹 5.执行安装命令 ...
- nginx+uwsgi配置
nginx #user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; ...
- HTML如何实现斜体字
HTML实现斜体字的标签为<i>标签,用来实现字体倾斜,写法如下: 字体斜体:<i>内容</i> 案例:正常 斜体 当文字加入i标签以后字体就会成为斜体
- docker 恶意镜像到容器逃逸影响本机
转载:http://521.li/post/122.html SUSE Linux GmbH高级软件工程师Aleksa Sarai公布了影响Docker, containerd, Podman, CR ...
- 我的es6笔记
变量 1. let 和 const 声明的变量不在window上了 2. es6中对于块级作用域里的函数声明实现不统一,要避免在大括号里声明函数,尽量用函数表达式来替代. 3. let和const声明 ...
- Python解释器的安装
Python解释器的安装 作者:Eric 微信:loveoracle11g 下载Python Python-3.7.0(64-bit)下载链接地址: https://www.python.org/ft ...
- 2018/12/20 20:52:42 螺纹钢PTA豆粕
如期向上,但是一点办法没有:没有好的入场位,不做不算错,面对诱惑不动如山也是一种修养,今晚看M5有没有3买,有的话可以看情况考虑要不要进场 PTA M30向下一笔过程中,等待M30当前一笔下跌结束,可 ...