php_PHP チュートリアルで単一リンク リストを実装するためのコード例

WBOY
リリース: 2016-07-21 15:12:25
オリジナル
1033 人が閲覧しました

コードをコピーします コードは次のとおりです:

//链表节点
class node {
public $id; //节点id
public $name; //节点名称
public $next; //下一节点

public function __construct($id, $name) {
$this->id = $id;
        $this->name = $name;
        $this->next = null;
    }
}

//单链表
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;
}
$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;
休憩。
}
$current = $current->next;
}
if ($flag) {
$current->next = $current->next->next;
} else {
echo "未找到id=" . $id . 「のポイント!echo(リンクされたリスト);リンクされたリストは空です!";
return;
$current->next; }
| If ($current-> id == $id) {
>
$lists = new singelLinkList ();
$lists->addLink (新しいノード ( 5, 'eeeeee' ) );
$lists->addLink ( 新しいノード ( 1, 'aaaaaa' ) ); ->addLink (新しいノード ( 6, 'ffffff' ) );
$lists->addLink ( 新しいノード ( 4, 'dddddd' ) );
$lists->addLink ( 新しいノード ( 3, 'cccccc) ' ) );
$lists->addLink ( 新しいノード ( 2, 'bbbbbb' ) );
echo "
------- -ノードの削除--------------
";
$lists->delLink (5);
$lists->getLinkList ();

echo "
----------ノード名を更新-----
";
$lists->updateLink ( 3 , "222222" );
$lists->getLinkList();

echo "
----------ノード名を取得-----
";
echo $lists->getLinkNameById ( 5);

echo "
----------リンクされたリストの長さを取得します--------------
"
echo $lists- >getLinkLength ( )?>

;

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