用类写栈和队列

class FooStack(object):

    def __init__(self):
        self.FooList=[]

    def push(self,num):
        if isinstance(num,list):
            self.FooList.extend(num)
        else:
            self.FooList.append(num)

    def singlepop(self,number=1):
        ret=self.FooList[-number:]
        for i in range(0,number):
            item=ret.pop()
            yield item

    def popall(self):
       return self.singlepop(len(self.FooList))

    def is_null(self):
        if self.FooList==[]:
            return True
        else:
            return False




text=FooStack()
text.push([1,2,3,4,5,6])
text.push([7,8,9,10])
ret=text.popall()

for item in ret:
    print(item)

 

 

队列

class FooStack(object):

    def __init__(self):
        self.FooList=[]

    def push(self,num):
        if isinstance(num,list):
            self.FooList.extend(num)
        else:
            self.FooList.append(num)

    def singlepop(self,number=1):
        ret=self.FooList[:number]
        for i in range(0,number):
            item=ret[i]
            yield item

    def popall(self):
       return self.singlepop(len(self.FooList))

    def is_null(self):
        if self.FooList==[]:
            return True
        else:
            return False




text=FooStack()
text.push([1,2,3,4,5,6])
text.push([7,8,9,10])
ret=text.popall()

for item in ret:
    print(item)

 

posted @ 2018-08-01 00:25  R00M  阅读(149)  评论(0编辑  收藏  举报