zoukankan      html  css  js  c++  java
  • python中的栈

    
    # @File: stack
    
    
    # 列表实现栈
    class MyStack(object):
    
        def __init__(self):
            self.data = []
    
        def push(self, item):
            self.data.append(item)
    
        def pop(self):
            return self.data.pop()
    
        def top(self):
            return self.data[-1]
    
        def is_empty(self):
            return self.data == []
    
        def size(self):
            return len(self.data)
    
    
    stack = MyStack()
    stack.push(9)
    stack.push(9)
    print(stack)
    print(stack.top())
    print(stack.pop())
    print(stack)
    
    ---------------------------------------------------------------------------------
    
    import queue
    
    stack = queue.LifoQueue()  # 可看LifoQueue的源码,就是以上原理
    stack.put(1)
    stack.put(2)
    print(stack.get())
    print(stack.queue)
    print(stack.qsize())
    # stack.empty()  # 栈空
    # stack.full()  # 栈满
    
    
    
    ---------------------------------------------------------------------------------
    # 例子(括号匹配)
    def bracket_match(s):
        stack = []
        d = {"}": "{", ']': '[', ')': ')'}
        for ch in s:
            if ch in {'{', '[', '('}:
                stack.append(ch)
            elif len(stack) == 0:
                print('缺少左括号')
            elif d[ch] == stack[-1]:
                stack.pop()
            else:
                print('左右括号不匹配')
                return False
        if len(stack) > 0:
            print('缺少右括号')
            return False
        else:
            return True
    
    
    # print(bracket_match("[(])"))
  • 相关阅读:
    mysql 函数
    flanneld和calico
    k8s1.20.5
    pre
    docker与K8S源远
    Form Trigger Sequence Demo Form
    消息中间件RabbitMQ
    asp.net mvc4压缩混淆JavaScript
    C# 基础语法
    APP测试 理论总结
  • 原文地址:https://www.cnblogs.com/xiao-xue-di/p/10168570.html
Copyright © 2011-2022 走看看