我在下面写了一个堆栈。
现在我想添加一个允许你指定堆栈最大大小的函数或方法。
我需要更改或添加什么?
class MyStack:
def __init__(self):
self.myList = []
self.top = -1
self.size = 0
def isEmpty(self):
if (self.size > 0):
return False
else:
return True
def push(self, item):
self.myList.append(item)
self.size = self.size + 1
self.top = self.top + 1
def pop(self):
if self.isEmpty():
return None
else:
self.ret = self.myList.pop(self.top)
self.size = self.top - 1
self.top = self.top - 1
return self.ret
def peek(self):
if self.isEmpty():
return None
else:
return self.myList[self.top]
答案 0 :(得分:-1)
更改初始化函数以适应最大尺寸
<div class="delimiter">
<form>
<ul>
<li><label><input name="stage" type="radio" onclick="activate(this)" value="stage1" checked>Stage 1</label></li>
<li><label><input onclick="activate(this)" name="stage" type="radio" value="stage2">Stage 2</label></li>
<li><label><input onclick="activate(this)" name="stage" type="radio" value="stage3">Stage 3</label></li>
<li><label><input onclick="activate(this)" name="stage" type="radio" value="stage4">Stage 3</label></li>
</ul>
</form>
<div class="stage1-row active">
Stage 1 content
</div>
<div class="stage2-row">
Stage 2 content
</div>
<div class="stage3-row">
Stage 3 content
</div>
<div class="stage4-row">
Stage 4 content
</div>
</div>
同时修改推送功能:
def __init__(self,maxsize):
self.myList = []
self.top = -1
self.size = 0
#ADDED
self.max_size=maxsize
现在可以看出:
def push(self, item):
#check if you are exceeding the limit
if(len(self.myList)<self.max_size):
self.myList.append(item)
self.size = self.size + 1
self.top = self.top + 1
else:
print("Stack full")