列表可以做队列可以做的所有事情 [英] Lists can do everything that queues can

查看:93
本文介绍了列表可以做队列可以做的所有事情的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们知道,列表可以视为队列的超集(不要直译字面意思).像在list中一样,当Queue是list的专用版本时,我们可以从任何地方插入,删除元素(您也可以使用其他数据结构实现queue).那么为什么我们不直接使用列表而不是队列呢?

As we know, list can be treated as superset of queue(don''t go on literal meaning). Like in list we can insert, delete element from anyplace while Queue is the specialized version of list( u can also implement queue using other data structure too). SO why don''t we directly use list instead of queue.

推荐答案

因为API不应允许超出预期用途的任何东西.如果我将一个函数放到一个API中,该API返回一个列表,结果实际上是一个队列,那么调用该函数的程序员可能会认为可以在顶部添加元素. API文档是否对此提出警告无关紧要-您不能假设其他人也会阅读它.
Because an API should not allow anything beyond the intended use. If I were to put a function into an API that returns a list as a result that in truth is a queue, then programmers calling that function might assume it''s ok to add elements at the top. Whether or not your API documentation warns against that is irrelevant - you can''t assume others will read it.


这篇关于列表可以做队列可以做的所有事情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆