zoukankan      html  css  js  c++  java
  • 《数据结构

    一:什么是栈?

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

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

    二:栈的基本定义?

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

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

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

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

    三:栈 - 顺序存储

    <?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());
  • 相关阅读:
    python之高阶函数
    [第二版]多线程的发送与接收
    基本函数与结构
    unp.h
    gdb调试命令
    System V共享内存区
    Posix 共享内存区
    System V信号量
    Posix 信号量
    记录锁
  • 原文地址:https://www.cnblogs.com/25-lH/p/10431189.html
Copyright © 2011-2022 走看看