感谢您提前阅读问题。
我正在通过Python研究链表数据结构。下面两种方法push(),push1()困扰了我。
类Node(object):
def __init__(self, value):
self.value = value
self.next = None
def __repr__(self):
nval = self.value and self.next.value or None
return f"[{self.value}:{repr(nval)}]"
类LinkedListController(object):
def __init__(self):
self.begin = None
# Appends new value at the end of the list
def push(self, obj):
if not self.begin:
self.begin = Node(obj)
else:
n = self.begin
while n.next :
n = n.next
n.next = Node(obj)
def push1(self, obj):
if not self.begin:
self.begin = Node(obj)
else:
while self.begin.next:
self.begin = self.begin.next
self.begin.next = Node(obj)
# Counts the number of elements in the list
def count(self):
current_node = self.begin
count = 0
while current_node:
count = count + 1
current_node = current_node.next
return count
下面是测试代码:
from unittest import TestCase
from data_structure.single_linked_list_without_endnode import LinkedListController as node_controller
类TestLinkedList(TestCase):
def test_push(self):
colors = node_controller()
colors.push("Pathalon Blue")
self.assertEquals(colors.count(), 1)
colors.push("Ultramarine Blue")
self.assertEquals(colors.count(), 2)
colors.push("Blaring Sun")
self.assertEquals(colors.count(), 3)
animals = node_controller()
animals.push1("Dog")
self.assertEquals(animals.count(), 1)
animals.push1("Cat")
self.assertEquals(animals.count(), 2)
animals.push1("Cow")
self.assertEquals(animals.count(), 3)
The only difference between push() and push1() is reference 'n', other than that I see the same logic but why push1() does not work ?
Traceback (most recent call last): File "C:\Users\Dell\projects\data_structure\tests\test_single_linked_list_without_endnode.py", line 24, in test_push self.assertEquals(animals.count(), 3) AssertionError: 2 != 3
In the first push method,
n.next = Node(obj)
is just assigning to reference data 'n', not toself.begin
. And I thinkself.begin
should be still None since we did not do anything likeself.begin.next = Node(obj)
, but the test is working fine. How..?
非常感谢
In the first push method, it keeps following the linked list starting from your
self.begin
element until it reaches the last element in the linked list, yes it doesn't always directly link yourself.begin
to your new element, but that's how linked list is as a data structure.In the
push1()
method you keep changing the your base element in the linked list by redefiningself.begin
in each iteration, thus your linked list will lose some of its elements, consider the case where you have 3 -- > 5 --> 6 as a linked list and you usepush1()
to add element 8 for instance, what it will do is that in the first iteration it redefinesself.begin
so it becomes 5 and your linked list becomes 5-->6, then in the next iteration it become only the element 6, then it appends your new element so it becomes 6 --> 8, and that's why the error says2!=3
, becausepush1()
will always result in having a linked list of length 2push1()不起作用,因为您实际上正在更改self.begin。 在push()的情况下,您将创建一个新对象,并仅给其对self.begin的引用,然后将该引用更改为另一个对象(下一个),而不更改属性本身,以使self.begin成为第一个元素操作之后(在push1()之后,self.begin不是第一个元素,而是倒数第二个元素)。