Stack data structure in python

前端 未结 7 1591
轮回少年
轮回少年 2021-02-02 09:24

I have 2 issues with the code below:

  1. push(o) throws an exception TypeError: can only assign an iterable.
  2. Should I throw an exception if pop()

7条回答
  •  我在风中等你
    2021-02-02 09:40

    class Stack:
        def __init__(self):
            self.stack = []
        def pop(self):
            if self.is_empty():
                return None
            else:
                return self.stack.pop()
        def push(self, d):
            return self.stack.append(d)
        def peek(self):
            if self.is_empty():
                return None
            else:
                return self.stack[-1]
        def size(self):
            return len(self.stack)
        def is_empty(self):
            return self.size() == 0
    

提交回复
热议问题