• 《数据结构


    一:什么是栈?

      - 栈是只允许表尾进行操作的线性表。

      - 栈又称为先进后出线性表

    二:栈的基本定义?

      -  允许插入/删除的一端称为 栈顶,另一端称为栈底

      -  不含任何数据元素的栈称为空栈

      -  栈的插入操作,也叫进栈/压栈/入栈

      -  栈的删除操作,也叫出栈/弹栈

    三:栈 - 顺序存储

    <?php
    /**
     *    栈的定义:栈是只允许在表尾进行操作的线性表
     *    栈的顺序存储结构的实现,只实现了最主要的进,出操作。
     *    顺序结构即由一段连续的空间存储,即数组实现
     */
    class Stack
    {
        const  MAXSIZE = 20; // 存储栈长度
        public $top;  // 栈位置
        public $data; //// 入栈
        public function push($elem)
        {
            if($this->top == self::MAXSIZE - 1) {
                return false;
            }
            $this->top++;
            $this->data[$this->top] = $elem;
            return true;
        }
    
        // 出栈
        public function pop()
        {
            if($this->top == -1) {
                return false;
            }
            $elem = $this->data[$this->top];
            $this->top--;
            return $elem;
        }
    }
    
    $stack = new Stack();
    $stack->push(1);
    $stack->push(2);
    $stack->push(3);
    $stack->push(4);
    
    print_r($stack->pop());
  • 相关阅读:
    [Leetcode] ZigZag Conversion
    [Leetcode] Wildcard Matching
    [Leetcode] 4Sum
    [Leetcode] Word Break II
    [Leetcode] Best Time to Buy and Sell Stock III
    [Leetcode] Permutation Sequence
    [Leetcode] Surrounded Regions
    [Jobdu] 题目1522:包含min函数的栈
    CUDA2.1-原理之索引与warp
    opencv8-GPU之相似性计算
  • 原文地址:https://www.cnblogs.com/25-lH/p/10431189.html
Copyright © 2020-2023  润新知