本文共 3796 字,大约阅读时间需要 12 分钟。
数据结构在计算机中组织存储,以便我们可以有效地访问和更改数据。 堆栈和队列是计算机科学中定义的最早的数据结构。
遵循后进先出 (Last-in-First-Out LIFO)原则。
遵循先入先出(FIFO:First-in-First-Out)原则。
Python的内置List数据结构k堆栈和队列操作的方法。
堆栈
letters = []# Let's push some letters into our listletters.append('c') letters.append('a') letters.append('t') letters.append('g')# Now let's pop letters, we should get 'g'last_item = letters.pop() print(last_item)# If we pop again we'll get 't'last_item = letters.pop() print(last_item)# 'c' and 'a' remainprint(letters) # ['c', 'a']
执行结果
gt['c', 'a']
队列
fruits = []# Let's enqueue some fruits into our listfruits.append('banana') fruits.append('grapes') fruits.append('mango') fruits.append('orange')# Now let's dequeue our fruits, we should get 'banana'first_item = fruits.pop(0) print(first_item)# If we dequeue again we'll get 'grapes'first_item = fruits.pop(0) print(first_item)# 'mango' and 'orange' remainprint(fruits) # ['c', 'a']
执行结果
bananagrapes['mango', 'orange']
deque是Double Ended Queue的缩写 - 可以获取存储的第一个或最后一个元素的通用队列,下面我们使用Deque库的堆栈和队列:
from collections import deque# you can initialize a deque with a list numbers = deque()# Use append like before to add elementsnumbers.append(99) numbers.append(15) numbers.append(82) numbers.append(50) numbers.append(47)# You can pop like a stacklast_item = numbers.pop() print(last_item) # 47 print(numbers) # deque([99, 15, 82, 50])# You can dequeue like a queuefirst_item = numbers.popleft() print(first_item) # 99 print(numbers) # deque([15, 82, 50])
执行结果
47deque([99, 15, 82, 50])99deque([15, 82, 50])
创建撤消功能 - 允许用户回溯他们的操作,直到会话开始。堆栈是这种情况的理想选择。 我们可以通过将其推送到堆栈来记录用户所采取的每个操作。 当用户想要撤消操作时,他们将从堆栈中弹出它。
游戏中,每次按下按钮,都会触发输入事件。 测试人员注意到,如果按钮按下得太快,游戏只处理第一个按钮,特殊动作将无效!可以使用队列修复它。 我们可以将所有输入事件排入队列。
#!/usr/bin/python3# -*- coding: utf-8 -*-# 项目实战讨论QQ群630011153 144081101# python测试开发库汇总: https://github.com/china-testing/python-api-tesing/# 本文最佳板式地址: https://www.jianshu.com/p/c990427ca608# A simple class stack that only allows pop and push operationsclass Stack: def __init__(self): self.stack = [] def pop(self): if len(self.stack) < 1: return None return self.stack.pop() def push(self, item): self.stack.append(item) def size(self): return len(self.stack)# And a queue that only has enqueue and dequeue operationsclass Queue: def __init__(self): self.queue = [] def enqueue(self, item): self.queue.append(item) def dequeue(self): if len(self.queue) < 1: return None return self.queue.pop(0) def size(self): return len(self.queue) document_actions = Stack()# The first enters the title of the documentdocument_actions.push('action: enter; text_id: 1; text: This is my favourite document') # Next they center the textdocument_actions.push('action: format; text_id: 1; alignment: center') # As with most writers, the user is unhappy with the first draft and undoes the center alignmentdocument_actions.pop() # The title is better on the left with bold fontdocument_actions.push('action: format; text_id: 1; style: bold') input_queue = Queue()# The player wants to get the upper hand so pressing the right combination of buttons quicklyinput_queue.enqueue('DOWN') input_queue.enqueue('RIGHT') input_queue.enqueue('B')# Now we can process each item in the queue by dequeueing themkey_pressed = input_queue.dequeue() # 'DOWN'# We'll probably change our player positionkey_pressed = input_queue.dequeue() # 'RIGHT'# We'll change the player's position again and keep track of a potential special move to performkey_pressed = input_queue.dequeue() # 'B'# This can do the act, but the game's logic will know to do the special move
转载地址:http://hzfna.baihongyu.com/