Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中单链表上的Push和Pop方法_Python_Singly Linked List - Fatal编程技术网

python中单链表上的Push和Pop方法

python中单链表上的Push和Pop方法,python,singly-linked-list,Python,Singly Linked List,--对于本作业,我们应该使用教授提供的代码创建push_-back、pop_-back和pop_-front方法 我总是出错 “AssertionError:main.LinkedList对象位于0x106ec7dd0>>!=1” 我猜pop和push方法中的返回返回的是对链表中的值的引用,而不是这些节点中的实际值。我完全被难住了。我四处打听,每个人都说了同样的话。你的参考资料都是乱七八糟的,但我无法明确指出什么地方出了问题,以及如何着手解决 非常感谢您的帮助 另外,如果有人愿意为初学者推荐关于

--对于本作业,我们应该使用教授提供的代码创建push_-back、pop_-back和pop_-front方法

我总是出错 “AssertionError:main.LinkedList对象位于0x106ec7dd0>>!=1”

我猜pop和push方法中的返回返回的是对链表中的值的引用,而不是这些节点中的实际值。我完全被难住了。我四处打听,每个人都说了同样的话。你的参考资料都是乱七八糟的,但我无法明确指出什么地方出了问题,以及如何着手解决

非常感谢您的帮助

另外,如果有人愿意为初学者推荐关于此类问题的论坛,也将不胜感激。我已经在stackoverflow上发表了文章,但我愿意接受任何其他建议

这是源代码

“”“”----------------------------------------------------------------“”

''----此块由讲师提供-----''

类链接列表(对象):

'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

“开始测试”

类TestPrintMethods(unittest.TestCase):

类TestEmpty(unittest.TestCase):

类TestPushFrontPopBack(unittest.TestCase):

类TestPushFrontPopFront(unittest.TestCase):

类TestPushBackPopFront(unittest.TestCase):

类TestPushBackPopBack(unittest.TestCase):


“”“”-----------------------------------------------------------------------“”“

您忘记调用这些方法了

self.assertEqual(linked_list.pop_front, 1)
应该是

self.assertEqual(linked_list.pop_front(), 1)

哦我真傻。现在我得到了“AssertionError:1!=[3,2,1]”,我不确定我的其余代码有什么问题。我还得到了“AttributeError:'NoneType'对象没有属性'next_node'”,这对我来说意味着仍然引用列表时存在问题。
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)
def test(self):
    self.assertTrue(LinkedList().empty())
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())
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())
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())
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())
self.assertEqual(linked_list.pop_front, 1)
self.assertEqual(linked_list.pop_front(), 1)