PHP コード PHP 単一リンク リスト実装のコード共有

WBOY
リリース: 2016-07-28 08:27:22
オリジナル
904 人が閲覧しました

この記事の例では、参考のために PHP 単一リンク リストの具体的なコードを共有します。具体的な内容は次のとおりです

<?php /** * 单链表 */ class Demo { private $id; public $name; public $next; public function __construct ($id = '', $name = '') { $this->id = $id; $this->name = $name; } static public function show ($head) { $cur = $head; while ($cur->next) { echo $cur->next->id,'###',$cur->next->name,'
'; $cur = $cur->next; } echo '
'; } //尾插法 static public function push ($head, $node) { $cur = $head; while (NULL != $cur->next) { $cur = $cur->next; } $cur->next = $node; return $head; } static public function insert($head, $node) { $cur = $head; while (NULL != $cur->next) { if ($cur->next->id > $node->id) { break; } $cur = $cur->next; } $node->next = $cur->next; $cur->next = $node; return $head; } static public function edit($head, $node) { $cur = $head; while (NULL != $cur->next) { if ($cur->next->id == $node->id) { break; } $cur = $cur->next; } $cur->next->name = $node->name; return $head; } static public function pop ($head, $node) { $cur = $head; while (NULL != $cur->next) { if ($cur->next == $node) { break; } $cur = $cur->next; } $cur->next = $node->next; return $head; } } $team = new Demo(); $node1 = new Demo(1, '唐三藏'); Demo::push($team, $node1); $node1->name = '唐僧'; Demo::show($team); // Demo::show($team); $node2 = new Demo(2, '孙悟空'); Demo::insert($team, $node2); // Demo::show($team); $node3 = new Demo(5, '白龙马'); Demo::push($team, $node3); // Demo::show($team); $node4 = new Demo(3, '猪八戒'); Demo::insert($team, $node4); // Demo::show($team); $node5 = new Demo(4, '沙和尚'); Demo::insert($team, $node5); // Demo::show($team); $node4->name = '猪悟能';//php对象传引用,所以Demo::edit没有必要 // unset($node4); // $node4 = new Demo(3, '猪悟能'); // Demo::edit($team, $node4); Demo::pop($team, $node1); Demo::show($team);
ログイン後にコピー

以上がこの記事の全内容であり、皆さんの実装に役立つことを願っています。 PHP の単一リンクリスト。

上記では、PHP コードの内容を含む PHP シングル リンク リストの実装コード共有を紹介しました。PHP チュートリアルに興味のある友人に役立つことを願っています。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!