列表维护成队列和栈

先进先出:队列

class Queue(object):
    def __init__(self):
        self.q = []

    def push(self, v):
        self.q.append(v)

    def pop(self):
        try:
            l=self.q.pop(0)
            print(l)
        except IndexError:
            print("pop from empty list")

q = Queue()
q.push('11')
q.push('22')
q.push('33')
q.push('44')

print(q.q)

q.pop()
q.pop()
q.pop()
q.pop()
q.pop()
队列

先进后出:栈

class Stack(object):
    def __init__(self):
        self.list = []

    def push(self, v):
        self.list.append(v)

    def pop(self):
        try:
            l=self.list.pop()
            print(l)
        except IndexError:
            print('pop from empty list')

s=Stack()
s.push('11')
s.push('22')
s.push('33')

print(s.list)

s.pop()
s.pop()
s.pop()

猜你喜欢

转载自www.cnblogs.com/hbfengjun/p/12557003.html