南山区之狼 2021-05-08 08:11 采纳率: 48.1%
浏览 35
已采纳

Pyhon 自定义数据结构栈

  • 写回答

1条回答 默认 最新

  • 天元浪子 Python领域优质创作者 2021-05-08 09:20
    关注
    >>> class Stack:
    	def __init__(self, size=10):
    		self.__size = size
    		self.__content = list()
    		self.__current = 0
    	def isempty(self):
    		return self.__current == 0
    	def empty(self):
    		self.__content.clear()
    		self.__current = 0
    	def setSize(self, size):
    		if size < self.__cuttent:
    			self.__content = self.__content[:size]
    			self.__current = size
    		self.__size = size
    	def get(self):
    		if self.__current > 0:
    			self.__current -= 1
    			return self.__content.pop()
    		else:
    			raise RuntimeError('stack empty')
    	def push(self, data):
    		if self.__current < self.__size:
    			self.__content.append(data)
    			self.__current += 1
    		else:
    			raise RuntimeError('stack full')
    
    		
    >>> s = Stack()
    >>> s.isempty()
    True
    >>> s.push(3)
    >>> s.push('x')
    >>> s.isempty()
    False
    >>> s.get()
    'x'
    >>> s.get()
    3
    >>> s.get()
    Traceback (most recent call last):
      File "<pyshell#47>", line 1, in <module>
        s.get()
      File "<pyshell#39>", line 21, in get
        raise RuntimeError('stack empty')
    RuntimeError: stack empty
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度