国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > PHP > 正文

PHP實(shí)現(xiàn)單鏈表

2019-11-08 01:47:32
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
class Node { public $data = ''; public $next = null; public function __construct($data = ''){ $this->data = $data; }}//添加節(jié)點(diǎn)function addNode($head,$data){ $cur = $head; while($cur->next != null){ $cur = $cur->next; } $new = new Node($data); $cur->next = $new;}//獲取鏈表長(zhǎng)度,元素個(gè)數(shù)function getLength($head){ $cur = $head; $i = 0; while($cur->next != null){ ++$i; $cur = $cur->next; } return $i;}//遍歷元素值function showNode($head){ $cur = $head; while($cur->next != null){ $cur = $cur->next; echo $cur->data.php_EOL; }}//刪除節(jié)點(diǎn)function delNode($head,$seq){ $cur = $head; $flag = 0; for($i=0;$i<$seq-1;$i++){ $cur = $cur->next; if($cur->next == null){ $flag = 1; break; } } if($flag == 0){ $cur->next = $cur->next->next; }}//插入節(jié)點(diǎn)function insertNode($head,$data,$seq){ $cur = $head; $flag = 0; $node = new Node($data); for($i=0;$i<$seq;$i++){ $cur = $cur->next; if($cur->next == null){ $flag = 1; break; } } if($flag == 0){ $node->next = $cur->next; $cur->next = $node; }}//翻轉(zhuǎn)鏈表function reverseNode($head){ if($head == null){ return $head; } $PRe = $head; $cur = $head->next; $next = null; while($cur != null){ $next = $cur->next; $cur->next = $pre; $pre = $cur; $cur = $next; } $head->next = null; $head = $pre; return $head;} $head = new Node('head');addNode($head,1);addNode($head,2);addNode($head,3);print_r($head);showNode($head);insertNode($head,0,2);print_r($head);delNode($head,3);print_r($head);$len = getLength($head);print_r($len);$head = reverseNode($head);print_r($head);
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 化德县| 彭水| 乌兰察布市| 育儿| 昭觉县| 商洛市| 辽阳市| 融水| 漠河县| 会理县| 桓台县| 广安市| 汕尾市| 巴里| 涡阳县| 林芝县| 万全县| 韶关市| 东丰县| 驻马店市| 梓潼县| 张家界市| 聊城市| 彭山县| 锡林郭勒盟| 自贡市| 高雄县| 随州市| 阿荣旗| 邵武市| 道孚县| 延长县| 富平县| 中山市| 龙门县| 海城市| 渝中区| 双城市| 云阳县| 凤庆县| 甘南县|