python中使用class方法实现链表的添、删、查、遍操作,需要新建两个class
第一个class是node,第二个class是linked list
class node:
def __init__(self,data):
self.data = data
self.next = None
链表——打印,搜索,添加,删除算法
class linkedList:
#初始化头结点,一个链表只需要知道头结点,即可
def __init__(self):
self.head=None
#打印链表,从头结点开始遍历
def printList(self):
temp = self.head
while temp:
print(temp.data)
temp = temp.next
#搜索算法,无返回值,可根据需要更改
def search(self,item):
temp = self.head
Found = False
while temp and not Found :
if temp.data == item:
Found = True
else:
temp = temp.next
if Found == True:
print("Found!")
else:
pri