I'm a pretty new programmer to python and am having trouble iterating over a linked list. This is a linked list that is given to me as the output of some other software I am using (which I do not have the ability to modify), and contains parameters I need to access (just called my_parameter here for reference). For reference here, I have named the linked list sim_table. The snippet of code I am using to attempt to iterate is:
sim_table_rows = []
def iterate_linked_list(node):
while node is not None:
sim_table_rows.append(node.my_parameter)
node = node.next
iterate_linked_list(sim_table)
This works fine in ipython, which is where I test everything, but when I try to run the script outside of ipython I keep getting a segmentation fault: 11. To diagnose the problem, I tried printing the output instead of appending it:
def iterate_linked_list(node):
while node is not None:
print node.my_parameter
node = node.next
iterate_linked_list(sim_table)
The output I get is an infinite loop of the my_parameter from the last node in the list, but I'm not sure why. I also made a test sim_table that has only two nodes to see what happens if I try to iterate to a non-existent node in ipython:
In [10]: test_sim_table.next.next.my_parameter
AttributeError: 'NoneType' object has no attribute 'my_parameter'
So I get an attribute error instead of None, which is what I was expecting. Am I missing something simple? I am pretty new to all of this, so probably. Thanks for any and all help!
None, then you're going into an infinite loop of appending the first element... No where do you advance through the linked listMemoryErrorhere rather than a segfault. But if the linked list you're being handed is from a C extension module, it's pretty common for such modules to have bugs that make them crash in out-of-memory cases.