To maintain a list of queue and stack

FIFO: Queue

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()
queue

Last-out: Stack

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()
Stack

 

Guess you like

Origin www.cnblogs.com/hbfengjun/p/12557003.html
Recommended