php双向链表,PHP实现双向链表、栈

这次再来分享一下实现双向链表和栈的实现。代码虽然是以前写的了,但是发现PHP写的这些代码很容易看懂!

双向链表

classHero

{public $pre=null;//前指针

public $no;//排名

public $name;//名字

public $next=null;//后指针

/**

*构造函数,申明链表头*/

public function __construct($no=‘‘,$name=‘‘)

{$this->no=$no;$this->name=$name;

}/**

*插入*/

static public function addHero($head,$hero)

{$cur = $head;$isExist=false;//判断目前这个链表是否为空

if($cur->next==null)

{$cur->next=$hero;$hero->pre=$cur;

}else{//如果不是空节点,则安排名来添加

//找到添加的位置

while($cur->next!=null)

{if($cur->next->no > $hero->no)

{//如果大于了排名,跳出

break;

}else if($cur->next->no == $hero->no)

{//如果等于排名,则代表有这个元素了

$isExist=true;echo "
不能添加相同的编号";

}$cur=$cur->next;

}if(!$isExist)

{//如果元素不存在,执行插入操作

if($cur->next!=null)

{$hero->next=$cur->next;}$hero->pre=$cur;if($cur->next!=null)

{$hero->next->pre=$hero;}$cur->next=$hero;

}

}

}//遍历

static public function showHero($head)

{$cur=$head;while($cur->next!=null)

{echo "
编号:".$cur->next->no."名字:".$cur->next->name;$cur=$cur->next;

}

}static public function delHero($head,$herono)

{$cur=$head;$isFind=false;while($cur!=null)

{if($cur->no==$herono)

{$isFind=true;break;

}//继续找

$cur=$cur->next;

}if($isFind)

{if($cur->next!=null)

{$cur->next_pre=$cur->pre;}$cur->pre->next=$cur->next;

}else{echo "
没有找到目标";}

}

}$head = newHero();$hero1 = new Hero(1,‘1111‘);$hero3 = new Hero(3,‘3333‘);$hero2 = new Hero(2,‘2222‘);

Hero::addHero($head,$hero1);

Hero::addHero($head,$hero3);

Hero::addHero($head,$hero2);

Hero::showHero($head);

Hero::delHero($head,2);

Hero::showHero($head);?>

双向链表的插入操作示意图:

if($cur->next!=null)$hero->next=$cur->next;$hero->pre=$cur;if($cur->next!=null)$hero->next->pre=$hero;$cur->next=$hero;

1c7fa3c94d5159e7a59c1d82e71bba55.png

删除操作示意图:

if($cur->next!=null)$cur->next->pre=$cur->pre;$cur->pre->next=$cur->next;

da41aad2c6715f8d52e7f366f3401077.png

{private $top=-1;private $maxSize=5;private $stack=array();public function push($val)

{if($this->top == $this->maxSize)

{echo "
已经满了";

}$this->top++;$this->stack[$this->top]=$val;

}public functionshowStack()

{if($this->top==-1)

{echo "
栈为空!";return;

}for($i=$this->top;$i>-1;$i--)

{echo "
stack[".$i."]=".$this->stack[$i];

}

}public functionpop()

{if($this->top==-1)

{echo "
栈为空!";return;

}$val=$this->stack[$this->top];$this->top--;echo "
弹出".$val;

}

}$mystack = newmyStack;$mystack->push(‘111‘);$mystack->push(‘222‘);$mystack->showStack();$mystack->pop();$mystack->pop();?>

1338772566372.jpg

栈(Stack):是限制在表的一端进行插入和删除操作的线性表。又称为后进先出LIFO (Last In First Out)或先进后出FILO (First In Last Out)线性表。

栈在计算机的实现有多种方式:

硬堆栈:利用CPU中的某些寄存器组或类似的硬件或使用内存的特殊区域来实现。这类堆栈容量有限,但速度很快;

软堆栈:这类堆栈主要在内存中实现。堆栈容量可以达到很大。在实现方式上,又有动态方式和静态方式两种

栈顶(Top):允许进行插入、删除操作的一端,又称为表尾。用栈顶指针(top)来指示栈顶元素。

栈底(Bottom):是固定端,又称为表头。

空栈:当表中没有元素时称为空栈。

栈的链式存储结构称为链栈,是运算受限的单链表。其插入和删除操作只能在表头位置上进行。因此,链栈没有必要像单链表那样附加头结点,栈顶指针top就是链表的头指针。

当然,php中的数组API里面带的有push和pop函数。

我是天王盖地虎的分割线

原文:http://www.cnblogs.com/yydcdut/p/3827214.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值