ホームページ > バックエンド開発 > PHPの問題 > PHPで単一リンクリストを実装する方法

PHPで単一リンクリストを実装する方法

藏色散人
リリース: 2023-03-08 07:50:01
オリジナル
1586 人が閲覧しました

単一リンク リストを実装する

php メソッド: 最初にリンク リスト ノードのクラスを記述し、次にリンク リストに 2 つのメソッド (挿入と削除) を定義し、次にリンク リストの長さを取得してノードを追加します。データ; 最後にノード名を取得し、それを削除または更新するだけです。

PHPで単一リンクリストを実装する方法

#この記事の動作環境: 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 &#39;id:&#39; . $current->next->id . &#39;   name:&#39; . $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, &#39;eeeeee&#39; ) ); 
$lists->addLink ( new node ( 1, &#39;aaaaaa&#39; ) ); 
$lists->addLink ( new node ( 6, &#39;ffffff&#39; ) ); 
$lists->addLink ( new node ( 4, &#39;dddddd&#39; ) ); 
$lists->addLink ( new node ( 3, &#39;cccccc&#39; ) ); 
$lists->addLink ( new node ( 2, &#39;bbbbbb&#39; ) ); 
$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 サイトの他の関連記事を参照してください。

関連ラベル:
php
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
PHP 拡張子 intl
から 1970-01-01 08:00:00
0
0
0
phpのデータ取得?
から 1970-01-01 08:00:00
0
0
0
PHP GET エラー レポート
から 1970-01-01 08:00:00
0
0
0
phpを上手に学ぶ方法
から 1970-01-01 08:00:00
0
0
0
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート