class Stack(object):
def __init__(self):
self.pTop = None
self.pBottom = None
class Node(object):
def __init__(self, data=None, pNext=None):
self.data = data
self.pNext = pNext
def pushStack(s, pNew):
pNew.pNext = s.pTop
s.pTop = pNew
def popStack(s):
cur = s.pTop
while cur != s.pBottom:
s.pTop = cur.pNext
print("出栈的元素是:%s" % cur.data)
cur = cur.pNext
else:
print("出栈失败")
def showAll(s):
cur = s.pTop
while cur != s.pBottom:
print("元素是:%s" % cur.data)
cur = cur.pNext
def isEmpty(s):
if s.pTop == s.pBottom:
print("the stack is empty")
return True
return False
def clearStack(s):
"""
和pop的区别在于,pop让python来清空引用计数为0的数据
clear要我们手动清空
:param s:
:return:
"""
if isEmpty(s):
return None
cur = s.pTop
q = None
while cur != s.pBottom:
q = cur.pNext
del cur
cur = q
else:
s.pBottom = s.pTop
s = Stack()
p = Node()
s.pTop = s.pBottom = p
n1 = Node(4)
pushStack(s, n1)
n2 = Node(5)
pushStack(s, n2)
n3 = Node(6)
pushStack(s, n3)
n4 = Node(2)
pushStack(s, n4)
print("**********遍历元素***************")
showAll(s)
# print("************出栈*****************")
# popStack(s)
print("*************清空栈*************")
clearStack(s)