[PHP] 链表数据结构(单链表),php表数据结构单链_PHP教程

WBOY
Libérer: 2016-07-12 08:52:23
original
1139 Les gens l'ont consulté

[PHP] 链表数据结构(单链表),php表数据结构单链

链表:是一个有序的列表,但是它在内存中是分散存储的,使用链表可以解决类似约瑟夫问题,排序问题,搜索问题,广义表

单向链表,双向链表,环形链表

 

PHP的底层是C,当一个程序运行时,内存分成五个区(堆区,栈区,全局区,常量区,代码区)

规定:基本数据类型,一般放在栈区

复合数据类型,比如对象,放在堆区

 

定义一个类Hero

定义成员属性排名 $no

定义成员属性姓名 $name

定义成员属性昵称 $nickname

定义成员属性 $next,是一个引用,指向下一个Hero对象

定义构造函数,传递参数:$no$name$nickname

 

创建一个头head,该head只是一个头,不放入数据

获取$head对象,new Hero()

获取第一个Hero对象$heronew Hero(1,”宋江”,”及时雨”)

连接两个对象,$head->next=$hero

获取第二个Hero对象$hero2new Hero(2,”卢俊义”,”玉麒麟”)

连接两个对象,$hero->next=$hero2

 

遍历链表

定义一个函数showHeros(),参数:$head对象

定义一个临时变量$cur来存储 $head对象

while循环,条件$cur->next不为null

打印一下

指针后移,$cur=$cur->next

PHP版:

 

<?<span>php
</span><span>/*</span><span>*
* 英雄类
</span><span>*/</span>
<span>class</span><span> Hero{
    </span><span>public</span> <span>$no</span><span>;
    </span><span>public</span> <span>$name</span><span>;
    </span><span>public</span> <span>$nickname</span><span>;
    </span><span>public</span> <span>$next</span>=<span>null</span><span>;
    </span><span>public</span> <span>function</span> __construct(<span>$no</span>='',<span>$name</span>='',<span>$nickname</span>=''<span>){
        </span><span>$this</span>->no=<span>$no</span><span>;
        </span><span>$this</span>->name=<span>$name</span><span>;
        </span><span>$this</span>->nickname=<span>$nickname</span><span>;
    }
}
</span><span>class</span><span> LinkListDemo{
    </span><span>public</span> <span>static</span> <span>function</span><span> main(){
        </span><span>$head</span>=<span>new</span><span> Hero();
        </span><span>$hero1</span>=<span>new</span> Hero(1,"宋江","及时雨"<span>);
        </span><span>$head</span>-><span>next</span>=<span>$hero1</span><span>;
        </span><span>$hero2</span>=<span>new</span> Hero(2,"卢俊义","玉麒麟"<span>);
        </span><span>$hero1</span>-><span>next</span>=<span>$hero2</span><span>;
        LinkListDemo</span>::showHeros(<span>$head</span><span>);
    }
    </span><span>/*</span><span>*
    * 展示英雄
    </span><span>*/</span>
    <span>public</span> <span>static</span> <span>function</span> showHeros(<span>$head</span><span>){
        </span><span>$cur</span>=<span>$head</span><span>;
        </span><span>while</span>(<span>$cur</span>-><span>next</span>!=<span>null</span><span>){
            </span><span>echo</span> "姓名:".<span>$cur</span>-><span>next</span>->name."<br/>"<span>;
            </span><span>$cur</span>=<span>$cur</span>-><span>next</span><span>;
        }
    }

}

LinkListDemo</span>::main();
Copier après la connexion

java版:

<span>class</span><span> Hero{
        </span><span>public</span> <span>int</span><span> no;
        </span><span>public</span><span> String name;
        </span><span>public</span><span> String nickname;
        </span><span>public</span> Hero next=<span>null</span><span>;
        </span><span>public</span><span> Hero(){
            
        }
        </span><span>public</span> Hero(<span>int</span><span> no,String name,String nickname) {
            </span><span>this</span>.no=<span>no;
            </span><span>this</span>.name=<span>name;
            </span><span>this</span>.nickname=<span>nickname;
        }

    }
</span><span>public</span> <span>class</span><span> LinkListDemo {
    </span><span>/**</span><span>
     * </span><span>@param</span><span> args
     </span><span>*/</span>
    <span>public</span> <span>static</span> <span>void</span><span> main(String[] args) {
        Hero head</span>=<span>new</span><span> Hero();
        
        Hero hero1</span>=<span>new</span> Hero(1, "宋江", "及时雨"<span>);
        head.next</span>=<span>hero1;
        Hero hero2</span>=<span>new</span> Hero(2, "卢俊义", "玉麒麟"<span>);
        hero1.next</span>=<span>hero2;
        showHeros(head);
    }
    </span><span>/**</span><span>
     * 展示英雄
     * </span><span>@param</span><span> head
     </span><span>*/</span>
    <span>public</span> <span>static</span> <span>void</span><span> showHeros(Hero head){
        Hero cur</span>=<span>head;
        </span><span>while</span>(cur.next!=<span>null</span><span>){
            System.out.println(</span>"姓名:"+<span>cur.next.name);
            cur</span>=<span>cur.next;
        }
    }
}</span>
Copier après la connexion

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1127372.htmlTechArticle[PHP] 链表数据结构(单链表),php表数据结构单链 链表:是一个有序的列表,但是它在内存中是分散存储的,使用链表可以解决类似约瑟夫...
Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal