Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 修改ArrayStack实现,使堆栈’;s的容量仅限于maxlen_Python_Data Structures - Fatal编程技术网

Python 修改ArrayStack实现,使堆栈’;s的容量仅限于maxlen

Python 修改ArrayStack实现,使堆栈’;s的容量仅限于maxlen,python,data-structures,Python,Data Structures,我需要帮助理解此任务: 修改ArrayStack实现,使堆栈的容量限制为maxlen元素,其中maxlen是构造函数的可选参数(默认为无)。如果在堆栈满容量时调用push,则抛出一个完全异常(定义类似于Empty) 这就是我在摆弄的东西 from exceptions import Empty from exceptions import Full class ArrayStack: def __init__(self): self._data = []

我需要帮助理解此任务:

修改ArrayStack实现,使堆栈的容量限制为maxlen元素,其中maxlen是构造函数的可选参数(默认为无)。如果在堆栈满容量时调用push,则抛出一个完全异常(定义类似于Empty)

这就是我在摆弄的东西

from exceptions import Empty
from exceptions import Full

class ArrayStack:

    def __init__(self):
        self._data = []                       # nonpublic list instance

    def __len__(self):
        return len(self._data)

    def is_empty(self):
        return len(self._data) == 0

    def is_full(self):
        return len(self.data) == n-1

    def push(self, e):
        self._data.append(e)                  # new item stored at end of list
        if self.is_full():
            raise Full('Stack is full')
        return self._data[n-1]

    def top(self):
        if self.is_empty():
            raise Empty('Stack is empty')
        return self._data[-1]                

    def pop(self):
        if self.is_empty():
            raise Empty('Stack is empty')
        return self._data.pop()

我希望您知道代码中的前两条语句会引发错误。您需要声明您的异常,我建议您查看

您可以在
\uuuu init\uuuu
中设置
maxlen

def __init__(self, maxlen=None):
    self.maxlen=maxlen

现在,
已满
需要修改,以根据
self.maxlen
None
还是一个数字采取不同的行动。如果
None
则堆栈将永远无法填充(至少在理论上是如此)。

请包括一个特定的问题-您的代码是产生错误还是输出错误?