马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
因为是纯0000000基础,以为栈和图示一个意思,
先要有几个初始栏位,然后填充内容。
本来也想append和pop,后来考虑栏位问题,没有值的位置用“”代替,
于是代码就变得有点麻(丢)烦(人)了。。。
class Stack:
def __init__(self):
self.stack = ["","","","","e"]
self.length = len(self.stack)
def now(self):
for each in self.stack:
print(each)
def isEmpty(self, default = True):
while 1:
for each in self.stack:
if each:
return False
break
return True
def top(self):
for i in range(self.length):
if self.stack[i]:
print("%s" %self.stack[i])
break
def base(self):
print("%s" %(self.stack[self.length - 1] if self.stack[self.length - 1] != "" else "none" ))
def push(self, a):
self.a = a
if self.stack[0]:
print("Enough! No more input!")
elif self.isEmpty():
self.stack[self.length - 1] = self.a
else:
for i in range(self.length):
if self.stack[i]:
self.stack[i-1] = self.a
break
def pop(self):
if self.isEmpty():
print("Empty! No more pop!")
for i in range(self.length):
if self.stack[i]:
print(self.stack[i])
self.stack[i] = ""
break
然后我的问题来了, “栈” 是没有定义初始长度的? 完全是根据输入来调节的? |