python实用小代码


栈的实现

 

#!/usr/bin/env python 
#coding=utf-8 
#python version 2.7.4
class stack:
    def __init__(self,list=None):
        self.contain = list 
        self.msize=100;
        self.top = 0;
    def getTop(self):
        if(self.top>0):
            return self.contain[self.top-1]
        else:
            return None
    def getLength(self):
        return len(contain);
    def push(self,str):
        if(self.top==self.msize):
            return -1
        self.contain.append(str)
        self.top=self.top +1
    def pop(self):
        try:
            res=self.contain.pop()
            return res;
        except IndexError:
            return None
        


li = [1,'51','15']
st = stack(li)
va='5'
print st.pop()
print st.pop()
st.push('fdef')
print st.pop()
print st.pop()


 










 

转载于:https://www.cnblogs.com/dyllove98/p/3157138.html

你可能感兴趣的:(python实用小代码)