You need to change FrequencyList by Frequency, but you also have to add it correctly. This example could work:
class FrequencyList(object):
"""Stores a collection of Frequency objects as a linked list."""
def __init__(self):
""" Creates an empty FrequencyList """
self.head = None
self.last = None
def add(self, letter, frequency=1):
temp = Frequency(letter, frequency)
if self.head is None:
self.head = temp
self.last = temp
else:
self.last.next_node = temp
self.last = temp
def __repr__(self):
if self.head is not None:
next_freq = self.head
data = "Frequency List -> "
while next_freq is not None:
data += repr(next_freq) + " -> "
next_freq = next_freq.next_node
data += "None"
return data
else:
return "Empty List"
class Frequency(object):
"""
Stores a letter:frequency pair.
"""
def __init__(self, letter, frequency):
self.letter = letter
self.frequency = frequency
# The next Frequency object when stored as part of a linked list
self.next_node = None
def __repr__(self):
return 'Frequency({}, {})'.format(repr(self.letter), repr(self.frequency))
test = FrequencyList()
test.add('a', 3)
print(test)
test.add('b', 2)
print(test)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…