我试图在Python 3.7中从头开始实现一个链表,经过多次尝试,我似乎无法获得print_values()方法来按期望的顺序(顺序)打印所有节点值。此时,我不确定问题出在append()方法还是print_values()方法。
class Node:
def __init__(self, node_value):
self.node_value = node_value
self.nextNode = None
class SinglyLinkedList:
# methods that should be available: get_size, insert_at, append, remove,
# update_node_value
def __init__(self):
self.head_node = None
self.tail_node = None
self.size = 0
def get_list_size(self):
"""This returns the value for the size variable which get incremented
every time a new node is added.
This implementation is better because it has a running time of O(1)
as opposed to iterating through the
whole list which has a running time of O(n)"""
return self.size
def append(self, value):
new_node = Node(value)
if self.head_node is None:
self.head_node = new_node
self.size += 1
else:
while self.head_node.nextNode is not None:
self.head_node = self.head_node.nextNode
self.head_node.nextNode = new_node
self.size += 1
def print_values(self):
current_node = self.head_node
list_values = []
while current_node.nextNode is not None:
list_values.append(current_node.node_value)
if current_node.nextNode.nextNode is None:
list_values.append(current_node.nextNode.node_value)
current_node = current_node.nextNode
if list_values is not None:
print("Linked list: " + str(list_values))
else:
print("Linked List is currently empty.")
# Helper code below.
new_ll = SinglyLinkedList()
new_ll.append("alpha")
print(new_ll.get_list_size())
new_ll.append("beta")
print(new_ll.get_list_size())
new_ll.append("gamma")
print(new_ll.get_list_size())
new_ll.append("delta")
print(new_ll.get_list_size())
new_ll.append("epsilon")
print(new_ll.get_list_size())
new_ll.append("zeta")
print(new_ll.get_list_size())
new_ll.print_values()
我在输出中得到的就是这样:
1
2
3
4
5
6
Linked list: ['epsilon', 'zeta']
答案 0 :(得分:1)
通常,单链接列表仅跟踪头部。 (也不是尾巴)。因此通常不使用mask
。
使用original_image
或self.tail_node = None
时,使用递归而不是使用循环将使您的工作变得更加轻松。如果您只想遍历列表,则循环可以很好地工作,但是如果您要更改列表,则建议使用递归解决方案。
话虽这么说,问题不关在您的linkedlist
上,而是在您的tree
上。
您可以从不移动头节点。您必须始终创建一个指针,这样才引起问题:
print
修复:
append
递归解决方案:
self.head_node = self.head_node.nextNode
话虽如此,直到我重做您的打印后我才意识到这一点,因此这里提供了一种使用python生成器的更干净的打印方法,可能会对您有所帮助。
Generators 是可以与python一起使用的东西,通常不能与其他编程语言一起使用,它可以使将链接列表转换为值列表真的很容易做到:
def append(self, value):
new_node = Node(value)
if self.head_node is None:
self.head_node = new_node
self.size += 1
else:
temp_head = self.head_node
while temp_head.nextNode is not None:
temp_head = temp_head.nextNode
temp_head.nextNode = new_node
self.size += 1
免责声明: 生成器很好,但是我只是这样做以匹配您的行为(即从链表中获取列表)。如果列表并不重要,但是您只想输出链表中的每个元素,那么我就可以这样做:
def append(self, value):
new_node = Node(value)
self.size += 1
self.head_node = self.__recursive_append(self.head_node, new_node)
def __recursive_append(self, node, new_node):
if not node:
node = new_node
elif not node.nextNode:
node.nextNode = new_node
else:
node.nextNode = self.__recursive_append(node.nextNode, new_node)
return node
答案 1 :(得分:0)
我同意错误-句法Re悔的回答,因为问题出在append和while循环的主体中……这是伪代码中的示例:
append 0:
head = alpha
append 1:
//skip the while loop
head = alpha
next = beta
append 2:
//one time through the while loop because next = beta
head = beta
//beta beta just got assigned to head, and beta doesn't have next yet...
next = gamma
append 3:
//head is beta, next is gamma...the linked list can only store 2 nodes
head = gamma //head gets next from itself
//then has no next
next = delta
...etc.