Linked Lists in Python 2.5
March 8th, 2007I’ve not used a linked list since my Pascal days, and have oft-wondered how one would create such a thing in Python. Well, I’ve found my answer in looking over the ctypes docs today.
You would define the class first, like so:
from ctypes import *
class LinkedItem(Structure):
pass
LinkedItem._fields_ = [("name", c_char_p),
("next", POINTER(LinkedItem))]
From there, you now have a class that can create a linked list. It uses ctypes, so typing is a bit more restrictive than normal python, but that can be worked around with wrapper classes, etc.