A question about reference in Python.

Chris Rebert clp at rebertia.com
Mon Dec 8 00:36:42 EST 2008


On Sun, Dec 7, 2008 at 9:26 PM, Group <kermit.group at gmail.com> wrote:
> Hello, I'm studying algorithom. For concentrating on the question itself, I
> intend to use Python to implement the algorithoms.
>
> Now, I want to write a Red-Black Tree, and a List structure. In C/C++, I can
> use pointers to refer to  children  notes (or next notes). But, in Python,
> how
> can I do it? Except the sequence, I know not any way.
>
> You'd better help me understan how can I transform the following C code into
> Python:
>
> /* a List */
> struct {
>  int data;
>  int *next;
>  int *prev;
> }

Possibly not an exact literal translation, but:

class ListNode(object):
    def __init__(self, data, prev=None, next=None)
        self.data = data
        self.prev = prev
        self.next = next


Keep in mind that Python uses call-by-object (google it), so it
doesn't have pointers/references per-se.

Cheers,
Chris

-- 
Follow the path of the Iguana...
http://rebertia.com

>
> That's all. Thanks!
> Kermit
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list