python Stack栈实现

2019-04-29  本文已影响0人  sttech

采用队列实现栈

# Author:Alpha
#  python 使用队列实现数据结构Strock
import collections
#  从右侧开始添加数据
class Strock(object):
    def __init__(self):
        self.stack = collections.deque([])
        print(self.stack.__class__)
    def push(self,item):
        self.stack.append(item)

    def pop(self):
        if self.is_empty() :
            return
        return self.stack.pop()

    def top(self):
        if self.is_empty() :return
        return  self.stack[self.size() - 1]

    def size(self):
        return  len(self.stack)
    def clear(self):
        del self.stack

    def is_empty(self):
        return  self.size() < 1

    def __str__(self):
        return str(self.stack)
上一篇 下一篇

猜你喜欢

热点阅读