用户:
丁仪awa查看:0 回复:0 评论:0 创建时间:2022-02-20T20:03:04
【作品展示】

【作品介绍】
一个栈,”短小精悍“
【作品源代码】
class Stack:
__stack = []
capacity = 0
Stack_to_List = []
List_to_Stack = []
def __init__(self, capacity):
self.capacity = capacity
print("栈", self, "初始化成功!")
def Enter(self, x):
if len(self.__stack) < self.capacity:
self.__stack.insert(0, x)
print("栈", self, "添加项目", x, "成功!")
else:
print("栈", self, "已满!")
def Out(self):
out = self.__stack[0]
del self.__stack[0]
print("栈", self, "取出元素", out, "成功!")
def List_to_Stack(self, List):
if self.capacity < len(self.__stack) + len(List):
self.Stack_to_List = List.copy().reverse()
self.__stack = self.Stack_to_List
print("栈", self, "添加元素", self.List_to_Stack, "成功!")
else:
print("栈", self, "已满!")
def Stack_to_List(self, destination):
self.Stack_to_List = self.__stack.copy
destination = self.Stack_to_List.copy
print("栈", self, "复制完成!")
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn