程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP基礎知識 >> php 隊列、堆棧類

php 隊列、堆棧類

編輯:PHP基礎知識
 

//php 隊列、堆棧類V1.1

 

//當deQueue()或pop()時,會unset該變量。

 

//添加destroyStack()

 

//添加clearQueue()

 

<?

//Creator: sheva
 

 

class Queue{

private $queue;
private $front;
private $rear;

function Queue(){

$this->queue[0][”content”] = “NULL”;
$this->front = 0;
$this->rear = 0;

return true;

}

function enQueue($node){

$this->rear += 1;

$this->queue[$this->rear][”content”] = $node;

return true;

}

function deQueue(){

if($this->front == $this->rear){

return false;

}else{

$this->front += 1;

$temp = $this->queue[$this->front][”content”];

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

if($this->front == $this->rear){

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

}

return $temp;

}

}

function getHead(){

return $this->queue[$this->front + 1][”content”];

}

function isEmpty(){

if($this->front == $this->rear)
return true;
else
return false;

}

function destroyQueue(){

unset($this->queue);
unset($this->front);
unset($this->rear);

return true;

}

function clearQueue(){

for($i = 0; $i <= $this->rear; $i++ ){

$this->queue[$i][”content”] = “”;

}

 

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

return true;

}

function queueLength(){

return ($this->rear - $this->front);

}

//獲取第$pointer個node, 從1開始
function getNode($pointer){

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

if( $pointer <= $difference ){

 

return $this->queue[$pointer][”content”];

}else{

return false;

}

}
 

 

}


?>

 

 

<?
//Creator: sheva
 

 

class Stack{

private $stack;
private $top;
private $base;

function Stack(){

$this->stack[0][”content”] = “NULL”;
$this->top = 0;
$this->base = 0;

return true;

}

function destroyStack(){

unset($this->stack);
unset($this->top);
unset($this->base);

return true;

}

function clearStack(){

for($i = 1; $i <= $this->top; $i++ ){

$this->stack[$i][”content”] = “”;

}

$this->top = 0;
$this->base = 0;

}

function isEmpty(){

if($this->top == $this->base)
return true;
else
return false;

}

function getTop(){

if($this->top == $this->base)
return false;

return $this->stack[$this->top][”content”];

}

function push($node){

$this->top += 1;
$this->stack[$this->top][”content”] = $node;

return true;

}

function pop(){

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

 

return false;

}else{

 

$temp = $this->stack[$this->top][”content”];

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

$this->top -= 1;

return $temp;

}

}

function stackLength(){

return ($this->top - $this->base);

}

}


?>
 

 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved