博客
关于我
栈和队列算法
阅读量:774 次
发布时间:2019-03-24

本文共 2540 字,大约阅读时间需要 8 分钟。

PHP堆栈实现
1、实现一个MinStack
                            #pragma once                <?php                #define MAX_VALUE 100                typedef struct MinStack {                    int array[MAX_VALUE];                    int top;                } MinStack;                void Init(MinStack *pMs) {                    pMs->top = 0;                }                void Push(MinStack *pMs, int data) {                    int min = data;                    if (pMs->top != 0 && pMs->array[pMs->top - 1] < min) {                        min = pMs->array[pMs->top - 1];                    }                    pMs->array[pMs->top++] = data;                    pMs->array[pMs->top++] = min;                }                void Pop(MinStack *pMs) {                    pMs->top -= 2;                }                int Min(MinStack *pMs) {                    return pMs->array[pMs->top - 1];                }                int Top(MinStack *pMs) {                    return pMs->array[pMs->top - 2];                }                void test1() {                    MinStack ms;                    Init(&ms);                    $arr = array(3, 2, 5, 6, 8, 3, 1, 9);                    foreach ($arr as $i => $val) {                        Push(&ms, $val);                    }                    echo Min(&ms) . " ";                    Pop(&ms);                    echo Min(&ms) . " ";                    Pop(&ms);                    echo Min(&ms) . " ";                }                    
2、改进方法
方法1:使用一个栈实现交叉存放数据
方法2:使用两个栈实现更高效的操作
3、其他技术问题
使用两个栈实现队列
            <?php            class Solution {                private Stack stack1;                private Stack stack2;                public void push(int node) {                    stack1.push($node);                }                public int pop() {                    if (stack2.isEmpty()) {                        while (!stack1.isEmpty()) {                            stack2.push(stack1.pop());                        }                    }                    return stack2.pop();                }            }            </?php        
4、字符串数组运算示例
            $str = array("2", "4", "+", "9", "*");            $result = Solution::evalRPN($str);            var_dump($result);        

转载地址:http://udlkk.baihongyu.com/

你可能感兴趣的文章
Redis 配置文件redis.conf详细解释
查看>>
python网络爬虫(2)——scrapy框架的基础使用
查看>>
python网络爬虫实例教程试读_Python网络爬虫实战教程(全套完整版) - 学途无忧网 - 做技术的王者 - Powered By EduSoho...
查看>>
Python 使用哈希函数用于加密
查看>>
Python 依赖管理的革新——Poetry 深度解析
查看>>
python 保留精度及增加去除数字的千位分隔符(金额化数字)
查看>>
python 倒计时 9,8,7,。。。。。。0
查看>>
Python 入门开发学习笔记之数据的增删改查
查看>>
Python 入门教程(2)搭建环境 2.4、VSCode配置Node.js运行环境
查看>>
Python 八大排序算法合集
查看>>
python 关于epoll的学习
查看>>
Python 内存管理
查看>>
Python 内嵌函数:它们有什么用处?
查看>>
Python 内置 sum 函数 vs. for 循环性能
查看>>
python 内置slice的用法
查看>>
Python 内置时间模块
查看>>
python 内部如何实现命名元组?
查看>>
Python 写Android App性能:入门到高级
查看>>
python 写入json数据到数据库
查看>>
Python 出现 TypeError: ‘encoding‘ is an invalid keyword argument for this function 解决方法
查看>>