链表实现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=" ")
原文地址:https://www.cnblogs.com/jia-bk-home/p/10005521.html