chapter_stack_and_queue/deque/ #140
Replies: 55 comments 82 replies
-
我想问一下K神,双向队列的具体操作体现在哪呢,我看大话数据结构中没有讲到这个,有点好奇它的实际应用,因为我细看了这一章节,更像是两个栈拼接在了一起 |
Beta Was this translation helpful? Give feedback.
-
// 当 i 越过数组头部后,回到尾部 |
Beta Was this translation helpful? Give feedback.
-
array_deque 队尾出队 popLast 方法不需要重设 rear 指针吗? |
Beta Was this translation helpful? Give feedback.
-
k神,基于双向链表的实现的图里,step4的popLast应该pop掉5吧? |
Beta Was this translation helpful? Give feedback.
-
def pop(self, is_front: bool) -> int: |
Beta Was this translation helpful? Give feedback.
-
我想请问一下这部分代码: |
Beta Was this translation helpful? Give feedback.
-
感觉这个地方会出bug啊 |
Beta Was this translation helpful? Give feedback.
-
/* 双向链表节点 */
class ListNode {
var val: Int // 节点值
var next: ListNode? // 后继节点引用(指针)
weak var prev: ListNode? // 前驱节点引用(指针)
init(val: Int) {
self.val = val
}
} 在Swift中为了避免循环引用,是不是加个 |
Beta Was this translation helpful? Give feedback.
-
return (i + capacity()) % capacity();这段代码我看了好久没明白,结果看到了front = index(front - 1);是这个原因吧 |
Beta Was this translation helpful? Give feedback.
-
原来如此啊,撤销功能用的竟然是双栈队列 |
Beta Was this translation helpful? Give feedback.
-
// 队首出队操作 大佬,我想请问下这段代码能不能替换成下面这样。 // 队首出队操作 |
Beta Was this translation helpful? Give feedback.
-
关于最后撤销的实现,那么恢复(CTRL + Y)操作是怎么实现的? |
Beta Was this translation helpful? Give feedback.
-
if (isFront) {
val = front->val; // 暂存头节点值
// 删除头节点
DoublyListNode *fNext = front->next;
if (fNext != nullptr) {
fNext->prev = nullptr;
front->next = nullptr;
delete front;
}
front = fNext; // 更新头节点
// 队尾出队操作
} else {
val = rear->val; // 暂存尾节点值
// 删除尾节点
DoublyListNode *rPrev = rear->prev;
if (rPrev != nullptr) {
rPrev->next = nullptr;
rear->prev = nullptr;
delete rear;
}
rear = rPrev; // 更新尾节点
} 关于这段代码中删除节点的部分,为什么删除节点前需要手动断开连接,而单链表删除节点前不需要手动断开,不手动断开的话会有什么影响? |
Beta Was this translation helpful? Give feedback.
-
在双向队列的pop函数中用if (fNext != null) { |
Beta Was this translation helpful? Give feedback.
-
环形数组真的太秒啦!!! |
Beta Was this translation helpful? Give feedback.
-
有个问题:刚开始是空队列,然后入队1个元素,入队的时候c++代码只对front进行了维护,但是正因为只入队了一个元素,该元素即是队首也是队尾,是不是也应该对 rear 进行维护。 |
Beta Was this translation helpful? Give feedback.
-
有个问题: 1.来源: 其中的self._front.next = None代码语句 2.问题: 3.原因: 以上是个人见解,如果哪里错了还请诸位解答解答🙂 |
Beta Was this translation helpful? Give feedback.
-
第二个问题:
此处 return (i + self.capacity()) % self.capacity() i + self.capacity()是多余的,为什么要加上呢? |
Beta Was this translation helpful? Give feedback.
-
图 5-9 基于数组实现双向队列的入队出队操作,Step 2:push_last 有点小小的失误,应该没有尾指针rear,而就是数组的size嘞?我看你代码里实现,也是,直接用size,从队尾出队的。 |
Beta Was this translation helpful? Give feedback.
-
5.3.1 双向队列常用操作的JS和TS的isEmpty有問題,因為size賦的值是數字(屬於primitive values),所以不會因為之後deque.length變更而跟著更新 /* 判断双向队列是否为空 */ 應該改成 |
Beta Was this translation helpful? Give feedback.
-
另一个可以resize的arraydeque,大家参考 |
Beta Was this translation helpful? Give feedback.
-
python中 return (i + self.capacity()) % self.capacity()这里和这个return i % self.capacity()是一样的效果的吧,没太理解为什么要加上一个capacity 示例测试capacity = 5 results = [test_mod(i, capacity) for i in test_cases] |
Beta Was this translation helpful? Give feedback.
-
public class LinkDeque {
} |
Beta Was this translation helpful? Give feedback.
-
public class ArrayDeque {
数组实现双端队列 |
Beta Was this translation helpful? Give feedback.
-
基于链表的双向队列的Python实现中,关于pop方法有疑问: |
Beta Was this translation helpful? Give feedback.
-
也可通过传递具体的操作函数实现push 和pop 的通用,如: C 的指针函数 void _push(Deque* q, int val, void(*fPush)(Deque*, ListNode*));
void _pushFirst(Deque* q, ListNode* node);
void _pushLast(Deque* q, ListNode* node);
void pushFirst(Deque* q, int val) {
_push(q, val, _pushFirst);
}
void pushLast(Deque* q, int val) {
_push(q, val, _pushLast);
} Python 的可调用对象 def _push(self, val: int, f_push: Callable[['Deque', ListNode], None])
pass
def push_first(self, val):
def _push_first(q: 'Deque', node: ListNode):
pass
self._push(val, _push_first)
def push_last(self, val):
def _push_last(q: 'Deque', node: ListNode):
pass
self._push(val, _push_last) |
Beta Was this translation helpful? Give feedback.
-
def to_list(self) -> list[int]:
return [self.nums[self.index(self.front + i)] for i in range(self.size)] |
Beta Was this translation helpful? Give feedback.
-
def peek_first(self) -> int: |
Beta Was this translation helpful? Give feedback.
-
def peek_first(self) -> int:
这个不对吧,队列不是先入先出吗,这个好像是先入后出的? |
Beta Was this translation helpful? Give feedback.
-
chapter_stack_and_queue/deque/
一本动画图解、能运行、可提问的数据结构与算法入门书
https://www.hello-algo.com/chapter_stack_and_queue/deque/
Beta Was this translation helpful? Give feedback.
All reactions