'''
链表实现
'''
# 节点
class Node:
def __init__(self, init_data):
self.data = init_data
self.next = None
def get_data(self):
return self.data
def get_next(self):
return self.next
def set_data(self, new_data):
self.data = new_data
def set_next(self, next_node):
self.next = next_node
# 链表及操作
class UnorderedList:
def __init__(self):
self.head = None
def is_empty(self):
return self.head == None
def add(self, item):
temp = Node(item)
# 将当前表头中的节点设置为temp的Next
temp.set_next(self.head)
# 将temp设置为当前表头
self.head = temp
def size(self):
current = self.head
count = 0
while current != None:
count = count + 1
current = current.get_next()
return count
def search(self, item):
current = self.head
found = False
while (current != None) and not found:
if current.get_data() == item:
found = True
else:
current = current.get_next()
return found
def remove(self, item):
current = self.head
previous = None
found = False
while not found:
if current.get_data() == item:
found = True
else:
previous = current
current = current.get_next()
if previous == None:
self.head = current.get_next()
else:
previous.set_next(current.get_next())