php版本链表操作
单链表
<?php class Node{ public $data = ''; public $next = null; function __construct($data) { $this->data = $data; } } // 链表有几个元素 function countNode($head){ $cur = $head; $i = 0; while(!is_null($cur->next)){ ++$i; $cur = $cur->next; } return $i; } // 增加节点 function addNode($head, $data){ $cur = $head; while(!is_null($cur->next)){ $cur = $cur->next; } $new = new Node($data); $cur->next = $new; } // 紧接着插在$no后 function insertNode($head, $data, $no){ if ($no > countNode($head)){ return false; } $cur = $head; $new = new Node($data); for($i=0; $i<$no;$i++){ $cur = $cur->next; } $new->next = $cur->next; $cur->next = $new; } // 删除第$no个节点 function delNode($head, $no){ if ($no > countNode($head)){ return false; } $cur = $head; for($i=0; $i<$no-1; $i++){ $cur = $cur->next; } $cur->next = $cur->next->next; } // 遍历链表 function showNode($head){ $cur = $head; while(!is_null($cur->next)){ $cur = $cur->next; echo $cur->data, '<br/>'; } } $head = new Node(null);// 定义头节点 addNode($head, 'a'); addNode($head, 'b'); addNode($head, 'c'); insertNode($head, 'd', 0); showNode($head); echo '<hr/>'; delNode($head, 2); showNode($head);
双向链表
<?php class link { public $preLink = NULL; //前指针 public $number; //排名 public $name; //名字 public $nextLink = NULL; //后指针 //构造函数,初始化值 public function __construct($number = '', $name = '') { $this->number = $number; $this->name = $name; } //增加数据 public static function addDom($head, $data) { $obj = $head; $isExist = FALSE; if (NULL == $obj->nextLink) { $obj->nextLink = $data; $data->preLink = $head; } //找到添加的位置 while (NULL != $obj->nextLink) { if ($obj->nextLink->number > $data->number) { break; } else if ($obj->nextLink->number == $data->number) { $isExist = TRUE; echo "<br>不能添加相同的编号"; } $obj = $obj->nextLink; } if (!$isExist) { if (NULL != $obj->nextLink) { $data->nextLink = $obj->nextLink; } $data->preLink = $obj; if (NULL != $obj->nextLink) { $data->nextLink->preLink = $data; } $obj->nextLink = $data; } } //输出链表数据 public static function showDom($head) { $obj = $head; while (NULL != $obj->nextLink) { echo "<br>编号:" . $obj->nextLink->number . "名字:" . $obj->nextLink->name; $obj = $obj->nextLink; } } public static function delDom($head, $num) { $obj = $head; $isFind = FALSE; while (NULL != $obj) { if ($obj->number == $num) { $isFind = TRUE; break; } $obj = $obj->nextLink; } if ($isFind) { if (NULL != $obj->nextLink) { $obj->preLink->nextLink = $obj->nextLink; echo '<br/>删除的号码是' . $obj->number; } else { echo "<br>没有找到目标"; } } } public static function updateDom($head, $num, $name) { $obj = $head; while (NULL != $obj) { if ($obj->number == $num) { $obj->name = $name; echo "<br/>改变号码{$obj->number}的结果:" . $obj->name; break; } $obj = $obj->nextLink; } } } $head = new link(); $one = new link(1, 'oooooooo'); $two = new link(2, 'wwwwwwww'); $three = new link(3, 'eeeeeeee'); link::addDom($head, $one); link::addDom($head, $two); link::addDom($head, $three); link::showDom($head); link::delDom($head, 2); link::showDom($head); link::updateDom($head, 3, 'kkkkkk'); link::showDom($head);