Python模拟队列

2021/9/23 22:11:30

本文主要是介绍Python模拟队列,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

# 创建队列
q = []
maxSize,front,rear = 100,0,0
# 占位
for i in range(maxSize):
    q.append(0)

# 这两个函数我承认我是从我写的C++函数改的
# 进队
def push(value):
    global rear,q
    # 如果队不满则添加值 
    if(rear < maxSize):
        q[rear] = value
        rear += 1

# 出队
def pop():
    global front
    # 如果队首不为队尾(即不为空),则front+1
    if(front != rear):
        front += 1

# 啥也没干
print('q:',q)
print('rear: %d\tfront: %d' % (rear,front))

# 先入队
for i in range(1,6):
    push(i)
print('q:',q)
print('rear: %d\tfront: %d' % (rear,front))

# 再出队
pop()
print('q:',q)
print('rear: %d\tfront: %d' % (rear,front))

q: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
rear: 0	front: 0
q: [1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
rear: 5	front: 0
q: [1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
rear: 5	front: 1



这篇关于Python模拟队列的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程