hello-algo/codes/cpp/chapter_stack_and_queue
KLYkl 9e914f8792
Update array_deque.cpp
将43行的return (i + capacity()) % capacity();改为return (i % capacity() + capacity()) % capacity();
原因:原来的表达式“(i + capacity()) % capacity();”如果i是负数的话,需要保证capacity() >= -i。而更改后的表达式“ (i % capacity() + capacity()) % capacity();” 中的先对i取模相加,最后再取模操作会确保结果是一个非负整数,并且不会超过 capacity() 的值。这是因为取模操作会返回一个在 0 到 capacity() - 1 范围内的数,即使原始的和可能是负数。保证函数index()的传入形参i<-capacity()不会崩溃,且能正确返回 0 到 capacity() - 1 范围内的的索引值。
2024-05-17 13:33:46 +08:00
..
array_deque.cpp Update array_deque.cpp 2024-05-17 13:33:46 +08:00
array_queue.cpp Some improvements (#1073) 2024-02-07 22:21:18 +08:00
array_stack.cpp A bug fix. 2023-12-19 21:55:57 +08:00
CMakeLists.txt Add cpp codes cmake build method (#419) 2023-03-14 20:40:33 +08:00
deque.cpp Some improvements (#1073) 2024-02-07 22:21:18 +08:00
linkedlist_deque.cpp Some improvements (#1073) 2024-02-07 22:21:18 +08:00
linkedlist_queue.cpp Some improvements (#1073) 2024-02-07 22:21:18 +08:00
linkedlist_stack.cpp A bug fix. 2023-12-19 21:55:57 +08:00
queue.cpp Rename the common modules in Java, C++ and C. 2023-04-24 04:11:18 +08:00
stack.cpp Rename the common modules in Java, C++ and C. 2023-04-24 04:11:18 +08:00