chap11 stack

class Stack:
    initial_capacity=10
    def __init__(self):
        self.A=[0]*self.initial_capacity
        self.top=0
        self.capacity=self.initial_capacity
    def expand(self):
        self.A.extend([0]*self.capacity)
        self.capacity*=2
    def empty(self):
        if self.top==0:
            return True
        else: return False
    def push(self,x):
        if self.top>=self.capacity:
            self.expand()
        self.A[self.top]=x
        self.top+=1
    def pop(self):
        assert self.top!=0
        self.top-=1
        return self.A[self.top]
    def __repr__(self):
        return ' '.join([str(self.A[i]) for i in range(0,self.top)])
stk=Stack()
for i in range(0,10):
    stk.push(i)
print(stk)
while stk.empty()!=True:
    print(stk)
    stk.pop()

    
        
        

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章