链表的结构是项的集合,其中每个项保持相对于其他项的相对位置。

python链表常用的函数 

 一个简单地项
class Node: #链表项,必须包含值,和下一个项两个属性,和对应的set get 方法 def __init__(self,initdata):
self.data = initdata self.next = None def getData(self): return self.data def
getNext(self): return self.next def setData(self,newdata): self.data = newdata
def setNext(self,newnext): self.next = newnext
一个链表
class UnorderedList: def __init__(self): self.head = None def isEmpty(self):
return self.head == None def add(self,item): item1 = Node(item)
item1.setNext(self.head) self.head = item1 def size(self): current = self.head
count = 0 while current != None: count = count + 1 current = current.getNext()
return count def exist(self,item): current = self.head found = False while
current != None and not found : if current.getData() == item: found = True
else: current = current.getNext() return found def show_all(self): current =
self.head show_list = [] while current is not None:
show_list.append(current.getData()) current = current.getNext()
print(show_list) def delete_item(self,item): parent = None current = self.head
found = False while current is not None and not found : if current.getData() ==
item: found = True else: parent = current current = current.getNext() if found:
if parent is None: parent = self.head else: parent.next = current.getNext() udl
= UnorderedList() for i in range(10): udl.add(i) udl.show_all()
print(udl.exist(11)) udl.delete_item(7) udl.show_all()

技术
©2019-2020 Toolsou All rights reserved,
LinkedHashMap基本用法&使用实现简单缓存 dedecms网站被黑 劫持到其他网站如何解决苹果不送充填器耳机真为环保?可能还是为了赚钱吧图片格式转换错误总结-myBatis plus 分页numpy:多维数组的创建用C语言做很简单的飞机游戏Keras保存与加载模型(JSON+HDF5)福布斯中国汽车富豪榜:何小鹏第11 李想第14 李斌第15hive大量小文件处理方法总结