python学习数据结构实例代码
在学习python的过程中,用来练习代码,并且复习数据结构的
#coding:utf-8
#author:Elvis
classStack(object):
def__init__(self,size=8):
self.stack=[]
self.size=size
self.top=-1
defis_empty(self):
ifself.top==-1:
returnTrue
else:
returnFalse
defis_full(self):
ifself.top+1==self.size:
returnTrue
else:
returnFalse
defpush(self,data):
ifself.is_full():
raiseException('stackOverFlow')
else:
self.top+=1
self.stack.append(data)
defstack_pop(self):
ifself.is_empty():
raiseException('stackIsEmpty')
else:
self.top-=1
returnself.stack.pop()
defstack_top(self):
ifself.is_empty():
raiseException('stackIsEmpty')
else:
returnself.stack[self.top]
defshow(self):
printself.stack
stack=Stack()
stack.push(1)
stack.push(2)
stack.push('a')
stack.push('b')
stack.push(5)
stack.push(6)
stack.stack_pop()
stack.stack_pop()
stack.stack_top()
stack.is_empty()
stack.is_full()
stack.show()
以上所述就是本文给大家分享的全部内容了,希望大家能够喜欢。