<?php
/*
堆棧和隊列都是特殊的線性表,差別是線性表的插入刪除操作不受限制,而堆棧只能在棧頂刪除和插入,隊列只能在隊尾插入,對頭刪除。堆棧可以用來完成數據元素序列的特定轉換,隊列可以用做數據元素序列的緩沖存儲。
堆棧:堆棧是一種特殊的線性表,堆棧的 數據元素以及數據元素之間的邏輯關系和線性表完全相同,只是線性表允許在任意位置插入和刪除數據元素,而堆棧指是在固定的一端進行數據的插入和刪除操作。
堆棧允許進行數據元素插入和刪除的一段稱為棧頂,另一端成為棧尾。棧頂的當前位置是動態因為隨時會插入數據和刪除數據。堆棧是一種后進先出的操作方式。PS:任何支持遞歸算法的程序設計語言,都是借助堆棧來實現遞歸算法的。
堆棧的數據集合
表示為A0,A1,........An-1.
操作集合
初始化堆棧
堆棧S是否非空
入棧:在堆棧S棧頂插入數據元素
出棧:在堆棧S棧頂刪除元素
去棧頂元素
堆棧的順序表表示和實現:
數序堆棧的存儲結構:順序堆棧和順序表的數據成員是相同的,不同的是順序的入棧和出棧都是在棧頂進行。
由此我們分析可以知道,需要一個存儲堆棧的數組,需要一個表示當前棧頂的top,需要定義數組的最大值Maxsize,。
*/
//數組(順序表形式的堆棧)形式的堆棧
class Tablestrack{
public $strack = array();
public $Maxsize = 10;
public $top;
public function __construct() {
$this->top = 0;//當前棧頂未 數組開頭
}
public isempty() {
if($this->top == 0 && $this->strack[0] == null) {
return true;
} else {
return false;
}
}
public function insert($str) {
if($this->top >=$this->Maxsize) {
echo "當前堆棧已滿";
return false;
}
$this->strack[$this->top] = $str;
$this->top++;
/*
或者這種寫法
if($this->top >=($this->Maxsize-1) ) {
echo "當前堆棧已滿";
return false;
}
if($this->top == 0 && $this->strack[0] == null){
$this->strack[$this->top] = $str;
} else {
$this->top++;
$this->strack[$this->top] = $str;
}
*/
}
public function delete() {
if($this->top <=0 && $this->top == null) {
echo "當前堆棧已空";
return false;
}
$this->top--;
$this->strack[$this->top] = null;
/*
或者這種寫法
$this->strack[$this->top] = null;
$this->top--;
*/
}
}
//堆棧的鏈式
class Node {
public $next;
public $data;
}
class strack {
public $head;
public function __construct() {
$this->head = null;
}
public function isempty() {
if($this->head == null) {
return true;
}
}
public function insert($str) {
if($this->head == null) {
$this->head->data = $str;
$this->head->next = null;
} else {
$old = $this->head;
$this->head = new Node;
$this->head->data = $str;
$this->head->next = $old;
}
}
public function delete() {
$this->head = $this->head->next;
}
}
/*****
隊列:也是一種特殊的線性表,隊列的數據元素及數據元素間的邏輯關系和線性表一樣,差別是隊列只允許在一端插入數據,一端刪除數據。允許插入數據的叫對頭,允許刪除數據的叫隊尾。對頭和隊尾分別有對頭指示器和隊尾指示器指示,隊列是一種先進先出的線性表。
每次新增加的數據都放在隊列的隊尾的后面,每次出隊列的都是對頭數據。
--------------
對頭(出) |A0|A1|....|AN 隊尾(進)
---------------
隊列的抽象數據集合
數據集合:A0,A1,A2,。。。。
操作集合:
初始化 隊列Q
判斷隊列是否非空
如隊列
出隊列
取隊列頭部數據
順序存儲結構的隊列及問題:
順序隊列容易出現假溢出的問題,要解決假溢出都是使用循環順序隊列的方式。
****/
class TableQue {
public $q; //隊列數組
public $Maxsize;//隊列最大數據個數
public $count; //計數器,記錄當前隊列個數
public $rear; //隊尾指示器
public $front; //對頭指示器
public function __construct() {
$this->q = array();
$this->Maxsize = 10;
$this->count = 0;
$this->rear = 0;
$this->front = 0;
}
public isempty() {
if($this->count == 0) return true; else return false;
}
public insert($str) {
if($this->count > && $this->front == $this->rear) {
echo "隊列已滿";
return false;
}
$this->q[$this->rear] = $str;
$this->rear = ($this->rear + 1)%$this->Maxsize;
$this->count++;
}
public function delete() {
if($this->count == 0) {
echo "隊列已空";
}
$this->q[$this->front] = null;
$this->front = ($this->front+1)%$this->Maxsize;
$this->count--;
}
}
/**
鏈式隊列和線性表物理結構相同,不過我們單獨需要一個對頭指示器和一個隊尾指示器。
**/
class Qnode {
public $data; //數據域
public $next; //指針域
}
class Que{
public $rear; //隊尾指針
public $front;//對頭指針
public function __construct() {
$this->rear = null;
$this->front = null;
}
public function inset($str) {
$q = new Qnode;
$q->data = $str;
$q->next = null;
if($this->rear != null) $this->rear->next = $q;
$this->rear = $q;
if($this->front == null) $this->front = $q;
}
public function delete() {
if($this->front == null) {
echo "隊列已經空";
}
$this->front = $this->front->next;
if($this->front == null) $this->rear = null;
}
}