链表实现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: ...
随机推荐
- Web 安全之 XSS 攻击与防御
前言 黑客,相信大家对这一名词并不陌生,黑客们往往会利用 Web 应用程序的漏洞来攻击咱们的系统.开放式 Web 应用程序安全项目(OWASP, Open Web Application Securi ...
- maya_help()验证编程过程中模块导入的情况
import rigLib reload(rigLib.base.control)spine = rigLib.base.control.Control( prefix = 'spine1') hel ...
- 跳台阶(JAVA)
跳台阶 题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法. 思路:典型的动态规划问题,动态规划问题最关键的是把事件中的各种情形抽象为状态,然后找到前后 ...
- sqlserver数据库知识点总结(转)
- Centos 7 修改日期和时间的命令
timedatectl set-ntp no //关闭时间动态更新timedatectl set-time "YYYY-MM-DD HH:MM:SS" //设置时间和日期timed ...
- C 标准库头文件
头文件 说明 头文件 说明 <assert.h> 条件编译宏,将参数与零比较 <complex.h> (C99 起) 复数运算 <ctype.h> 用来确定包含于字 ...
- NetCore部署到Linux服务器+Supervisor的步骤及过程中踩过的坑
本文作备忘使用 服务器配置: 下面是所有操作的具体步骤: 1.安装nginx 参考 1.1 添加源:默认情况Centos7中没有Nginx源,最近Nginx官网提供了Centos的源地址. sud ...
- FPGA Asynchronous FIFO设计思路
FPGA Asynchronous FIFO设计思路 将一个多位宽,且在不停变化的数据从一个时钟域传递到另一个时钟域是比较困难的. 同步FIFO的指针比较好确定,当FIFO counter达到上限值时 ...
- 二、易语言 api 相关
1. 取窗口句柄 对应的api: FindWindow (寻找顶级窗口) 2.取窗口矩形(位置) 对应的api: GetWindowRect(取窗口矩形) 3.取窗口标题 对应的api: Get ...
- 从已删除邮箱copy数据到活动邮箱
Start Windows PowerShell Start > search for "PowerShell" > Start Windows PowerShell ...