实现代码:
class linklist:
class Node:
def __init__(self,item=None):
self.item=item
self.next=None
class linklistIterator:
def __init__(self,node):
self.node=node
def __next__(self):
if self.node:
cur_node=self.node
self.node=cur_node.next
return cur_node.item
else:
raise StopIteration
def __iter__(self):
return self
def __init(self,iterable=None):
self.head=None
self.tail=None
if iterable:
self.extend(iterable)
def append(self,obj):
s=linklist.Node(obj)
if not self.head:
self.head=s
self.tail=s
else:
self.tail.next=s
self.tail=s
def extend(self,iterable):
for obj in iterable:
self.append(obj)
def find(self,obj):
for n in self:
if n==obj:
return True
else:
return False
def __iter__(self):
return self.linklistIterator(self.head)
def __repr__(self):
return '<<'+','.join(map(str,self))+'>>'
# 类似集合的哈希结构
class HashTable:
def __init__(self,size=101):
self.size=size
self.T=[linklist() for i in range(self.size)]
def h(self,k):
return k%self.size
def append(self,k):
i=self.h(k)
if self.find(k):
print('Duplicated Insert')
else:
self.T[i].append(k)
def find(self,k):
i=self.h(k)
return self.T[i].find(k)
应用: