PHP实现链表 栈 队列
链表的原理:
查找头节点 然后 循环查找下面的子节点
class Node{
public $value;
public $next;
public function __construct($value = null, $next = null)
{
$this->value = $value;
$this->next = $next;
}
}
class LinkList{
public $head;
public $size;
public function __construct()
{
$this->head = new Node();
$this->size = 0;
}
public function addFirst($value)
{
$this->add(0, $value);
}
public function add($index, $value)
{
if($index > $this->size){
throw new Exception("超过链表的范围");
}
$current = $this->head;
for($i=0;$i< $index; $i++){
$current = $current->next;
}
$current->next = new Node($value, $current->next);
$this->size++;
}
public function addLast($value)
{
$this->add($this->size, $value);
}
public function edit($index, $value)
{
if($index > $this->size-1){
throw new Exception("超过链表的范围");
}
$current = $this->head;
for ($i=0; $i<= $index; $i++){
if($i == $index){
$current->value = $value;
}
$current = $current->next;
}
}
public function select($index)
{
if($index > $this->size-1){
throw new Exception("超过链表的范围");
}
$current = $this->head;
for ($i=0;$i<=$index;$i++){
if($i == $index){
return $current;
}
$current = $current->next;
}
}
public function delete($index)
{
if($index > $this->size-1){
throw new Exception("超过链表的范围");
}
$current = $this->head;
for ($i=0;$i<=$index;$i++) {
if($i == $index){
$current->next = $current->next->next;
}
$current = $current->next;
}
$this->size--;
}
/**
*
*/
public function isContain($value)
{
$current = $this->head->next;
while ($current){
if($current->value == $value){
return true;
}
$current = $current->next;
}
return false;
}
/**
* @return Node
*/
public function toString()
{
$current = $this->head->next;
$res = [];
while($current){
$res[] = $current->value;
$current = $current->next;
}
return implode("->", $res);
}
public function removeFileds($value)
{
$current = $this->head;
while ($current->next){
if($current->value == $value){
$current->value = $current->next->value;
$current->next = $current->next->next;
}else{
$current = $current->next;
}
}
}
/**
* 通过递归方式删除指定的节点值
* @param $value
*/
public function removeFiledsByRecursion( $value ){
$this->head = $this->removeByRecursion( $this->head ,$value);
return $this->head;
}
public function removeByRecursion( $node , $value, $level=0 ){
if( $node->next == null ){
$this->showDeeep($level,$node->val);
return $node->val == $value ? $node->next:$node;
}
$this->showDeeep($level,$node->val);
$node->next = $this->removeByRecursion( $node->next,$value,++$level );
$this->showDeeep($level,$node->val);
return $node->val == $value ? $node->next:$node;
}
/**
* 显示深度
* 帮助理解递归执行过程,回调函数执行层序遵循系统栈
* @param int $level 深度层级
* @param $val
* @return bool
*/
public function showDeeep( $level=1,$val ){
if( $level<1 ){
return false;
}
while($level--){
echo '-';
}
echo "$val\n";
}
}
$node = new Linklist();
$node->addFirst(1);
$node->add(1,7);
$node->add(2,10);
$node->edit(1,8);
var_dump($node->select(1)) ;
$node->delete(1);
$node->addLast(99);
var_dump($node->iscontain(2));
var_export($node);
var_export($node->tostring());
/**
* 链表实现栈
* Class LinklistStack
* @package app\models
*/
class LinklistStack extends Linklist
{
/**
* @param $value
*/
public function push( $value ){
$this->addFirst($value);
}
/**
* @return mixed
*/
public function pop(){
$r = $this->head->next->value;
$this->delete(0);
return $r;
}
}
$stack = new LinklistStack();
$stack->push(1);
$stack->push(3);
$stack->push(6);
$stack->push(9);
print_r($stack->pop());
print_r($stack->head);
/**
* 链表实现队列
* Class LinkListQueue
* @package app\models
*/
class LinkListQueue extends Linklist
{
public $tail; //尾节点
/**
* push
* @param $value
*/
public function push( $value ){
if( $this->head->value==null ){
$this->tail = new Node( $value );
$this->head = $this->tail;
}else{
$this->tail->next = new Node( $value );
$this->tail = $this->tail->next;
}
$this->size++;
}
/**
* pop
* @return null
* @throws \Exception
*/
public function pop(){
if( $this->size<=0 )
throw new \Exception('超过链表范围');
$val = $this->head->value;
$this->head = $this->head->next;
$this->size--;
return $val;
}
/**
* 查看队首
*/
public function checkHead(){
return $this->head->value;
}
/**
* 查看队尾
*/
public function checkEnd(){
return $this->tail->value;
}
/**
* toString
*/
public function toString(){
$r = [];
while( $this->head ){
$r[] = $this->head->value;
$this->head = $this->head->next;
}
return implode('->',$r);
}
}
$stack = new LinkListQueue();
$stack->push(1);
$stack->push(3);
$stack->push(6);
$stack->push(9);
print_r($stack->pop());
print_r($stack->head);
print_r($stack->checkHead());
print_r($stack->checkEnd());
print_r($stack->toString());