博客
关于我
python 之栈的实现
阅读量:797 次
发布时间:2023-03-06

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

#!/usr/bin/env python# ---------------------------------------# author : Geng Jie# email  : gengjie@outlook.com# Create Time: 2016/3/16 23:38# ----------------------------------------class Node:    def __init__(self, value):        self.value = value        self.next = Noneclass Stack:    def __init__(self):        self.top = None    def push(self, value):        node = Node(value)        node.next = self.top        self.top = node    def pop(self):        node = self.top        self.top = node.next        return node.valueif __name__ == '__main__':    stack = Stack()    # for i in range(10):    #     stack.push(i)    #     # while stack.top:    #     #     print(stack.pop())    exp = '{a * [x/(x+y)]}'    for c in exp:        if c in '{[(':            stack.push(c)        elif c in '}])':            v = stack.top.value            if c == '}' and v != '{':                raise Exception('failed')            if c == ']' and v != '[':                raise Exception('failed')            if c == ')' and v != '(':                raise Exception('failed')            stack.pop()    if stack.top is not None:        raise Exception('failed')    print('ok')

  

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

你可能感兴趣的文章
Python 垃圾收集器文档
查看>>
python 基于 wordcloud + jieba + matplotlib 生成词云
查看>>
python 基于detectron或mask_rcnn的mask遮罩区域进行图片截取
查看>>
Python编程:Tkinter图形界面设计(2)
查看>>
Python 基础 - Day 5 Learning Note - 模块 之 标准库:time (1)
查看>>
Python 基础一
查看>>
python 基础操作知识整理总结
查看>>
Python 基础知识点整理与分享,期盼你的交流!
查看>>
python 基础第七篇
查看>>
Python编程:Tkinter图形界面设计(1)
查看>>
Python 基础语法:None
查看>>
Python 基础语法:基本数据类型(一)
查看>>
python编程读取写入excel_Python读取txt内容写入xls格式excel中的方法
查看>>
Python 基础语法:基本数据类型(字典)
查看>>
Python 基础语法:基本数据类型(字符串)
查看>>
Python 基础语法:基本数据类型(集合)
查看>>
Python编程的终极十大工具
查看>>
python 堆排序
查看>>
python编程方式之一-函数式编程
查看>>
python 复习计划
查看>>