Stack data structure in python

前端 未结 7 1467
轮回少年
轮回少年 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:45

    No need to jump through these loops, See 5.1.1 Using Lists as Stacks

    If you insist on having methods isEmpty() and push() you can do:

    class stack(list):
        def push(self, item):
            self.append(item)
        def isEmpty(self):
            return not self
    

提交回复
热议问题