chapter_stack_and_queue/queue/ #149
Replies: 47 comments 49 replies
-
针对Java的Queue类补充一些知识点:
队列除了基本的 Collection 操作外,还提供特有的插入、提取和检查操作(如上)。每个方法都存在两种形式:一种抛出异常(操作失败时),另一种返回一个特殊值(null 或 false,具体取决于操作)。 |
Beta Was this translation helpful? Give feedback.
-
循环数组 -🐂的 |
Beta Was this translation helpful? Give feedback.
-
pop = que.popleft() 这里python的实现应该是pop = que.pop(0),3.9.0版本无popleft用法,pooleft是用于collections中的deque对象 |
Beta Was this translation helpful? Give feedback.
-
队列一章中,包括双向队列,有些地方入队用 push , 有些地方入队用 offer,建议统一一下使文章更严谨。比如统一使用 Java 中的用词 offer |
Beta Was this translation helpful? Give feedback.
-
class typing.Deque 3.9 版后已移除,这个类型注解会导致程序报错 import typing
import collections
que: Deque[int] = collections.deque()
-------------------------------------------------
NameError: name 'Deque' is not defined |
Beta Was this translation helpful? Give feedback.
-
打卡,手敲了一遍链表和循环数组实现队列,不过我好像只能跟着敲。 |
Beta Was this translation helpful? Give feedback.
-
基于链表实现的队列进行入队操作,如果队列不为空,则将该节点添加到尾节点后中的else语句里这两句不理解 rear.next = node; rear = node;,不应该只用rear.next = node;就行了嘛 |
Beta Was this translation helpful? Give feedback.
-
问题:执行到 rear.next = node;(1处)这里后,当此时push的是2的时候,real.next.val = 2这个很好理解,为什么front.next.val = 2 也会赋值呢?在push的时候,又没有写front.next = node; 这里我不太理解~
/* 入队 */ |
Beta Was this translation helpful? Give feedback.
-
用数组实现队列的三张演示图中的第二张,也就是push操作,并没有提及rear自增1的操作:
|
Beta Was this translation helpful? Give feedback.
-
问题:环形数组队列的有效长度不应该是Maxsize=len(self.__nums)-1 |
Beta Was this translation helpful? Give feedback.
-
res = [0] * self.size()这种初始化队列的方式,会导致res 的每个元素引用相同的地址吗,就是如果我改动res[0]=1,那么res[1]、res[2]、...会不会因为存放的是相同的地址,导致都变为1 |
Beta Was this translation helpful? Give feedback.
-
在5.2.2的第1节基于链表的实现,C语言代码的打印队列函数中,for循环语句条件queue->front != queue->rear是用来检测队列是否为空吗?但是如果队列中只有一个元素,好像就会打印不出那个单独的元素 |
Beta Was this translation helpful? Give feedback.
-
环形数组队列扩容 /* 入队 */
push(num) {
if (this.size === this.capacity) {
this.extendCapacity();
}
// ...
}
/* 扩容 */
extendCapacity() {
const extraSize = Math.round(this.capacity * 0.5);
const arr = new Array(extraSize);
this.#nums = this.toArray().concat(...arr);
this.#front = 0;
this.capacity += extraSize;
} |
Beta Was this translation helpful? Give feedback.
-
c++链表实现的队列,入队push()那里我有两个疑问: |
Beta Was this translation helpful? Give feedback.
-
/* 出队 */ |
Beta Was this translation helpful? Give feedback.
-
perfect! |
Beta Was this translation helpful? Give feedback.
-
作者你好, class LinkedListQueue:
"""基于链表实现的队列"""
def __init__(self):
"""构造方法"""
self._front: ListNode | None = None # 头节点 front
self._rear: ListNode | None = None # 尾节点 rear
self._size: int = 0 请问上面代码的self._front: ListNode | None = None 这个代码怎么理解啊,ListNode是个什么类型,为什么在下面的push(num)方法中可以调用ListNode(num)? |
Beta Was this translation helpful? Give feedback.
-
请问js链表队列删除头节点后,什么时候会将头节点的内存释放掉?这里我理解只是改变类中#front的引用,而之前的头节点并未释放。 pop() {
const num = this.peek();
// 删除头节点
this.#front = this.#front.next;
this.#queSize--;
return num;
} |
Beta Was this translation helpful? Give feedback.
-
太精妙了,算法的魅力! |
Beta Was this translation helpful? Give feedback.
-
基于数组实现队列的C代码中第五行定义queSize的注释是不是错了?queSize应该是队列长度而不是尾指针吧? |
Beta Was this translation helpful? Give feedback.
-
public class ArrayQueue {
} |
Beta Was this translation helpful? Give feedback.
-
为什么基于链表实现访问队列队首元素时要考虑长度不为0的同时,还要考虑头节点是否为空;而访问栈顶元素时不要考虑顶部节点为空;我认为访问队首元素时,只需要考虑该队列是否为空,而判断是否为空的函数isempty也是基于队列元素个数是否为0判断的;所以我认为应该不需要判断头节点是否为空也无大碍。 |
Beta Was this translation helpful? Give feedback.
-
public class LinkQueue {
} |
Beta Was this translation helpful? Give feedback.
-
作者你好,数组实现队列的 js 代码 array_queue.js 类 LinkedListQueue 的方法 get size 命名有点问题,应该是驼峰的。
|
Beta Was this translation helpful? Give feedback.
-
freeMemoryLinkedList(front)这个是内置函数还是什么呀?怎么没有看到这个函数的实现? |
Beta Was this translation helpful? Give feedback.
-
基于数组的队列,扩容方法: /* 扩容队列 */
private void extendCapacity() {
// 新建一个长度为原数组 extendRatio 倍的新数组,并将原数组复制到新数组
nums = Arrays.copyOf(toArray(), capacity() * extendRatio);
front = 0;
} |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
这个用“环形数组”实现队列真的太巧妙了,各种操作刚好保证了下标不会溢出,而且数据在数组里都是连续存储的,也不会被覆盖或者打印了内存里的垃圾值(如 python 里默认给的0,C 里该地址上的随机值……) |
Beta Was this translation helpful? Give feedback.
-
chapter_stack_and_queue/queue/
一本动画图解、能运行、可提问的数据结构与算法入门书
https://www.hello-algo.com/chapter_stack_and_queue/queue/
Beta Was this translation helpful? Give feedback.
All reactions