単一リンク リストを実装する
php メソッド: 最初にリンク リスト ノードのクラスを記述し、次にリンク リストに 2 つのメソッド (挿入と削除) を定義し、次にリンク リストの長さを取得してノードを追加します。データ; 最後にノード名を取得し、それを削除または更新するだけです。
#この記事の動作環境: Windows7 システム、PHP7.1、Dell G3 コンピューター。
PHP で実装された単一リンク リスト
名前が示すように、単一リンク リストはリンクされたデータ構造であり、ヘッダーと、最後のノードを除くすべてのノードがあります。ノードにはその後継ノードがあります。以下に示すように。
まず、リンク リスト ノードのクラスを記述します。単一リンク リストの各ノードは、そのデータ フィールドとバックドライブ ポインタを保存します
//链表节点 class node { public $id; //节点id public $name; //节点名称 public $next; //下一节点 public function __construct($id, $name) { $this->id = $id; $this->name = $name; $this->next = null; } }
リンク リストには、挿入と削除という 2 つの特に重要なメソッドがあります。挿入には、次の図の左側に示すように、挿入位置を見つけて、前の要素の次のポインターを挿入されたノードにポイントし、挿入されたノードの次のポインターを次のノードにポイントする必要があります。以下の図の右側に示すように、削除では、前のノードの次のポインターが次のノードを指し、削除された要素のデータ コンテンツが返されます。
推奨: 「PHP ビデオ チュートリアル 」
//单链表 class singelLinkList { private $header; //链表头节点 //构造方法 public function __construct($id = null, $name = null) { $this->header = new node ( $id, $name, null ); } //获取链表长度 public function getLinkLength() { $i = 0; $current = $this->header; while ( $current->next != null ) { $i ++; $current = $current->next; } return $i; } //添加节点数据 public function addLink($node) { $current = $this->header; while ( $current->next != null ) { if ($current->next->id > $node->id) { break; } $current = $current->next; } $node->next = $current->next; $current->next = $node; } //删除链表节点 public function delLink($id) { $current = $this->header; $flag = false; while ( $current->next != null ) { if ($current->next->id == $id) { $flag = true; break; } $current = $current->next; } if ($flag) { $current->next = $current->next->next; } else { echo "未找到id=" . $id . "的节点!<br>"; } } //判断连表是否为空 public function isEmpty(){ return $this->header == null; } //清空链表 public function clear(){ $this->header = null; } //获取链表 public function getLinkList() { $current = $this->header; if ($current->next == null) { echo ("链表为空!"); return; } while ( $current->next != null ) { echo 'id:' . $current->next->id . ' name:' . $current->next->name . "<br>"; if ($current->next->next == null) { break; } $current = $current->next; } } //获取节点名字 public function getLinkNameById($id) { $current = $this->header; if ($current->next == null) { echo "链表为空!"; return; } while ( $current->next != null ) { if ($current->id == $id) { break; } $current = $current->next; } return $current->name; } //更新节点名称 public function updateLink($id, $name) { $current = $this->header; if ($current->next == null) { echo "链表为空!"; return; } while ( $current->next != null ) { if ($current->id == $id) { break; } $current = $current->next; } return $current->name = $name; } } $lists = new singelLinkList (); $lists->addLink ( new node ( 5, 'eeeeee' ) ); $lists->addLink ( new node ( 1, 'aaaaaa' ) ); $lists->addLink ( new node ( 6, 'ffffff' ) ); $lists->addLink ( new node ( 4, 'dddddd' ) ); $lists->addLink ( new node ( 3, 'cccccc' ) ); $lists->addLink ( new node ( 2, 'bbbbbb' ) ); $lists->getLinkList (); echo "<br>-----------删除节点--------------<br>"; $lists->delLink ( 5 ); $lists->getLinkList (); echo "<br>-----------更新节点名称--------------<br>"; $lists->updateLink ( 3, "222222" ); $lists->getLinkList (); echo "<br>-----------获取节点名称--------------<br>"; echo $lists->getLinkNameById ( 5 ); echo "<br>-----------获取链表长度--------------<br>"; echo $lists->getLinkLength ();
以上がPHPで単一リンクリストを実装する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。