简体   繁体   中英

Head pointer and Next pointer in Linked list in python

head和next属性如何保存地址,甚至在单链接列表程序 python中甚至没有明确声明它们为指针

In Python, variables are just names for objects. An object can have zero or more names. So if you have a simple class like:

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

You can create nodes and "name" the next one in the list:

>>> a = Node(1)
>>> b = Node(2)
>>> a.next = b    # a.next is another name for b
>>>
>>> b.value
2
>>> a.next.value
2
>>> b.value = 5     # change b
>>> a.next.value    # a.next another name for b
5

Internally, at least in CPython, The names refer to PyObject* instances, which are pointers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM