Hello Python forums,
I started working with Python recently and picked up a book. The book stated discussing using the ctypes module in python which is slick because it's exactly what I need to be learning.
I started trying to test my knowledge by implementing a simple, singly linked list in python using ctypes
from ctypes import * class linknode(Structure): pass linknode._fields_ = [ ("nextNode", POINTER(linknode)), ("intData", c_int), ]
At this point things work, I can create and use linknodes.
next to create a python linked list to use the ctypes linknode
class linked_list(): """ our own linked list based on ctypes """ head_node = None def add(self, int_data): node_to_add = linknode() node_to_add.intData = int_data node_to_add.nextNode = None if (self.head_node == None): self.head_node = node_to_add return else: traverse_node = self.head_node #while(not(traverse_node.nextNode == None)): print traverse_node.nextNode if __name__ == "__main__": ll = linked_list() ll.add(5) ll.add(6)
Adding 5 works just fine, but adding 6 shows my problem...
I remarked out the code where my error was and dropped in a print to illustrate the problem. I think this is a knowledge gap for me in python. I seem to never be able to evaluate the pointer as null, printing is is <__main__.LP_linknode object at 0x01A07F30>. That doesn't sound like a link to 0x00. Do I need to try and store a (char *) 0 instead of None? What am I doing wrong here?
Thanks for any input.