- 对于这项任务,我们应该使用教授提供的代码创建push_back,pop_back和pop_front方法。
我一直收到错误 “AssertionError:main.LinkedList对象位于0x106ec7dd0>>!= 1”
我的猜测是pop和push方法的返回返回对链表中的值的引用,而不是这些节点中保存的实际值。我完全难过了。我问过周围,每个人都说同样的话。你的引用搞砸了,但是我无法弄明白什么是错的以及如何修复它。
非常感谢任何帮助!!!!
此外,如果有人想在这样的问题上推荐初学者论坛,那也将非常感激。我确实已经发布了stackoverflow,但我对任何其他建议持开放态度。
这是源代码。
'''-------------------------------------------- ------------------ '''
'''-----------教练提供的这个区块-----------'''
class LinkedList(object):
class Node(object):
# pylint: disable=too-few-public-methods
''' no need for get or set, we only access the values inside the
LinkedList class. and really, never have setters. '''
def __init__(self, value, next_node):
self.value = value
self.next_node = next_node
def __init__(self, initial=None):
self.front = self.back = self.current = None
self.next_node = self.current
def empty(self):
return self.front == self.back == None
def __iter__(self):
self.current = self.front
return self
def __next__(self):
if self.current:
tmp = self.current.value
self.current = self.current.next_node
return tmp
else:
raise StopIteration()
def push_front(self, value):
new = self.Node(value, self.front)
if self.empty():
self.front = self.back = new
if self.empty() is not None:
self.front = new
''^^^^^^^这个座位由讲师提供^^^^^^^'''
''' I need help with following three methods'''
def pop_front(self):
if self.empty():
return None
tmp = self.front.value
self.front = self.front.next_node
if not self.front:
self.back = None
return tmp
def push_back(self, value):
new = self.Node(value, self.back)
if self.empty():
self.back = self.front = new
if self.empty() is not None:
if self.back.next_node is None:
self.current = self.back
self.back.next_node = new
def pop_back(self):
if self.empty():
return None
tmp = self.back.value
if not self.front.next_node:
self.front = self.back = None
else:
while self.front.next_node is not self.back:
self.front = self.next_node
self.front.next_node = None
self.back = self.front
return tmp
'''开始测试'''
class TestPrintMethods(unittest.TestCase):
def test(self):
linked_list = LinkedList()
linked_list.push_front(1)
linked_list.push_front(2)
linked_list.push_front(3)
linked_list.pop_front()
print(linked_list.front.value)
print(linked_list.back.value)
print(linked_list)
class TestEmpty(unittest.TestCase):
def test(self):
self.assertTrue(LinkedList().empty())
class TestPushFrontPopBack(unittest.TestCase):
def test(self):
linked_list = LinkedList()
linked_list.push_front(1)
linked_list.push_front(2)
linked_list.push_front(3)
self.assertFalse(linked_list.empty())
self.assertEqual(linked_list.pop_back(), 1)
self.assertEqual(linked_list.pop_back(), 2)
self.assertEqual(linked_list.pop_back(), 3)
self.assertTrue(linked_list.empty())
class TestPushFrontPopFront(unittest.TestCase):
def test(self):
linked_list = LinkedList()
linked_list.push_front(1)
linked_list.push_front(2)
linked_list.push_front(3)
self.assertEqual(linked_list.pop_front, 3)
self.assertEqual(linked_list.pop_front, 2)
self.assertEqual(linked_list.pop_front, 1)
self.assertTrue(linked_list.empty())
class TestPushBackPopFront(unittest.TestCase):
def test(self):
linked_list = LinkedList()
linked_list.push_back(1)
linked_list.push_back(2)
linked_list.push_back(3)
self.assertFalse(linked_list.empty())
self.assertEqual(linked_list.pop_front, 1)
self.assertEqual(linked_list.pop_front, 2)
self.assertEqual(linked_list.pop_front, 3)
self.assertTrue(linked_list.empty())
class TestPushBackPopBack(unittest.TestCase):
def test(self):
linked_list = LinkedList()
linked_list.push_back(1)
linked_list.push_back("foo")
linked_list.push_back([3, 2, 1])
print(linked_list)
self.assertFalse(linked_list.empty())
self.assertEqual(linked_list.pop_back(), [3, 2, 1])
self.assertEqual(linked_list.pop_back(), "foo")
self.assertEqual(linked_list.pop_back(), 1)
self.assertTrue(linked_list.empty())
'''-------------------------------------------- ------------------ '''
答案 0 :(得分:1)
您忘记调用这些方法
self.assertEqual(linked_list.pop_front, 1)
应该是
self.assertEqual(linked_list.pop_front(), 1)