//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);
}
}
?>