Heim  >  Artikel  >  Backend-Entwicklung  >  Java、Python中没有指针,怎么实现链表、图等数据结构?

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

WBOY
WBOYOriginal
2016-06-06 16:23:243415Durchsuche

回复内容:

我只说一下 Java :虽然没有指针,但每个变量,如果不是基本数据类型(int float 等),那么就是一个引用(reference)。

引用类似指针,只是不能进行指针运算,比如 p + 1 指向下一个元素之类的。

各种语言的链表实现:

Singly-linked list/Element definition
Singly-linked list/Element insertion 实现基本的数据结构时指针唯一的作用就是指向,不涉及指针运算(pointer arithmetic)(这也不叫 const pointer),所以 Java / Python 的引用已经足够做到这一点了。就算实现 B-tree 什么的,配个数组也够了。
随手写个 Python 的链表节点,Java 的下面有人贴例子了,都是一样的道理。
需要
<code class="language-text">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
</code>
Python里直接用dict套dict就可以表示图,graph[u][v]=w,表示从u到v的有向边,边权(或者其他需要标记的信息)为w。
如果是无向图,可以限定u如果u到v有多条边,那就是dict套dict套set,graph[u][v]={w0,w1..}
如果要求存留边之间的或者节点之间的顺序关系,就把dict/set改为ordereddict。

以上做法相当于hash表示的邻接表,适合稀疏图。稠密图可以考虑用NumPy存储邻接矩阵以减小额外开销。

想看成熟的Python库怎样设计处理图的API的话,看Overview — NetworkX。 方法有很多:
  • 用内置数据结构(list, dict, tuple等)的嵌套/组合,它们隐式地包含了指向/嵌套关系,如上面回答中的graph[u][v]={w0,w1..}
  • 类的成员变量、嵌套类可能包含了指向/嵌套关系;
  • 引用表示指向关系,只不过引用不能像指针一样运算,比如 p + 1 指向下一个元素,所以可能限制颇多。
  • ...
Leetcode中的例子
<code class="language-python"><span class="k">class</span> <span class="nc">TreeNode</span><span class="p">:</span>
<span class="c"># 类的结构已经包含了指向关系</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">val</span> <span class="o">=</span> <span class="n">x</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">left</span> <span class="o">=</span> <span class="bp">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">right</span> <span class="o">=</span> <span class="bp">None</span>

<span class="n">l1</span> <span class="o">=</span> <span class="n">TreeNode</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> 
<span class="n">l2</span> <span class="o">=</span> <span class="n">TreeNode</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="n">l3</span> <span class="o">=</span> <span class="n">TreeNode</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>

<span class="c"># 引用表示指向关系</span>
<span class="n">l1</span><span class="o">.</span><span class="n">left</span>  <span class="o">=</span> <span class="n">l2</span>
<span class="n">l1</span><span class="o">.</span><span class="n">right</span> <span class="o">=</span> <span class="n">l3</span>
</code>
这个可以怒答一发,因为刚用java实现了一下链表,这里针对java和链表,python不发表意见

首先,楼上的大大们都说了,引用差不多是受限的指针,完全可以实现指针的功能

先上节点的代码
<code class="language-text">private class Node<t> {
    public Node<t> next;
    public T data;
    public Node(T d,Node<t> n) {data = d; next = n;}
}
</t></t></t></code>
java的引用就不说了,python里所有的变量其实都是对象的引用(连基本类型都是如此),而这个引用,说白了就是个指向实例的指针。

所以并不是没有指针,而是全都是指针只不过你看不出来而已。

做链表当然可以,对于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的简单链表实现:
<code class="language-python"><span class="c"># -*- coding: utf-8 -*-</span>

<span class="k">class</span> <span class="nc">Node</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">datum</span><span class="p">,</span> <span class="nb">next</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__datum</span> <span class="o">=</span> <span class="n">datum</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__next</span> <span class="o">=</span> <span class="nb">next</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">datum</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__datum</span>
        
    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">next</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__next</span>

    <span class="nd">@next.setter</span>
    <span class="k">def</span> <span class="nf">next</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">next</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__next</span> <span class="o">=</span> <span class="nb">next</span>


<span class="k">class</span> <span class="nc">LinkedList</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__head</span> <span class="o">=</span> <span class="bp">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__tail</span> <span class="o">=</span> <span class="bp">None</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">head</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__head</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">tail</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__tail</span>

    <span class="k">def</span> <span class="nf">purge</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__head</span> <span class="o">=</span> <span class="bp">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__tail</span> <span class="o">=</span> <span class="bp">None</span>

    <span class="k">def</span> <span class="nf">is_empty</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__head</span> <span class="ow">is</span> <span class="bp">None</span>

    <span class="k">def</span> <span class="nf">append</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">item</span><span class="p">):</span>
        <span class="c">#Generates the node.</span>
        <span class="n">datum</span> <span class="o">=</span> <span class="n">Node</span><span class="p">(</span><span class="n">item</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
        <span class="c">#For the first node in a linked list, it will change the head.</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">__head</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">__head</span> <span class="o">=</span> <span class="n">datum</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">__tail</span><span class="o">.</span><span class="n">next</span> <span class="o">=</span> <span class="n">datum</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__tail</span> <span class="o">=</span> <span class="n">datum</span>

    <span class="k">def</span> <span class="nf">remove</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">item</span><span class="p">):</span>
        <span class="n">pointer</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__head</span>
        <span class="n">prepointer</span> <span class="o">=</span> <span class="bp">None</span>
        <span class="c">#Finds the item</span>
        <span class="k">while</span> <span class="n">pointer</span> <span class="ow">and</span> <span class="n">pointer</span><span class="o">.</span><span class="n">datum</span> <span class="o">!=</span> <span class="n">item</span><span class="p">:</span>
            <span class="n">prepointer</span> <span class="o">=</span> <span class="n">pointer</span>
            <span class="n">pointer</span> <span class="o">=</span> <span class="n">pointer</span><span class="o">.</span><span class="n">next</span>
        <span class="c">#Raise the error when not finding the item.</span>
        <span class="k">if</span> <span class="n">pointer</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span> <span class="k">raise</span> <span class="ne">KeyError</span>
        <span class="c">#When the linked list has one node, it will sets the head.</span>
        <span class="k">if</span> <span class="n">pointer</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">__head</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">__head</span> <span class="o">=</span> <span class="n">pointer</span><span class="o">.</span><span class="n">next</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">prepointer</span><span class="o">.</span><span class="n">next</span> <span class="o">=</span> <span class="n">pointer</span><span class="o">.</span><span class="n">next</span>
        <span class="k">if</span> <span class="n">pointer</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">__tail</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">__tail</span> <span class="o">=</span> <span class="n">prepointer</span>
</code>
其实完全不用指针也可以实现这种结构。
参见:cons
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn