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

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,
html+css个人简历/网页界面【超详细】Java实现学生信息管理系统java 数组下标 变量_Java基础语法:数组实验四 自动化测试工具-软件测试C++之string的compare用法2022蓝桥杯JavaB组省赛试题docker镜像存储在哪里opencv-python傅里叶变换以及逆变换C语言——qsort函数计算机一级多分,多少分能过一级计算机考试