class Node(): def __init__(self,Data): self.data = Data # 当前节点 self.next = None # 下一节点,为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): temp = Node(item) # 创建一个节点 temp.setNext(self.head) # 将当前的头节点变成新创建的节点的下一个节点:即往后移动一位 self.head = temp # 再将新创建的节点设成头 def size(self): current = self.head count = 0 while current != None: count += 1 current = current.getNext() return count def search(self,item): current = self.head while current != None: if current.getData() == item: return True else: current = current.getNext() return False def remove(self,item): if self.search(item) == True: current = self.head previous = None found = False while not found: if current.getData() == item: found = True else: previous = current current = current.getNext() if previous == None: self.head = current.getNext() else: previous.setNext(current.getNext()) else: print('No item found') li = UnorderedList() li.add(5) li.add(6) li.add(7) print(li.size()) print(li.search(6)) li.remove(6) print(li.search(5))