class TreeNode:
# 类的结构已经包含了指向关系
def __init__(self, x):
self.val = x
self.left = None
self.right = None
l1 = TreeNode(0)
l2 = TreeNode(1)
l3 = TreeNode(2)
# 引用表示指向关系
l1.left = l2
l1.right = l3
登录后复制
这个可以怒答一发,因为刚用java实现了一下链表,这里针对java和链表,python不发表意见
首先,楼上的大大们都说了,引用差不多是受限的指针,完全可以实现指针的功能
先上节点的代码
private class Node {
public Node next;
public T data;
public Node(T d,Node n) {data = d; next = n;}
}
登录后复制
java的引用就不说了,python里所有的变量其实都是对象的引用(连基本类型都是如此),而这个引用php指针,说白了就是个指向实例的指针。
所以并不是没有指针,而是全都是指针只不过你看不出来而已。
做链表当然可以,对于python而言就一个类里两个属性,next仍旧是next,指向下个节点的实例就可以了。Python和Java的“引用”就是不能运算的指针。
其实我更喜欢go语言的理念,没有任何“引用”,所以就避免了混淆的“传引用”概念。所有的值传递都是传值,指针就是指针,但是不能运算。其实Java, python完全可以看成在语言层用语法糖隐藏了指针。
Java中,假设class A有 属性 int a, B(class B) b,那A实例化(b在A实例化新开辟而不是通过外部赋值),内存开辟一段包含一个int a和 一个b 的reference地址,b实例化和A一样,然后把地址抛给A中b的reference。其实这个和C/C++中的A中有个B* b差不多。所以链表、图等数据结构在C/C++中的实现转义到Java中也就是改改语法糖的表示。
Python中,链表、树等数据结构大致对应Python自带list、tuple、dict。Python这种动态语言用C实现,拿Python的list来讲,用一个struct PyListObject 实现,看看源码指针随处都是。
C/C++这样的语言比较接近早期抽象计算机数据的思维方式,指针其实就是我们抽象数据并且这个数据是聚合但是实际内存离散的胶合层产物。后来出的语言通过其他的方式来抽象数据表示,将指针这个概念隐藏了,显得友好了,但是其实他还在那。基本上没有指针会有更灵活的方式进行,回收机制直接丢弃可以自动释放内存,写起来也简单,一码胜千言,贴张python的简单链表实现:
# -*- coding: utf-8 -*-
class Node(object):
def __init__(self, datum, next):
self.__datum = datum
self.__next = next
@property
def datum(self):
return self.__datum
@property
def next(self):
return self.__next
@next.setter
def next(self, next):
self.__next = next
class LinkedList(object):
def __init__(self):
self.__head = None
self.__tail = None
@property
def head(self):
return self.__head
@property
def tail(self):
return self.__tail
def purge(self):
self.__head = None
self.__tail = None
def is_empty(self):
return self.__head is None
def append(self, item):
#Generates the node.
datum = Node(item, None)
#For the first node in a linked list, it will change the head.
if self.__head is None:
self.__head = datum
else:
self.__tail.next = datum
self.__tail = datum
def remove(self, item):
pointer = self.__head
prepointer = None
#Finds the item
while pointer and pointer.datum != item:
prepointer = pointer
pointer = pointer.next
#Raise the error when not finding the item.
if pointer is None: raise KeyError
#When the linked list has one node, it will sets the head.
if pointer == self.__head:
self.__head = pointer.next
else:
prepointer.next = pointer.next
if pointer == self.__tail:
self.__tail = prepointer
登录后复制
其实完全不用指针也可以实现这种结构。
参见:cons

(编辑:晋中站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|