php栈好队列的链式实现

<?php

 

class Stack {

private $stack;

private $top;

private $size;

public function  __construct(){

$this->stack=array();

$this->top=null;

$this->size=0;

}

//初始化

public function initStack(){

$this->stack=array();

$this->top=null;

$this->size=0;

}

//销毁

public function destoryStack(){

if(isset($this->stack)&&is_null($this->stack)){

unset($this->stack);

}

if(isset($this->top)&&is_null($this->top)){

unset($this->top);

}

if(isset($this->size)&&is_null($this->size)){

unset($this->size);

}

}

//清空

public function clearStack(){

if(isset($this->stack)&&is_null($this->stack)){

unset($this->stack);

$this->stack=array();

}

if(isset($this->top)&&is_null($this->top)){

unset($this->top);

$this->top=null;

}

if(isset($this->size)&&is_null($this->size)){

unset($this->size);

$this->size=0;

}

}

//空判断

public function emptyStack(){

if(isset($this->size)&&$this->size==0){

return TRUE;

}

}

//大小

public function lengthStack(){

if(isset($this->size)){

return $this->size;

}

}

//入栈

public function push($e){

if(isset($this->stack)&&is_array($this->stack)){

$id=$this->uuid();

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

//栈空

$this->stack[$id][‘data’]=$e;

$this->stack[$id][‘next’]=null;

$this->top=$id;

}else{

$tmp=$this->top;

$this->top=$id;

$this->stack[$id][‘data’]=$e;

$this->stack[$id][‘next’]=$tmp;

}

$this->size++;

}

}

//出栈

public function pop(){

if(isset($this->stack)&&is_array($this->stack)){

if($this->size==0){

echo “栈空”;

exit();

}else{

$tmp=$this->top;

$this->top=$this->stack[$tmp][‘next’];

unset($this->stack[$tmp]);

$this->size–;

}

}

}

//得到栈顶元素

public function  getTop(){

if(isset($this->stack)&&is_array($this->stack)){

if($this->size==0){

echo “栈空”;

exit();

}else{

return  $this->stack[$this->top][‘data’];

}

}

}

//遍历

public function traverstStack(){

if(isset($this->stack)&&is_array($this->stack)){

$tmp=$this->top;

while($this->stack[$tmp][‘next’]!=null){

$this->printStack($this->stack[$tmp][‘data’],TRUE);

$tmp=$this->stack[$tmp][‘next’];

}

$this->printStack($this->stack[$tmp][‘data’],FALSE);

}

}

public function printStack($str,$flag){

if($flag){

echo$str.”->”;

}else {

echo$str.”<br>”;

}

}

//uuid 唯一码

public  function uuid($prefix = ”) {

$chars = md5(uniqid(mt_rand(), true));

$uuid = substr($chars,0,8) . ‘-‘;

$uuid .= substr($chars,8,4) . ‘-‘;

$uuid .= substr($chars,12,4) . ‘-‘;

$uuid .= substr($chars,16,4) . ‘-‘;

$uuid .= substr($chars,20,12);

return $prefix . $uuid;

}

}

 

?>

 

 

队列

<?php

 

class Queue {

private $queue;

private $front;

private $rear;

private $size;

public function  __construct(){

$this->queue=array();

$this->front=null;

$this->rear=null;

$this->size=0;

}

//初始化

public function initQueue(){

$this->queue=array();

$this->front=null;

$this->rear=null;

$this->size=0;

}

//销毁

public function destoryQueue(){

if(isset($this->queue)&&is_null($this->queue)){

unset($this->queue);

}

if(isset($this->front)&&is_null($this->front)){

unset($this->front);

}

if(isset($this->size)&&is_null($this->size)){

unset($this->size);

}

if(isset($this->rear)&&is_null($this->rear)){

unset($this->rear);

}

}

//清空

public function clearQueue(){

if(isset($this->queue)&&is_null($this->queue)){

unset($this->queue);

$this->queue=array();

}

if(isset($this->front)&&is_null($this->front)){

unset($this->front);

$this->front=null;

}

if(isset($this->size)&&is_null($this->size)){

unset($this->size);

$this->size=0;

}

if(isset($this->rear)&&is_null($this->rear)){

unset($this->rear);

$this->rear=null;

}

}

//空判断

public function emptyQueue(){

if(isset($this->size)&&$this->size==0){

return TRUE;

}

}

//大小

public function lengthQueue(){

if(isset($this->size)){

return $this->size;

}

}

//入对

public function enQueue($e){

if(isset($this->queue)&&is_array($this->queue)){

$id=$this->uuid();

if($this->size==0){

//队列为空

$this->front=$this->rear=$id;

$this->queue[$id][‘data’]=$e;

$this->queue[$id][‘next’]=null;

}else{

$tmp=$this->rear;

$this->queue[$id][‘data’]=$e;

$this->queue[$id][‘next’]=null;

$this->queue[$tmp][‘next’]=$id;

$this->rear=$id;

}

$this->size++;

}

}

//出对

public function deQueue(){

if(isset($this->queue)&&is_array($this->queue)){

if($this->size==0){

echo “队空”;

exit();

}else{

$tmp=$this->front;

$this->front=$this->queue[$tmp][‘next’];

unset($this->queue[$tmp]);

$this->size–;

}

}

}

//得到队列头元素

public function  getHead(){

if(isset($this->queue)&&is_array($this->queue)){

if($this->size==0){

echo “对空”;

exit();

}else{

return  $this->queue[$this->front][‘data’];

}

}

}

//遍历

public function traverstQueue(){

if(isset($this->queue)&&is_array($this->queue)){

$tmp=$this->front;

while($this->queue[$tmp][‘next’]!=null){

$this->printQueue($this->queue[$tmp][‘data’],TRUE);

$tmp=$this->queue[$tmp][‘next’];

}

$this->printQueue($this->queue[$tmp][‘data’],FALSE);

}

}

public function printQueue($str,$flag){

if($flag){

echo$str.”->”;

}else {

echo$str.”<br>”;

}

}

//uuid 唯一码

public  function uuid($prefix = ”) {

$chars = md5(uniqid(mt_rand(), true));

$uuid = substr($chars,0,8) . ‘-‘;

$uuid .= substr($chars,8,4) . ‘-‘;

$uuid .= substr($chars,12,4) . ‘-‘;

$uuid .= substr($chars,16,4) . ‘-‘;

$uuid .= substr($chars,20,12);

return $prefix . $uuid;

}

}

 

?>

标签