Stack data structure in python

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

    Here is an example for stack class

    class Stack(object):
    
       def __init__(self):
          self.items = []
    
       def push(self, item):
          self.items.append(item)
    
       def pop(self):
           return self.items.pop()
    
       def peek(self):
           return self.items[-1]
    
       def isEmpty(self):
           return len(self.items) == 0
    

提交回复
热议问题