当前位置: 首页 > news >正文

算法学习day16----Python数据结构--模拟队列

久违的更新,主包也是终于回到家里了

输入样例

10
push 6
empty
query
pop
empty
push 3
push 4
pop
query
push 6

输出样例

NO
6
YES
4

解决代码(今天没写笔记了,题目比较简单,可以仿写之前写过的模拟栈)

class Queue:def __init__(self):self.queue = []def push(self, x):self.queue.append(x)def pop(self):if not self.queue:return "queue is empty"return self.queue.pop(0) #头部弹出def query(self):if not self.queue:return "queue is empty"return self.queue[0]def is_empty(self):return "YES" if not self.queue else "NO"def main():import sysinput = sys.stdin.readline  m = int(input().strip())    queue = Queue()output = []for _ in range(m):          parts = input().split() if not parts:continue  # 跳过空行op = parts[0]if op == 'push':if len(parts) < 2:output.append("error: missing value")continuetry:x = int(parts[1])queue.push(x)except ValueError:output.append("error: invalid number")elif op == 'pop':res = queue.pop()if res == "queue is empty":output.append(res)#  pop 成功,不输出elif op == 'query':res = queue.query()output.append(str(res))elif op == 'empty':output.append(queue.is_empty())print('\n'.join(output))  # 统一输出结果if __name__ == "__main__":main()

算是一个回顾题吧


文章转载自:
http://carnally.zzyjnl.cn
http://attrited.zzyjnl.cn
http://alta.zzyjnl.cn
http://bourtree.zzyjnl.cn
http://badge.zzyjnl.cn
http://building.zzyjnl.cn
http://bondieuserie.zzyjnl.cn
http://berley.zzyjnl.cn
http://balance.zzyjnl.cn
http://actinicity.zzyjnl.cn
http://boaster.zzyjnl.cn
http://acronym.zzyjnl.cn
http://bengalee.zzyjnl.cn
http://brandling.zzyjnl.cn
http://barium.zzyjnl.cn
http://carmelita.zzyjnl.cn
http://barbitone.zzyjnl.cn
http://attitudinize.zzyjnl.cn
http://basification.zzyjnl.cn
http://bitten.zzyjnl.cn
http://auriga.zzyjnl.cn
http://bilbao.zzyjnl.cn
http://chaperonage.zzyjnl.cn
http://anthracitic.zzyjnl.cn
http://aep.zzyjnl.cn
http://cheeseparing.zzyjnl.cn
http://autosemantic.zzyjnl.cn
http://bouffe.zzyjnl.cn
http://antespring.zzyjnl.cn
http://can.zzyjnl.cn
http://www.dtcms.com/a/281174.html

相关文章:

  • 传统三层网络架构和现代数据中心网络架构(如思科 ACI 的 Spine-Leaf 架构)的对比和分析(Grok3 回答)
  • Mac电脑上无需卸载即可切换使用多个EasyConnect客户端的解决方案
  • Docker安装升级redis,并设置持久化
  • 使用 sudo iftop -i 分析服务器带宽使用情况
  • 一文读懂语义解析技术:从规则到神经网络的演进与挑战
  • PPP 链路及 MP 捆绑与 CHAP 验证实验
  • 从零开始学前端html篇3
  • Missing classes detected while running R8解决
  • 创客匠人:从 IP 到变现,定位是构建价值闭环的核心
  • Elasticsearch的深度翻页问题
  • Git本地操作完全指南:从入门到精通
  • 创客匠人:创始人 IP 打造,知识变现的时代必然
  • Elasticsearch 9.x 搜索执行流程(源码解读)
  • 深度学习中的激活函数:从原理到 PyTorch 实战
  • CentOS服务器安装Supervisor使队列可以在后台运行
  • 【用unity实现100个游戏之33】用Unity手搓一个类【红色警戒|魔兽争霸|帝国时代|星际争霸】3D RTS游戏(附源码)
  • vue openlayer创建地图弹框overlay
  • 【html常见页面布局】
  • [ROS 系列学习教程] ROS动作通讯(Action):通信模型、Hello World与拓展
  • k8s环境使用Operator部署Seaweedfs集群(下)
  • 【鸿蒙HarmonyOS】鸿蒙app开发入门到实战教程(三):实现一个音乐列表的页面
  • Flutter Socket 连接方案分析与适用场景
  • RestTemplate 实现后端 HTTP 调用详解
  • spring-ai-alibaba 多模态之音频
  • 前端Vue.js面试题(4)
  • 超详细 anji-captcha滑块验证springboot+uniapp微信小程序前后端组合
  • 如何定义一个只能在堆上或栈上生成对象的类
  • Python初学者笔记第十二期 -- (集合与字典编程练习题)
  • U-Boot 中增加 GIC-400中断服务程序
  • Copula理论:覆盖相关性分析、极值相依性、回归建模、时间序列预测、贝叶斯网络,R/Python双语言实现+AI编程辅助(科研绘图与结果呈现)