class stack():
__data=None
__next=None
__length=0
def push(self,v):
"""进栈"""
tmp=stack()
tmp.data=v
tmp.next=self.__next
self.__next=tmp
self.__length+=1
def pop(self):
"""出栈"""
tmp=self.__next
self.__next=tmp.next
self.__length-=1
return tmp.data
@property
def top(self):
"""栈顶元素"""
if self.__next:
return self.__next.data
return None
@property
def length(self):
"""栈的长度"""
return self.__length
s=stack()
s.push(100) #入栈
s.push(101) #入栈
print "当前栈的长度为:",s.length
print "当前出栈的元素为:",s.pop() #出栈
print "当前栈的长度为:",s.length
print "当前栈顶元素为:",s.top
print "当前出栈的元素为:",s.pop() #出栈
print "当前栈顶元素为:",s.top
转载于:https://www.cnblogs.com/JiangHuakey/archive/2011/12/03/2274735.html