php栈链表,PHP实现链表 栈 队列

本文介绍使用PHP实现链表数据结构,并在此基础上实现栈和队列的功能。文章详细展示了如何创建节点和链表,进行增删改查操作,以及如何利用链表实现栈和队列的数据结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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());

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值