chapter_stack_and_queue/deque/ #140
Replies: 63 comments 94 replies
-
hi |
Beta Was this translation helpful? Give feedback.
-
我想问一下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.
-
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.
-
def pop(self, is_front: bool) -> int: 双向队列基于链表实现的出队操作:为什么要暂存头节点和尾节点?不理解求大神指点(我上面写的对吗?) |
Beta Was this translation helpful? Give feedback.
-
Ab by
Sent from Yahoo Mail for iPhone
On Friday, January 24, 2025, 8:20 AM, wqk756633 ***@***.***> wrote:
def pop(self, is_front: bool) -> int:
if is_empty():
raise IndexError("双向队列为空")
elif is_front:
node = self._front
self._front = node.next
else:
node = self._rear
self._rear = node.prev
双向队列基于链表实现的出队操作:为什么要暂存头节点和尾节点?不理解求大神指点(我上面写的对吗?)
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
不管是队列还是双向队列,使用链表/双向链表实现的方案都看起来更直观更佳。 |
Beta Was this translation helpful? Give feedback.
-
day04 |
Beta Was this translation helpful? Give feedback.
-
https://www.hello-algo.com/chapter_stack_and_queue/deque.assets/linkedlist_deque_step4_pop_last.png |
Beta Was this translation helpful? Give feedback.
-
class LinkedListDeque {
|
Beta Was this translation helpful? Give feedback.
-
return (i + self.capacity()) % self.capacity() |
Beta Was this translation helpful? Give feedback.
-
为什么队尾不是指向元素呢而是指向空的数组呢 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
chapter_stack_and_queue/deque/
一本动画图解、能运行、可提问的数据结构与算法入门书
https://www.hello-algo.com/chapter_stack_and_queue/deque/
Beta Was this translation helpful? Give feedback.
All reactions