java、python中没有指针,怎么实现链表、图等数据结构?

回复内容:
我只说一下 java :虽然没有指针,但每个变量,如果不是基本数据类型(int float 等),那么就是一个引用(reference)。引用类似指针,只是不能进行指针运算,比如 p + 1 指向下一个元素之类的。各种语言的链表实现:singly-linked list/element definitionsingly-linked list/element insertion
实现基本的数据结构时指针唯一的作用就是指向,不涉及指针运算(pointer arithmetic)(这也不叫 const pointer),所以 java / python 的引用已经足够做到这一点了。就算实现 b-tree 什么的,配个数组也够了。随手写个 python 的链表节点,java 的下面有人贴例子了,都是一样的道理。需要

class node(object):
def __init__(self, value, nextnode=none):
self.value = value
self._nextnode = nextnode
def append(self, n):
if not isinstance(n, node):
n = node(n)
self._nextnode, n = n, self._nextnode
self._nextnode._nextnode = n

python里直接用dict套dict就可以表示图,graph[u][v]=w,表示从u到v的有向边,边权(或者其他需要标记的信息)为w。如果是无向图,可以限定u

Posted in 未分类

发表评论