• PHP 堆栈和队列


    <?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;

    }

    }

  • 相关阅读:
    Spire.PDF
    C# 下载PDF文件(http与ftp)
    7、软件复用 软件技术系列文章
    15、博客飘叶效果 博客界面改造文章(202211)
    Windows 11 ISO原版镜像文件下载(2022年11月)
    公司管人 我的闪存
    Windows 10 ISO原版镜像文件下载(2022年11月)
    回收站管理器 开源研究系列文章
    2、编码规范 软件技术系列文章
    最值得收藏的 Web 开发资源荟萃
  • 原文地址:https://www.cnblogs.com/phplhs/p/5504017.html
Copyright © 2020-2023  润新知