#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数据类型的更多相关文章

  1. python 基本数据类型分析

    在python中,一切都是对象!对象由类创建而来,对象所拥有的功能都来自于类.在本节中,我们了解一下python基本数据类型对象具有哪些功能,我们平常是怎么使用的. 对于python,一切事物都是对象 ...

  2. python常用数据类型内置方法介绍

    熟练掌握python常用数据类型内置方法是每个初学者必须具备的内功. 下面介绍了python常用的集中数据类型及其方法,点开源代码,其中对主要方法都进行了中文注释. 一.整型 a = 100 a.xx ...

  3. 闲聊之Python的数据类型 - 零基础入门学习Python005

    闲聊之Python的数据类型 让编程改变世界 Change the world by program Python的数据类型 闲聊之Python的数据类型所谓闲聊,goosip,就是屁大点事可以咱聊上 ...

  4. python自学笔记(二)python基本数据类型之字符串处理

    一.数据类型的组成分3部分:身份.类型.值 身份:id方法来看它的唯一标识符,内存地址靠这个查看 类型:type方法查看 值:数据项 二.常用基本数据类型 int 整型 boolean 布尔型 str ...

  5. Python入门-数据类型

    一.变量 1)变量定义 name = 100(name是变量名 = 号是赋值号100是变量的值) 2)变量赋值 直接赋值 a=1 链式赋值  a=b=c=1 序列解包赋值  a,b,c = 1,2,3 ...

  6. Python基础:八、python基本数据类型

    一.什么是数据类型? 我们人类可以很容易的分清数字与字符的区别,但是计算机并不能,计算机虽然很强大,但从某种角度上来看又很傻,除非你明确告诉它,"1"是数字,"壹&quo ...

  7. python之数据类型详解

    python之数据类型详解 二.列表list  (可以存储多个值)(列表内数字不需要加引号) sort s1=[','!'] # s1.sort() # print(s1) -->['!', ' ...

  8. Python特色数据类型(列表)(上)

    Python从零开始系列连载(9)——Python特色数据类型(列表)(上) 原创 2017-10-07 王大伟 Python爱好者社区 列表 列表,可以是这样的: 分享了一波我的网易云音乐列表 今天 ...

  9. 【Python】-NO.97.Note.2.Python -【Python 基本数据类型】

    1.0.0 Summary Tittle:[Python]-NO.97.Note.2.Python -[Python 基本数据类型] Style:Python Series:Python Since: ...

随机推荐

  1. Java(全局变量-静态变量-位运算符)

    全局变量是默认赋值的:而局部变量是没有默认赋值的(需要赋值才能使用)静态变量只能被静态方法使用 位运算右移,相当于做除法,2的n次幂00001000操作的位移数相当于是偏移量从右向左数n位,从第n+1 ...

  2. zabbix自动发现及其自动注册

    在大企业环境中,不可能在zabbix页面上逐个添加被监控的主机.还好zabbix自带自动发现和自动注册功能 被监控端安装zabbix客户端之后,将配置文件配置指向服务器端ip即可.红色箭头改为zabb ...

  3. day02 运算符

                                  运算符2019-04-01 目录 一.算数运算符 + = * / % // ** 二.比较运算 > < == != >= ...

  4. gitlab 安装和使用

    正常 团队开发 不可能吧代码托管给 github 或者码云之类的 三方托管机构. 然后  原始的 git 没有图形用户界面. 这时候我们可以选择 gitlab . 安装环境 centos7 1 安装依 ...

  5. OpenStack搭建Q版在控制节点上的环境准备(step2)

    接下来是只需要在控制节点上准备的环境配置.其中虽然NTP服务需要在所有节点上都安装,但NTP服务在控制节点和其他的节点上的配置是不同的,所以不把它放在step1的公共配置中进行准备.如下: 1.配置N ...

  6. Spring Boot - AOP(面向切面)

    AOP 全称 Aspect Oriented Programming(面向切面),AOP主要实现的目的是针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某个步骤或阶段,以获得逻辑过程中各部分 ...

  7. MySQL is running but PID file could not be found

    在Linux 中,当你启动或者重启 MySQL 时,报 关于 PID file 的错误 解决方法 第一步:找到   mysql 中 data 目录下的 mysql-bin.index 文件,然后删除 ...

  8. C 标准库头文件

    头文件 说明 头文件 说明 <assert.h> 条件编译宏,将参数与零比较 <complex.h> (C99 起) 复数运算 <ctype.h> 用来确定包含于字 ...

  9. Day11字符串 title

    一.title 二.join------将字符串中的每一个元素按照指定分隔符进行拼接. 三.ljust rjust center    左.右.中间填充 四.lower  islower   uppe ...

  10. Python3 标准库学习

    python3.5.6 官方文档  https://docs.python.org/3.5/library/index.html 1.介绍 2.内置函数 3.内置常量 3.1常数添加的 site模块 ...