Friday, April 20, 2012

Linked lists in python necessary?!

I know in a previous post suggested a linked list...why would you use a linked list?!

Well, it seems if you needed an ordered set of data in any manner of typical type ordered construction, dictionaries, list, sets, or any manner of typical python would be just as practical.  However, it seems in terms of organisation of data itself, the linked list could have purpose here in python.  Consider a bulk list of custom class objects that needed to be stored in a list ordered fashion here, if you hadn't wanted to use say a dictionary to store such objects using say a key string value or some sort of key code, with linked list structures you could instead define attribute data as a key instead.  Thus instead of a reference to class object

ordereddictionary['key']

to such object, with a linked list you could instead redefine a given key as

orderedlinkedlist.key


Here's a very simple linked list class structure with method for writing to

class Storeobj:
    def __init__(self, a = None, n = None):
        self.val = a
        self.n = n

cr = range(0, 5)
b = ''
collect = []
for x in cr:

    a = Storeobj(a = x)
    if not type(b) == str:
        b.n = a
        collect.append(b)
    b = a
collect.append(a)

x = collect[0]    
while not (x == None):
    print(x.val)
    x = x.n
           
for col in collect:
    print(col.val) 
 
Explaining some aspects of code above, aside from examples of a similar sort provided on this topic implementation in writing to linked lists are another matter.   In the loop,
 
for x in cr:

    a = Storeobj(a = x)
    if not type(b) == str:
        b.n = a
        collect.append(b)
    b = a

one Storeobj is linked to the next in the assignment of Storeobj.n = previous Storeobj(a = x) noting that the previous store obj in the loop were at the assignment call b = a.

. In this case, for the linked list, we hadn't need use a collect list object but we could still use this, illustrating the purpose of python's list container in so far as use as an ordered list likewise here.


Technically in terms of the ordered list it seems on the while loop is slightly less compact, however, given other possibilities depending on the extent of a linked list object's attribute structure could provide some differences here?!

In other programming languages I had imagined where type restrictions on container objects themselves offered difficulties in the re implementation of extensive data structures and class objects themselves when any particular number of distinct instances of these need exist simultaneously, the linked list proves of help here.  In python, however, flexibilities in type storage allows for custom data types to, however, be stored in python's natively defined data container structures.  This means, that the list (like the array in other languages) could easily be constructed as an ordered list like the link list, only using array like code conventions here serving the same purpose and sparing some effort here, if your ordered list is simple enough.

While avoiding linked list at all costs could be very pythonic in terms of code structure, I'd also mention in terms of larger more extensive data structures, reading clarity might be aided here using linked lists in python and possibly aiding in translation to other programming languages.

No comments:

Post a Comment

Oblivion

 Between the fascination of an upcoming pandemic ridden college football season, Taylor Swift, and Kim Kardashian, wildfires, crazier weathe...