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

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,
Java开发2020年最新常见面试题整理【Spring源码分析】42-@Conditional详解element-ui踩坑记录神仙面试宝典你有了吗?半月看完25大专题,居然斩获阿里P8offer使用css样式设计一个简单的html登陆界面XCTF攻防世界web新手练习_ 9_command_executionJS中的解构赋值的详解与具体用途Python Web 框架elementui 穿梭框 el-transfer 展示列表内容文字过长ConcurrentHashMap实现原理及源码解析