1 Star 0 Fork 0

蓝桥云课/python-100

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
017-n_stacks.py 1016 Bytes
一键复制 编辑 原始数据 按行查看 历史
xiaoyi733112 提交于 2020-02-19 17:50 . python-100 answer
class Stacks(object):
def __init__(self, num_stacks, stack_size):
self.num_stacks = num_stacks
self.stack_size = stack_size
self.stack_pointers = [-1] * self.num_stacks
self.stack_array = [None] * self.num_stacks * self.stack_size
def abs_index(self, stack_index):
return stack_index * self.stack_size + self.stack_pointers[stack_index]
def push(self, stack_index, data):
if self.stack_pointers[stack_index] == self.stack_size - 1:
raise Exception('Stack is full')
self.stack_pointers[stack_index] += 1
array_index = self.abs_index(stack_index)
self.stack_array[array_index] = data
def pop(self, stack_index):
if self.stack_pointers[stack_index] == -1:
raise Exception('Stack is empty')
array_index = self.abs_index(stack_index)
data = self.stack_array[array_index]
self.stack_array[array_index] = None
self.stack_pointers[stack_index] -= 1
return data
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/lanqiao-courses/python-100.git
git@gitee.com:lanqiao-courses/python-100.git
lanqiao-courses
python-100
python-100
master

搜索帮助