模擬鏈表:
<?php /** * PHP實(shí)現(xiàn)鏈表的基本操作 */class linkList { /** * 姓名 * @var string */ public $name = ''; /** * 編號(hào) * @var int */ public $id = 0; /* * 引用下一個(gè)對(duì)象 */ public $next = null; /** * 構(gòu)造函數(shù)初始化數(shù)據(jù) * @param int $id * @param string $name */ public function __construct($id = 0, $name = '') { $this->name = $name; $this->id = $id; } /** * 遍歷鏈表 */ public static function echo_link_list($head) { $curr = $head; while ($curr->next != null) { echo '姓名:'.$curr->next->name, ' 編號(hào):'.$curr->next->id; echo '<br>'; $curr = $curr->next; } } /** * 添加新節(jié)點(diǎn) */ public static function add($head, $id, $name) { $curr = $head; $obj = new linkList($id, $name); while ($curr->next != null) { // 如果當(dāng)前ID < 下一個(gè)ID,則添加到中間,添加節(jié)點(diǎn)到指定順序位置 if ($curr->next->id > $id) { $obj->next = $curr->next; $curr->next = $obj; return true; } else if ($curr->next->id == $id) { echo '當(dāng)前Id:'.$id.'重復(fù)了,請(qǐng)不要繼續(xù)添加了!'; echo '<br>'; return false; } $curr = $curr->next; } // 添加節(jié)點(diǎn)到尾部 if ($curr->next == null) { $curr->next = $obj; } } /** * 刪除節(jié)點(diǎn) */ public static function del($head, $id) { $curr = $head; while($curr->next != null) { if ($curr->next->id == $id) { $curr->next = $curr->next->next; return true; } $curr = $curr->next; } } /** * 修改節(jié)點(diǎn) */ public static function edit($head, $id, $new_name) { $curr = $head; while($curr->next != null) { if ($curr->next->id == $id) { $curr->next->name = $new_name; } $curr = $curr->next; } }} $head = new linkList();linkList::add($head, 1, 'wangdk');linkList::add($head, 2, 'sunshuzhen');linkList::add($head, 8, 'wanghaha');linkList::add($head, 6, 'wangchufen');linkList::add($head, 6, 'wangchufen');linkList::add($head, 3, 'wangdaye'); linkList::del($head, 1);linkList::edit($head, 2, 'hahaha');linkList::echo_link_list($head); ?> 鏈表的增刪查改:
<?php /** * PHP實(shí)現(xiàn)鏈表的基本操作 */class linkList { /** * 姓名 * @var string */ public $name = ''; /** * 編號(hào) * @var int */ public $id = 0; /* * 引用下一個(gè)對(duì)象 */ public $next = null; /** * 構(gòu)造函數(shù)初始化數(shù)據(jù) * @param int $id * @param string $name */ public function __construct($id = 0, $name = '') { $this->name = $name; $this->id = $id; } /** * 遍歷鏈表 */ public static function echo_link_list($head) { $curr = $head; while ($curr->next != null) { echo '姓名:'.$curr->next->name, ' 編號(hào):'.$curr->next->id; echo '<br>'; $curr = $curr->next; } } /** * 添加新節(jié)點(diǎn) */ public static function add($head, $id, $name) { $curr = $head; $obj = new linkList($id, $name); while ($curr->next != null) { // 如果當(dāng)前ID < 下一個(gè)ID,則添加到中間,添加節(jié)點(diǎn)到指定順序位置 if ($curr->next->id > $id) { $obj->next = $curr->next; $curr->next = $obj; return true; } else if ($curr->next->id == $id) { echo '當(dāng)前Id:'.$id.'重復(fù)了,請(qǐng)不要繼續(xù)添加了!'; echo '<br>'; return false; } $curr = $curr->next; } // 添加節(jié)點(diǎn)到尾部 if ($curr->next == null) { $curr->next = $obj; } } /** * 刪除節(jié)點(diǎn) */ public static function del($head, $id) { $curr = $head; while($curr->next != null) { if ($curr->next->id == $id) { $curr->next = $curr->next->next; return true; } $curr = $curr->next; } } /** * 修改節(jié)點(diǎn) */ public static function edit($head, $id, $new_name) { $curr = $head; while($curr->next != null) { if ($curr->next->id == $id) { $curr->next->name = $new_name; } $curr = $curr->next; } }} $head = new linkList();linkList::add($head, 1, 'wangdk');linkList::add($head, 2, 'sunshuzhen');linkList::add($head, 8, 'wanghaha');linkList::add($head, 6, 'wangchufen');linkList::add($head, 6, 'wangchufen');linkList::add($head, 3, 'wangdaye'); linkList::del($head, 1);linkList::edit($head, 2, 'hahaha');linkList::echo_link_list($head); ?> 
















