- 对于这个任务,我们应该使用教授提供的代码创建push_back,pop_back和pop_front方法。python中单向链表上的Push和Pop方法
我不断收到一个错误 “Asse田:在0x106ec7dd0 main.LinkedList对象>> = 1”
我的猜测是,在弹出的回报,推动方法参考返回的值链表,而不是这些节点中保存的实际值。我完全难倒了。我问周围,每个人都说同样的事情。你的参考文献搞砸了,但我无法弄清楚具体是什么错误以及如何去解决它。
任何帮助非常感谢!
此外,如果任何人想推荐论坛的初学者在这样的问题上,这将不胜感激。我已经发布在stackoverflow,但我打开任何其他建议。
这里是源代码。
'''------------------------------------------- -------------------'''
'''----------- This Block Provided by Instructor ----- ------ '' '
LinkedList类(对象):
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
'' '开始测试 '''
类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)
类TestEmpty(unittest.TestCase生成):
def test(self):
self.assertTrue(LinkedList().empty())
类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())
类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())
类TestPushBackPopBack(单元测试。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())
'''------------------------------------- ------------------------- ''”
哦!傻我。现在我越来越 “Asse田:1 = 3,2,1]” 而且我不知道什么是错用我的代码的其余部分。 我仍然得到 “AttributeError:'NoneType'对象没有属性'next_node'” 对我而言,这表明在引用列表时仍然存在问题。 –