Data structure and algorithms

azrael jura.grozni at gmail.com
Sun Jan 28 17:56:05 EST 2007


Hy, i am a student and in 2 days I am writing a test in data 
structures and algorithms. I've done my homework and understood all 
the implementations and structures. My profesor was so kind to allow 
us to use any programing language we want, and I'd like to use 
pythhon. At the first look it looked great, but we are not allowed to 
use the built in functions like append(), so i have to write my own.
 I tried it like this:

class Node:
  def __init__(self, cargo=None, next=None):
    self.cargo = cargo
    self.next  = next

  def __str__(self):
    return str(self.cargo)


then

>>> node1 = Node(1)
>>> node2 = Node(2)
>>> node3 = Node(3)

>>> node1.next = node2
>>> node2.next = node3

but this was not dinamicly enough for me (or my prof.) so i did the 
following:

>>> list=[]
>>> list.append(Node(1))
>>> list.append(Node(2))
>>> list[0].next=list[1]
>>> list.append(Node(3))
>>> list[1].next=list[2]



but deleting the list[1] willl automaticly make the list[2] become 
list[1] and list[0] will "point" new to list[1]= 3
For my prof i should be doing something like deleting movig the 
pointer from  pointing to list[1] to list[2]. If I would do this in C 
then the old list[1] would be lost, but not in python. If i just 
redirect the "pointer" then the old value is still there.

I think that my concept is wrong by using a list to create a list. Is 
it possible to manage the insertation of new object like in C by 
allocating new memory space.


any sugestions how to make the implementation more like in C. I never 
managed the syntax of C so I stoped when structs crossed my way. 
Please help. I dont want to learn C. And especialy not in 2 days.




More information about the Python-list mailing list