- 對於這個任務,我們應該使用教授提供的代碼創建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'」 對我而言,這表明在引用列表時仍然存在問題。 –