以下代码由python3实现,欢迎大家来讨论
代码语言:javascript复制import random as rd
class Linklist(object):
def __init__(self,data,next=None):
self.data=data
self.next=next
def createListHead(n):
L=Linklist(0) ##链表头
list=[]
for i in range(n):
num=rd.randint(0,100)
list.append(num)
p=Linklist(num,L.next)
L.next=p
L.data =1 ##链表长度加1
print("rawlist===",list)
return L
def createListTail(n):
L=Linklist(0) ##链表头
list = []
num = rd.randint(0, 100)
list.append(num)
head=Linklist(num) ##建立实际数据表头
L.data =1 ##链表长度加1
L.next=head
temp=head ##建立当前数据指针
for i in range(n-1):
num = rd.randint(0, 100)
list.append(num)
p=Linklist(num)
temp.next=p ##当前数据的指针指向新数据
temp=p ##移动当前数据指针
L.data =1 ##链表长度加1
temp.next=None
print('raw data',list)
return L
if __name__=='__main__':
head=createListTail(10)
realData=head.next
list = []
while realData!=None:
list.append(realData.data)
realData=realData.next
print('linklist===',list)
print('linklist len====',head.data)