php 队列、堆栈类V1.1
时间:2010-02-01
来源:互联网
/当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);
}
}
?>
//添加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);
}
}
?>
作者: php华南培训 发布时间: 2010-02-01
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28