@ -3110,9 +3110,144 @@
< / code > < / pre > < / div >
< / div >
< div class = "tabbed-block" >
< div class = "highlight" > < span class = "filename" > linkedlist_deque.cs< / span > < pre > < span > < / span > < code > < a id = "__codelineno-17-1" name = "__codelineno-17-1" href = "#__codelineno-17-1" > < / a > < span class = "na" > [class]< / span > < span class = "p" > {< / span > < span class = "n" > ListNode< / span > < span class = "p" > }< / span > < span class = "o" > -< / span > < span class = "p" > [< / span > < span class = "n" > func< / span > < span class = "p" > ]{}< / span >
< a id = "__codelineno-17-2" name = "__codelineno-17-2" href = "#__codelineno-17-2" > < / a >
< a id = "__codelineno-17-3" name = "__codelineno-17-3" href = "#__codelineno-17-3" > < / a > < span class = "na" > [class]< / span > < span class = "p" > {< / span > < span class = "n" > LinkedListDeque< / span > < span class = "p" > }< / span > < span class = "o" > -< / span > < span class = "p" > [< / span > < span class = "n" > func< / span > < span class = "p" > ]{}< / span >
< div class = "highlight" > < span class = "filename" > linkedlist_deque.cs< / span > < pre > < span > < / span > < code > < a id = "__codelineno-17-1" name = "__codelineno-17-1" href = "#__codelineno-17-1" > < / a > < span class = "cm" > /* 双向链表节点 */< / span >
< a id = "__codelineno-17-2" name = "__codelineno-17-2" href = "#__codelineno-17-2" > < / a > < span class = "k" > class< / span > < span class = "w" > < / span > < span class = "nc" > ListNode< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-17-3" name = "__codelineno-17-3" href = "#__codelineno-17-3" > < / a > < span class = "w" > < / span > < span class = "k" > public< / span > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "n" > val< / span > < span class = "p" > ;< / span > < span class = "w" > < / span > < span class = "c1" > // 节点值< / span >
< a id = "__codelineno-17-4" name = "__codelineno-17-4" href = "#__codelineno-17-4" > < / a > < span class = "w" > < / span > < span class = "k" > public< / span > < span class = "w" > < / span > < span class = "n" > ListNode< / span > < span class = "o" > ?< / span > < span class = "w" > < / span > < span class = "n" > next< / span > < span class = "p" > ;< / span > < span class = "w" > < / span > < span class = "c1" > // 后继节点引用(指针)< / span >
< a id = "__codelineno-17-5" name = "__codelineno-17-5" href = "#__codelineno-17-5" > < / a > < span class = "w" > < / span > < span class = "k" > public< / span > < span class = "w" > < / span > < span class = "n" > ListNode< / span > < span class = "o" > ?< / span > < span class = "w" > < / span > < span class = "n" > prev< / span > < span class = "p" > ;< / span > < span class = "w" > < / span > < span class = "c1" > // 前驱节点引用(指针)< / span >
< a id = "__codelineno-17-6" name = "__codelineno-17-6" href = "#__codelineno-17-6" > < / a >
< a id = "__codelineno-17-7" name = "__codelineno-17-7" href = "#__codelineno-17-7" > < / a > < span class = "w" > < / span > < span class = "k" > public< / span > < span class = "w" > < / span > < span class = "nf" > ListNode< / span > < span class = "p" > (< / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "n" > val< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-17-8" name = "__codelineno-17-8" href = "#__codelineno-17-8" > < / a > < span class = "w" > < / span > < span class = "k" > this< / span > < span class = "p" > .< / span > < span class = "n" > val< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > val< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-17-9" name = "__codelineno-17-9" href = "#__codelineno-17-9" > < / a > < span class = "w" > < / span > < span class = "n" > prev< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "k" > null< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-17-10" name = "__codelineno-17-10" href = "#__codelineno-17-10" > < / a > < span class = "w" > < / span > < span class = "n" > next< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "k" > null< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-17-11" name = "__codelineno-17-11" href = "#__codelineno-17-11" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-17-12" name = "__codelineno-17-12" href = "#__codelineno-17-12" > < / a > < span class = "p" > }< / span >
< a id = "__codelineno-17-13" name = "__codelineno-17-13" href = "#__codelineno-17-13" > < / a >
< a id = "__codelineno-17-14" name = "__codelineno-17-14" href = "#__codelineno-17-14" > < / a > < span class = "cm" > /* 基于双向链表实现的双向队列 */< / span >
< a id = "__codelineno-17-15" name = "__codelineno-17-15" href = "#__codelineno-17-15" > < / a > < span class = "k" > class< / span > < span class = "w" > < / span > < span class = "nc" > LinkedListDeque< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-17-16" name = "__codelineno-17-16" href = "#__codelineno-17-16" > < / a > < span class = "w" > < / span > < span class = "k" > private< / span > < span class = "w" > < / span > < span class = "n" > ListNode< / span > < span class = "o" > ?< / span > < span class = "w" > < / span > < span class = "n" > front< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > rear< / span > < span class = "p" > ;< / span > < span class = "w" > < / span > < span class = "c1" > // 头节点 front, 尾节点 rear< / span >
< a id = "__codelineno-17-17" name = "__codelineno-17-17" href = "#__codelineno-17-17" > < / a > < span class = "w" > < / span > < span class = "k" > private< / span > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "n" > queSize< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "m" > 0< / span > < span class = "p" > ;< / span > < span class = "w" > < / span > < span class = "c1" > // 双向队列的长度< / span >
< a id = "__codelineno-17-18" name = "__codelineno-17-18" href = "#__codelineno-17-18" > < / a >
< a id = "__codelineno-17-19" name = "__codelineno-17-19" href = "#__codelineno-17-19" > < / a > < span class = "w" > < / span > < span class = "k" > public< / span > < span class = "w" > < / span > < span class = "nf" > LinkedListDeque< / span > < span class = "p" > ()< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-17-20" name = "__codelineno-17-20" href = "#__codelineno-17-20" > < / a > < span class = "w" > < / span > < span class = "n" > front< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "k" > null< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-17-21" name = "__codelineno-17-21" href = "#__codelineno-17-21" > < / a > < span class = "w" > < / span > < span class = "n" > rear< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "k" > null< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-17-22" name = "__codelineno-17-22" href = "#__codelineno-17-22" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-17-23" name = "__codelineno-17-23" href = "#__codelineno-17-23" > < / a >
< a id = "__codelineno-17-24" name = "__codelineno-17-24" href = "#__codelineno-17-24" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 获取双向队列的长度 */< / span >
< a id = "__codelineno-17-25" name = "__codelineno-17-25" href = "#__codelineno-17-25" > < / a > < span class = "w" > < / span > < span class = "k" > public< / span > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "nf" > size< / span > < span class = "p" > ()< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-17-26" name = "__codelineno-17-26" href = "#__codelineno-17-26" > < / a > < span class = "w" > < / span > < span class = "k" > return< / span > < span class = "w" > < / span > < span class = "n" > queSize< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-17-27" name = "__codelineno-17-27" href = "#__codelineno-17-27" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-17-28" name = "__codelineno-17-28" href = "#__codelineno-17-28" > < / a >
< a id = "__codelineno-17-29" name = "__codelineno-17-29" href = "#__codelineno-17-29" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 判断双向队列是否为空 */< / span >
< a id = "__codelineno-17-30" name = "__codelineno-17-30" href = "#__codelineno-17-30" > < / a > < span class = "w" > < / span > < span class = "k" > public< / span > < span class = "w" > < / span > < span class = "kt" > bool< / span > < span class = "w" > < / span > < span class = "nf" > isEmpty< / span > < span class = "p" > ()< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-17-31" name = "__codelineno-17-31" href = "#__codelineno-17-31" > < / a > < span class = "w" > < / span > < span class = "k" > return< / span > < span class = "w" > < / span > < span class = "nf" > size< / span > < span class = "p" > ()< / span > < span class = "w" > < / span > < span class = "o" > ==< / span > < span class = "w" > < / span > < span class = "m" > 0< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-17-32" name = "__codelineno-17-32" href = "#__codelineno-17-32" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-17-33" name = "__codelineno-17-33" href = "#__codelineno-17-33" > < / a >
< a id = "__codelineno-17-34" name = "__codelineno-17-34" href = "#__codelineno-17-34" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 入队操作 */< / span >
< a id = "__codelineno-17-35" name = "__codelineno-17-35" href = "#__codelineno-17-35" > < / a > < span class = "w" > < / span > < span class = "k" > private< / span > < span class = "w" > < / span > < span class = "k" > void< / span > < span class = "w" > < / span > < span class = "nf" > push< / span > < span class = "p" > (< / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "n" > num< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "kt" > bool< / span > < span class = "w" > < / span > < span class = "n" > isFront< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-17-36" name = "__codelineno-17-36" href = "#__codelineno-17-36" > < / a > < span class = "w" > < / span > < span class = "n" > ListNode< / span > < span class = "w" > < / span > < span class = "n" > node< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "k" > new< / span > < span class = "w" > < / span > < span class = "n" > ListNode< / span > < span class = "p" > (< / span > < span class = "n" > num< / span > < span class = "p" > );< / span >
< a id = "__codelineno-17-37" name = "__codelineno-17-37" href = "#__codelineno-17-37" > < / a > < span class = "w" > < / span > < span class = "c1" > // 若链表为空,则令 front, rear 都指向 node< / span >
< a id = "__codelineno-17-38" name = "__codelineno-17-38" href = "#__codelineno-17-38" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > isEmpty< / span > < span class = "p" > ())< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-17-39" name = "__codelineno-17-39" href = "#__codelineno-17-39" > < / a > < span class = "w" > < / span > < span class = "n" > front< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > node< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-17-40" name = "__codelineno-17-40" href = "#__codelineno-17-40" > < / a > < span class = "w" > < / span > < span class = "n" > rear< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > node< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-17-41" name = "__codelineno-17-41" href = "#__codelineno-17-41" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-17-42" name = "__codelineno-17-42" href = "#__codelineno-17-42" > < / a > < span class = "w" > < / span > < span class = "c1" > // 队首入队操作< / span >
< a id = "__codelineno-17-43" name = "__codelineno-17-43" href = "#__codelineno-17-43" > < / a > < span class = "w" > < / span > < span class = "k" > else< / span > < span class = "w" > < / span > < span class = "nf" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > isFront< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-17-44" name = "__codelineno-17-44" href = "#__codelineno-17-44" > < / a > < span class = "w" > < / span > < span class = "c1" > // 将 node 添加至链表头部< / span >
< a id = "__codelineno-17-45" name = "__codelineno-17-45" href = "#__codelineno-17-45" > < / a > < span class = "w" > < / span > < span class = "n" > front< / span > < span class = "p" > .< / span > < span class = "n" > prev< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > node< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-17-46" name = "__codelineno-17-46" href = "#__codelineno-17-46" > < / a > < span class = "w" > < / span > < span class = "n" > node< / span > < span class = "p" > .< / span > < span class = "n" > next< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > front< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-17-47" name = "__codelineno-17-47" href = "#__codelineno-17-47" > < / a > < span class = "w" > < / span > < span class = "n" > front< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > node< / span > < span class = "p" > ;< / span > < span class = "w" > < / span > < span class = "c1" > // 更新头节点 < / span >
< a id = "__codelineno-17-48" name = "__codelineno-17-48" href = "#__codelineno-17-48" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-17-49" name = "__codelineno-17-49" href = "#__codelineno-17-49" > < / a > < span class = "w" > < / span > < span class = "c1" > // 队尾入队操作< / span >
< a id = "__codelineno-17-50" name = "__codelineno-17-50" href = "#__codelineno-17-50" > < / a > < span class = "w" > < / span > < span class = "k" > else< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-17-51" name = "__codelineno-17-51" href = "#__codelineno-17-51" > < / a > < span class = "w" > < / span > < span class = "c1" > // 将 node 添加至链表尾部< / span >
< a id = "__codelineno-17-52" name = "__codelineno-17-52" href = "#__codelineno-17-52" > < / a > < span class = "w" > < / span > < span class = "n" > rear< / span > < span class = "p" > .< / span > < span class = "n" > next< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > node< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-17-53" name = "__codelineno-17-53" href = "#__codelineno-17-53" > < / a > < span class = "w" > < / span > < span class = "n" > node< / span > < span class = "p" > .< / span > < span class = "n" > prev< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > rear< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-17-54" name = "__codelineno-17-54" href = "#__codelineno-17-54" > < / a > < span class = "w" > < / span > < span class = "n" > rear< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > node< / span > < span class = "p" > ;< / span > < span class = "w" > < / span > < span class = "c1" > // 更新尾节点< / span >
< a id = "__codelineno-17-55" name = "__codelineno-17-55" href = "#__codelineno-17-55" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-17-56" name = "__codelineno-17-56" href = "#__codelineno-17-56" > < / a >
< a id = "__codelineno-17-57" name = "__codelineno-17-57" href = "#__codelineno-17-57" > < / a > < span class = "w" > < / span > < span class = "n" > queSize< / span > < span class = "o" > ++< / span > < span class = "p" > ;< / span > < span class = "w" > < / span > < span class = "c1" > // 更新队列长度< / span >
< a id = "__codelineno-17-58" name = "__codelineno-17-58" href = "#__codelineno-17-58" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-17-59" name = "__codelineno-17-59" href = "#__codelineno-17-59" > < / a >
< a id = "__codelineno-17-60" name = "__codelineno-17-60" href = "#__codelineno-17-60" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 队首入队 */< / span >
< a id = "__codelineno-17-61" name = "__codelineno-17-61" href = "#__codelineno-17-61" > < / a > < span class = "w" > < / span > < span class = "k" > public< / span > < span class = "w" > < / span > < span class = "k" > void< / span > < span class = "w" > < / span > < span class = "nf" > pushFirst< / span > < span class = "p" > (< / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "n" > num< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-17-62" name = "__codelineno-17-62" href = "#__codelineno-17-62" > < / a > < span class = "w" > < / span > < span class = "n" > push< / span > < span class = "p" > (< / span > < span class = "n" > num< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "k" > true< / span > < span class = "p" > );< / span >
< a id = "__codelineno-17-63" name = "__codelineno-17-63" href = "#__codelineno-17-63" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-17-64" name = "__codelineno-17-64" href = "#__codelineno-17-64" > < / a >
< a id = "__codelineno-17-65" name = "__codelineno-17-65" href = "#__codelineno-17-65" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 队尾入队 */< / span >
< a id = "__codelineno-17-66" name = "__codelineno-17-66" href = "#__codelineno-17-66" > < / a > < span class = "w" > < / span > < span class = "k" > public< / span > < span class = "w" > < / span > < span class = "k" > void< / span > < span class = "w" > < / span > < span class = "nf" > pushLast< / span > < span class = "p" > (< / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "n" > num< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-17-67" name = "__codelineno-17-67" href = "#__codelineno-17-67" > < / a > < span class = "w" > < / span > < span class = "n" > push< / span > < span class = "p" > (< / span > < span class = "n" > num< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "k" > false< / span > < span class = "p" > );< / span >
< a id = "__codelineno-17-68" name = "__codelineno-17-68" href = "#__codelineno-17-68" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-17-69" name = "__codelineno-17-69" href = "#__codelineno-17-69" > < / a >
< a id = "__codelineno-17-70" name = "__codelineno-17-70" href = "#__codelineno-17-70" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 出队操作 */< / span >
< a id = "__codelineno-17-71" name = "__codelineno-17-71" href = "#__codelineno-17-71" > < / a > < span class = "w" > < / span > < span class = "k" > private< / span > < span class = "w" > < / span > < span class = "kt" > int?< / span > < span class = "w" > < / span > < span class = "n" > pop< / span > < span class = "p" > (< / span > < span class = "kt" > bool< / span > < span class = "w" > < / span > < span class = "n" > isFront< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-17-72" name = "__codelineno-17-72" href = "#__codelineno-17-72" > < / a > < span class = "w" > < / span > < span class = "c1" > // 若队列为空,直接返回 null< / span >
< a id = "__codelineno-17-73" name = "__codelineno-17-73" href = "#__codelineno-17-73" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > isEmpty< / span > < span class = "p" > ())< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-17-74" name = "__codelineno-17-74" href = "#__codelineno-17-74" > < / a > < span class = "w" > < / span > < span class = "k" > return< / span > < span class = "w" > < / span > < span class = "k" > null< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-17-75" name = "__codelineno-17-75" href = "#__codelineno-17-75" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-17-76" name = "__codelineno-17-76" href = "#__codelineno-17-76" > < / a >
< a id = "__codelineno-17-77" name = "__codelineno-17-77" href = "#__codelineno-17-77" > < / a > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "n" > val< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-17-78" name = "__codelineno-17-78" href = "#__codelineno-17-78" > < / a > < span class = "w" > < / span > < span class = "c1" > // 队首出队操作< / span >
< a id = "__codelineno-17-79" name = "__codelineno-17-79" href = "#__codelineno-17-79" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > isFront< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-17-80" name = "__codelineno-17-80" href = "#__codelineno-17-80" > < / a > < span class = "w" > < / span > < span class = "n" > val< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > front< / span > < span class = "p" > .< / span > < span class = "n" > val< / span > < span class = "p" > ;< / span > < span class = "w" > < / span > < span class = "c1" > // 暂存头节点值< / span >
< a id = "__codelineno-17-81" name = "__codelineno-17-81" href = "#__codelineno-17-81" > < / a > < span class = "w" > < / span > < span class = "c1" > // 删除头节点< / span >
< a id = "__codelineno-17-82" name = "__codelineno-17-82" href = "#__codelineno-17-82" > < / a > < span class = "w" > < / span > < span class = "n" > ListNode< / span > < span class = "w" > < / span > < span class = "n" > fNext< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > front< / span > < span class = "p" > .< / span > < span class = "n" > next< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-17-83" name = "__codelineno-17-83" href = "#__codelineno-17-83" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > fNext< / span > < span class = "w" > < / span > < span class = "o" > !=< / span > < span class = "w" > < / span > < span class = "k" > null< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-17-84" name = "__codelineno-17-84" href = "#__codelineno-17-84" > < / a > < span class = "w" > < / span > < span class = "n" > fNext< / span > < span class = "p" > .< / span > < span class = "n" > prev< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "k" > null< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-17-85" name = "__codelineno-17-85" href = "#__codelineno-17-85" > < / a > < span class = "w" > < / span > < span class = "n" > front< / span > < span class = "p" > .< / span > < span class = "n" > next< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "k" > null< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-17-86" name = "__codelineno-17-86" href = "#__codelineno-17-86" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-17-87" name = "__codelineno-17-87" href = "#__codelineno-17-87" > < / a >
< a id = "__codelineno-17-88" name = "__codelineno-17-88" href = "#__codelineno-17-88" > < / a > < span class = "w" > < / span > < span class = "n" > front< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > fNext< / span > < span class = "p" > ;< / span > < span class = "w" > < / span > < span class = "c1" > // 更新头节点< / span >
< a id = "__codelineno-17-89" name = "__codelineno-17-89" href = "#__codelineno-17-89" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-17-90" name = "__codelineno-17-90" href = "#__codelineno-17-90" > < / a > < span class = "w" > < / span > < span class = "c1" > // 队尾出队操作< / span >
< a id = "__codelineno-17-91" name = "__codelineno-17-91" href = "#__codelineno-17-91" > < / a > < span class = "w" > < / span > < span class = "k" > else< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-17-92" name = "__codelineno-17-92" href = "#__codelineno-17-92" > < / a > < span class = "w" > < / span > < span class = "n" > val< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > rear< / span > < span class = "p" > .< / span > < span class = "n" > val< / span > < span class = "p" > ;< / span > < span class = "w" > < / span > < span class = "c1" > // 暂存尾节点值< / span >
< a id = "__codelineno-17-93" name = "__codelineno-17-93" href = "#__codelineno-17-93" > < / a > < span class = "w" > < / span > < span class = "c1" > // 删除尾节点< / span >
< a id = "__codelineno-17-94" name = "__codelineno-17-94" href = "#__codelineno-17-94" > < / a > < span class = "w" > < / span > < span class = "n" > ListNode< / span > < span class = "w" > < / span > < span class = "n" > rPrev< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > rear< / span > < span class = "p" > .< / span > < span class = "n" > prev< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-17-95" name = "__codelineno-17-95" href = "#__codelineno-17-95" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > rPrev< / span > < span class = "w" > < / span > < span class = "o" > !=< / span > < span class = "w" > < / span > < span class = "k" > null< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-17-96" name = "__codelineno-17-96" href = "#__codelineno-17-96" > < / a > < span class = "w" > < / span > < span class = "n" > rPrev< / span > < span class = "p" > .< / span > < span class = "n" > next< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "k" > null< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-17-97" name = "__codelineno-17-97" href = "#__codelineno-17-97" > < / a > < span class = "w" > < / span > < span class = "n" > rear< / span > < span class = "p" > .< / span > < span class = "n" > prev< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "k" > null< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-17-98" name = "__codelineno-17-98" href = "#__codelineno-17-98" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-17-99" name = "__codelineno-17-99" href = "#__codelineno-17-99" > < / a >
< a id = "__codelineno-17-100" name = "__codelineno-17-100" href = "#__codelineno-17-100" > < / a > < span class = "w" > < / span > < span class = "n" > rear< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > rPrev< / span > < span class = "p" > ;< / span > < span class = "w" > < / span > < span class = "c1" > // 更新尾节点< / span >
< a id = "__codelineno-17-101" name = "__codelineno-17-101" href = "#__codelineno-17-101" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-17-102" name = "__codelineno-17-102" href = "#__codelineno-17-102" > < / a >
< a id = "__codelineno-17-103" name = "__codelineno-17-103" href = "#__codelineno-17-103" > < / a > < span class = "w" > < / span > < span class = "n" > queSize< / span > < span class = "o" > --< / span > < span class = "p" > ;< / span > < span class = "w" > < / span > < span class = "c1" > // 更新队列长度< / span >
< a id = "__codelineno-17-104" name = "__codelineno-17-104" href = "#__codelineno-17-104" > < / a > < span class = "w" > < / span > < span class = "k" > return< / span > < span class = "w" > < / span > < span class = "n" > val< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-17-105" name = "__codelineno-17-105" href = "#__codelineno-17-105" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-17-106" name = "__codelineno-17-106" href = "#__codelineno-17-106" > < / a >
< a id = "__codelineno-17-107" name = "__codelineno-17-107" href = "#__codelineno-17-107" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 队首出队 */< / span >
< a id = "__codelineno-17-108" name = "__codelineno-17-108" href = "#__codelineno-17-108" > < / a > < span class = "w" > < / span > < span class = "k" > public< / span > < span class = "w" > < / span > < span class = "kt" > int?< / span > < span class = "w" > < / span > < span class = "n" > popFirst< / span > < span class = "p" > ()< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-17-109" name = "__codelineno-17-109" href = "#__codelineno-17-109" > < / a > < span class = "w" > < / span > < span class = "k" > return< / span > < span class = "w" > < / span > < span class = "nf" > pop< / span > < span class = "p" > (< / span > < span class = "k" > true< / span > < span class = "p" > );< / span >
< a id = "__codelineno-17-110" name = "__codelineno-17-110" href = "#__codelineno-17-110" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-17-111" name = "__codelineno-17-111" href = "#__codelineno-17-111" > < / a >
< a id = "__codelineno-17-112" name = "__codelineno-17-112" href = "#__codelineno-17-112" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 队尾出队 */< / span >
< a id = "__codelineno-17-113" name = "__codelineno-17-113" href = "#__codelineno-17-113" > < / a > < span class = "w" > < / span > < span class = "k" > public< / span > < span class = "w" > < / span > < span class = "kt" > int?< / span > < span class = "w" > < / span > < span class = "n" > popLast< / span > < span class = "p" > ()< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-17-114" name = "__codelineno-17-114" href = "#__codelineno-17-114" > < / a > < span class = "w" > < / span > < span class = "k" > return< / span > < span class = "w" > < / span > < span class = "nf" > pop< / span > < span class = "p" > (< / span > < span class = "k" > false< / span > < span class = "p" > );< / span >
< a id = "__codelineno-17-115" name = "__codelineno-17-115" href = "#__codelineno-17-115" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-17-116" name = "__codelineno-17-116" href = "#__codelineno-17-116" > < / a >
< a id = "__codelineno-17-117" name = "__codelineno-17-117" href = "#__codelineno-17-117" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 访问队首元素 */< / span >
< a id = "__codelineno-17-118" name = "__codelineno-17-118" href = "#__codelineno-17-118" > < / a > < span class = "w" > < / span > < span class = "k" > public< / span > < span class = "w" > < / span > < span class = "kt" > int?< / span > < span class = "w" > < / span > < span class = "n" > peekFirst< / span > < span class = "p" > ()< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-17-119" name = "__codelineno-17-119" href = "#__codelineno-17-119" > < / a > < span class = "w" > < / span > < span class = "k" > return< / span > < span class = "w" > < / span > < span class = "nf" > isEmpty< / span > < span class = "p" > ()< / span > < span class = "w" > < / span > < span class = "o" > ?< / span > < span class = "w" > < / span > < span class = "k" > null< / span > < span class = "w" > < / span > < span class = "p" > :< / span > < span class = "w" > < / span > < span class = "n" > front< / span > < span class = "p" > .< / span > < span class = "n" > val< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-17-120" name = "__codelineno-17-120" href = "#__codelineno-17-120" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-17-121" name = "__codelineno-17-121" href = "#__codelineno-17-121" > < / a >
< a id = "__codelineno-17-122" name = "__codelineno-17-122" href = "#__codelineno-17-122" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 访问队尾元素 */< / span >
< a id = "__codelineno-17-123" name = "__codelineno-17-123" href = "#__codelineno-17-123" > < / a > < span class = "w" > < / span > < span class = "k" > public< / span > < span class = "w" > < / span > < span class = "kt" > int?< / span > < span class = "w" > < / span > < span class = "n" > peekLast< / span > < span class = "p" > ()< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-17-124" name = "__codelineno-17-124" href = "#__codelineno-17-124" > < / a > < span class = "w" > < / span > < span class = "k" > return< / span > < span class = "w" > < / span > < span class = "nf" > isEmpty< / span > < span class = "p" > ()< / span > < span class = "w" > < / span > < span class = "o" > ?< / span > < span class = "w" > < / span > < span class = "k" > null< / span > < span class = "w" > < / span > < span class = "p" > :< / span > < span class = "w" > < / span > < span class = "n" > rear< / span > < span class = "p" > .< / span > < span class = "n" > val< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-17-125" name = "__codelineno-17-125" href = "#__codelineno-17-125" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-17-126" name = "__codelineno-17-126" href = "#__codelineno-17-126" > < / a >
< a id = "__codelineno-17-127" name = "__codelineno-17-127" href = "#__codelineno-17-127" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 返回数组用于打印 */< / span >
< a id = "__codelineno-17-128" name = "__codelineno-17-128" href = "#__codelineno-17-128" > < / a > < span class = "w" > < / span > < span class = "k" > public< / span > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "p" > []< / span > < span class = "w" > < / span > < span class = "nf" > toArray< / span > < span class = "p" > ()< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-17-129" name = "__codelineno-17-129" href = "#__codelineno-17-129" > < / a > < span class = "w" > < / span > < span class = "n" > ListNode< / span > < span class = "w" > < / span > < span class = "n" > node< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > front< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-17-130" name = "__codelineno-17-130" href = "#__codelineno-17-130" > < / a > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "p" > []< / span > < span class = "w" > < / span > < span class = "n" > res< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "k" > new< / span > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "p" > [< / span > < span class = "n" > size< / span > < span class = "p" > ()];< / span >
< a id = "__codelineno-17-131" name = "__codelineno-17-131" href = "#__codelineno-17-131" > < / a > < span class = "w" > < / span > < span class = "k" > for< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "n" > i< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "m" > 0< / span > < span class = "p" > ;< / span > < span class = "w" > < / span > < span class = "n" > i< / span > < span class = "w" > < / span > < span class = "o" > < < / span > < span class = "w" > < / span > < span class = "n" > res< / span > < span class = "p" > .< / span > < span class = "n" > Length< / span > < span class = "p" > ;< / span > < span class = "w" > < / span > < span class = "n" > i< / span > < span class = "o" > ++< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-17-132" name = "__codelineno-17-132" href = "#__codelineno-17-132" > < / a > < span class = "w" > < / span > < span class = "n" > res< / span > < span class = "p" > [< / span > < span class = "n" > i< / span > < span class = "p" > ]< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > node< / span > < span class = "p" > .< / span > < span class = "n" > val< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-17-133" name = "__codelineno-17-133" href = "#__codelineno-17-133" > < / a > < span class = "w" > < / span > < span class = "n" > node< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > node< / span > < span class = "p" > .< / span > < span class = "n" > next< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-17-134" name = "__codelineno-17-134" href = "#__codelineno-17-134" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-17-135" name = "__codelineno-17-135" href = "#__codelineno-17-135" > < / a >
< a id = "__codelineno-17-136" name = "__codelineno-17-136" href = "#__codelineno-17-136" > < / a > < span class = "w" > < / span > < span class = "k" > return< / span > < span class = "w" > < / span > < span class = "n" > res< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-17-137" name = "__codelineno-17-137" href = "#__codelineno-17-137" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-17-138" name = "__codelineno-17-138" href = "#__codelineno-17-138" > < / a > < span class = "p" > }< / span >
< / code > < / pre > < / div >
< / div >
< div class = "tabbed-block" >
@ -4182,7 +4317,112 @@
< / code > < / pre > < / div >
< / div >
< div class = "tabbed-block" >
< div class = "highlight" > < span class = "filename" > array_deque.cs< / span > < pre > < span > < / span > < code > < a id = "__codelineno-27-1" name = "__codelineno-27-1" href = "#__codelineno-27-1" > < / a > < span class = "na" > [class]< / span > < span class = "p" > {< / span > < span class = "n" > ArrayDeque< / span > < span class = "p" > }< / span > < span class = "o" > -< / span > < span class = "p" > [< / span > < span class = "n" > func< / span > < span class = "p" > ]{}< / span >
< div class = "highlight" > < span class = "filename" > array_deque.cs< / span > < pre > < span > < / span > < code > < a id = "__codelineno-27-1" name = "__codelineno-27-1" href = "#__codelineno-27-1" > < / a > < span class = "cm" > /* 基于环形数组实现的双向队列 */< / span >
< a id = "__codelineno-27-2" name = "__codelineno-27-2" href = "#__codelineno-27-2" > < / a > < span class = "k" > class< / span > < span class = "w" > < / span > < span class = "nc" > ArrayDeque< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-27-3" name = "__codelineno-27-3" href = "#__codelineno-27-3" > < / a > < span class = "w" > < / span > < span class = "k" > private< / span > < span class = "w" > < / span > < span class = "k" > readonly< / span > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "p" > []< / span > < span class = "w" > < / span > < span class = "n" > nums< / span > < span class = "p" > ;< / span > < span class = "w" > < / span > < span class = "c1" > // 用于存储双向队列元素的数组< / span >
< a id = "__codelineno-27-4" name = "__codelineno-27-4" href = "#__codelineno-27-4" > < / a > < span class = "w" > < / span > < span class = "k" > private< / span > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "n" > front< / span > < span class = "p" > ;< / span > < span class = "w" > < / span > < span class = "c1" > // 队首指针,指向队首元素< / span >
< a id = "__codelineno-27-5" name = "__codelineno-27-5" href = "#__codelineno-27-5" > < / a > < span class = "w" > < / span > < span class = "k" > private< / span > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "n" > queSize< / span > < span class = "p" > ;< / span > < span class = "w" > < / span > < span class = "c1" > // 双向队列长度< / span >
< a id = "__codelineno-27-6" name = "__codelineno-27-6" href = "#__codelineno-27-6" > < / a >
< a id = "__codelineno-27-7" name = "__codelineno-27-7" href = "#__codelineno-27-7" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 构造方法 */< / span >
< a id = "__codelineno-27-8" name = "__codelineno-27-8" href = "#__codelineno-27-8" > < / a > < span class = "w" > < / span > < span class = "k" > public< / span > < span class = "w" > < / span > < span class = "nf" > ArrayDeque< / span > < span class = "p" > (< / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "n" > capacity< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-27-9" name = "__codelineno-27-9" href = "#__codelineno-27-9" > < / a > < span class = "w" > < / span > < span class = "k" > this< / span > < span class = "p" > .< / span > < span class = "n" > nums< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "k" > new< / span > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "p" > [< / span > < span class = "n" > capacity< / span > < span class = "p" > ];< / span >
< a id = "__codelineno-27-10" name = "__codelineno-27-10" href = "#__codelineno-27-10" > < / a > < span class = "w" > < / span > < span class = "n" > front< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > queSize< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "m" > 0< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-27-11" name = "__codelineno-27-11" href = "#__codelineno-27-11" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-27-12" name = "__codelineno-27-12" href = "#__codelineno-27-12" > < / a >
< a id = "__codelineno-27-13" name = "__codelineno-27-13" href = "#__codelineno-27-13" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 获取双向队列的容量 */< / span >
< a id = "__codelineno-27-14" name = "__codelineno-27-14" href = "#__codelineno-27-14" > < / a > < span class = "w" > < / span > < span class = "k" > public< / span > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "nf" > capacity< / span > < span class = "p" > ()< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-27-15" name = "__codelineno-27-15" href = "#__codelineno-27-15" > < / a > < span class = "w" > < / span > < span class = "k" > return< / span > < span class = "w" > < / span > < span class = "n" > nums< / span > < span class = "p" > .< / span > < span class = "n" > Length< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-27-16" name = "__codelineno-27-16" href = "#__codelineno-27-16" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-27-17" name = "__codelineno-27-17" href = "#__codelineno-27-17" > < / a >
< a id = "__codelineno-27-18" name = "__codelineno-27-18" href = "#__codelineno-27-18" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 获取双向队列的长度 */< / span >
< a id = "__codelineno-27-19" name = "__codelineno-27-19" href = "#__codelineno-27-19" > < / a > < span class = "w" > < / span > < span class = "k" > public< / span > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "nf" > size< / span > < span class = "p" > ()< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-27-20" name = "__codelineno-27-20" href = "#__codelineno-27-20" > < / a > < span class = "w" > < / span > < span class = "k" > return< / span > < span class = "w" > < / span > < span class = "n" > queSize< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-27-21" name = "__codelineno-27-21" href = "#__codelineno-27-21" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-27-22" name = "__codelineno-27-22" href = "#__codelineno-27-22" > < / a >
< a id = "__codelineno-27-23" name = "__codelineno-27-23" href = "#__codelineno-27-23" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 判断双向队列是否为空 */< / span >
< a id = "__codelineno-27-24" name = "__codelineno-27-24" href = "#__codelineno-27-24" > < / a > < span class = "w" > < / span > < span class = "k" > public< / span > < span class = "w" > < / span > < span class = "kt" > bool< / span > < span class = "w" > < / span > < span class = "nf" > isEmpty< / span > < span class = "p" > ()< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-27-25" name = "__codelineno-27-25" href = "#__codelineno-27-25" > < / a > < span class = "w" > < / span > < span class = "k" > return< / span > < span class = "w" > < / span > < span class = "n" > queSize< / span > < span class = "w" > < / span > < span class = "o" > ==< / span > < span class = "w" > < / span > < span class = "m" > 0< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-27-26" name = "__codelineno-27-26" href = "#__codelineno-27-26" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-27-27" name = "__codelineno-27-27" href = "#__codelineno-27-27" > < / a >
< a id = "__codelineno-27-28" name = "__codelineno-27-28" href = "#__codelineno-27-28" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 计算环形数组索引 */< / span >
< a id = "__codelineno-27-29" name = "__codelineno-27-29" href = "#__codelineno-27-29" > < / a > < span class = "w" > < / span > < span class = "k" > private< / span > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "nf" > index< / span > < span class = "p" > (< / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "n" > i< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-27-30" name = "__codelineno-27-30" href = "#__codelineno-27-30" > < / a > < span class = "w" > < / span > < span class = "c1" > // 通过取余操作实现数组首尾相连< / span >
< a id = "__codelineno-27-31" name = "__codelineno-27-31" href = "#__codelineno-27-31" > < / a > < span class = "w" > < / span > < span class = "c1" > // 当 i 越过数组尾部后,回到头部< / span >
< a id = "__codelineno-27-32" name = "__codelineno-27-32" href = "#__codelineno-27-32" > < / a > < span class = "w" > < / span > < span class = "c1" > // 当 i 越过数组头部后,回到尾部< / span >
< a id = "__codelineno-27-33" name = "__codelineno-27-33" href = "#__codelineno-27-33" > < / a > < span class = "w" > < / span > < span class = "k" > return< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > i< / span > < span class = "w" > < / span > < span class = "o" > +< / span > < span class = "w" > < / span > < span class = "n" > capacity< / span > < span class = "p" > ())< / span > < span class = "w" > < / span > < span class = "o" > %< / span > < span class = "w" > < / span > < span class = "n" > capacity< / span > < span class = "p" > ();< / span >
< a id = "__codelineno-27-34" name = "__codelineno-27-34" href = "#__codelineno-27-34" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-27-35" name = "__codelineno-27-35" href = "#__codelineno-27-35" > < / a >
< a id = "__codelineno-27-36" name = "__codelineno-27-36" href = "#__codelineno-27-36" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 队首入队 */< / span >
< a id = "__codelineno-27-37" name = "__codelineno-27-37" href = "#__codelineno-27-37" > < / a > < span class = "w" > < / span > < span class = "k" > public< / span > < span class = "w" > < / span > < span class = "k" > void< / span > < span class = "w" > < / span > < span class = "nf" > pushFirst< / span > < span class = "p" > (< / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "n" > num< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-27-38" name = "__codelineno-27-38" href = "#__codelineno-27-38" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > queSize< / span > < span class = "w" > < / span > < span class = "o" > ==< / span > < span class = "w" > < / span > < span class = "n" > capacity< / span > < span class = "p" > ())< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-27-39" name = "__codelineno-27-39" href = "#__codelineno-27-39" > < / a > < span class = "w" > < / span > < span class = "n" > Console< / span > < span class = "p" > .< / span > < span class = "n" > WriteLine< / span > < span class = "p" > (< / span > < span class = "s" > " 双向队列已满" < / span > < span class = "p" > );< / span >
< a id = "__codelineno-27-40" name = "__codelineno-27-40" href = "#__codelineno-27-40" > < / a > < span class = "w" > < / span > < span class = "k" > return< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-27-41" name = "__codelineno-27-41" href = "#__codelineno-27-41" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-27-42" name = "__codelineno-27-42" href = "#__codelineno-27-42" > < / a > < span class = "w" > < / span > < span class = "c1" > // 队首指针向左移动一位< / span >
< a id = "__codelineno-27-43" name = "__codelineno-27-43" href = "#__codelineno-27-43" > < / a > < span class = "w" > < / span > < span class = "c1" > // 通过取余操作,实现 front 越过数组头部后回到尾部< / span >
< a id = "__codelineno-27-44" name = "__codelineno-27-44" href = "#__codelineno-27-44" > < / a > < span class = "w" > < / span > < span class = "n" > front< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > index< / span > < span class = "p" > (< / span > < span class = "n" > front< / span > < span class = "w" > < / span > < span class = "o" > -< / span > < span class = "w" > < / span > < span class = "m" > 1< / span > < span class = "p" > );< / span >
< a id = "__codelineno-27-45" name = "__codelineno-27-45" href = "#__codelineno-27-45" > < / a > < span class = "w" > < / span > < span class = "c1" > // 将 num 添加至队首< / span >
< a id = "__codelineno-27-46" name = "__codelineno-27-46" href = "#__codelineno-27-46" > < / a > < span class = "w" > < / span > < span class = "n" > nums< / span > < span class = "p" > [< / span > < span class = "n" > front< / span > < span class = "p" > ]< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > num< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-27-47" name = "__codelineno-27-47" href = "#__codelineno-27-47" > < / a > < span class = "w" > < / span > < span class = "n" > queSize< / span > < span class = "o" > ++< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-27-48" name = "__codelineno-27-48" href = "#__codelineno-27-48" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-27-49" name = "__codelineno-27-49" href = "#__codelineno-27-49" > < / a >
< a id = "__codelineno-27-50" name = "__codelineno-27-50" href = "#__codelineno-27-50" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 队尾入队 */< / span >
< a id = "__codelineno-27-51" name = "__codelineno-27-51" href = "#__codelineno-27-51" > < / a > < span class = "w" > < / span > < span class = "k" > public< / span > < span class = "w" > < / span > < span class = "k" > void< / span > < span class = "w" > < / span > < span class = "nf" > pushLast< / span > < span class = "p" > (< / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "n" > num< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-27-52" name = "__codelineno-27-52" href = "#__codelineno-27-52" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > queSize< / span > < span class = "w" > < / span > < span class = "o" > ==< / span > < span class = "w" > < / span > < span class = "n" > capacity< / span > < span class = "p" > ())< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-27-53" name = "__codelineno-27-53" href = "#__codelineno-27-53" > < / a > < span class = "w" > < / span > < span class = "n" > Console< / span > < span class = "p" > .< / span > < span class = "n" > WriteLine< / span > < span class = "p" > (< / span > < span class = "s" > " 双向队列已满" < / span > < span class = "p" > );< / span >
< a id = "__codelineno-27-54" name = "__codelineno-27-54" href = "#__codelineno-27-54" > < / a > < span class = "w" > < / span > < span class = "k" > return< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-27-55" name = "__codelineno-27-55" href = "#__codelineno-27-55" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-27-56" name = "__codelineno-27-56" href = "#__codelineno-27-56" > < / a > < span class = "w" > < / span > < span class = "c1" > // 计算尾指针,指向队尾索引 + 1< / span >
< a id = "__codelineno-27-57" name = "__codelineno-27-57" href = "#__codelineno-27-57" > < / a > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "n" > rear< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > index< / span > < span class = "p" > (< / span > < span class = "n" > front< / span > < span class = "w" > < / span > < span class = "o" > +< / span > < span class = "w" > < / span > < span class = "n" > queSize< / span > < span class = "p" > );< / span >
< a id = "__codelineno-27-58" name = "__codelineno-27-58" href = "#__codelineno-27-58" > < / a > < span class = "w" > < / span > < span class = "c1" > // 将 num 添加至队尾< / span >
< a id = "__codelineno-27-59" name = "__codelineno-27-59" href = "#__codelineno-27-59" > < / a > < span class = "w" > < / span > < span class = "n" > nums< / span > < span class = "p" > [< / span > < span class = "n" > rear< / span > < span class = "p" > ]< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > num< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-27-60" name = "__codelineno-27-60" href = "#__codelineno-27-60" > < / a > < span class = "w" > < / span > < span class = "n" > queSize< / span > < span class = "o" > ++< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-27-61" name = "__codelineno-27-61" href = "#__codelineno-27-61" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-27-62" name = "__codelineno-27-62" href = "#__codelineno-27-62" > < / a >
< a id = "__codelineno-27-63" name = "__codelineno-27-63" href = "#__codelineno-27-63" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 队首出队 */< / span >
< a id = "__codelineno-27-64" name = "__codelineno-27-64" href = "#__codelineno-27-64" > < / a > < span class = "w" > < / span > < span class = "k" > public< / span > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "nf" > popFirst< / span > < span class = "p" > ()< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-27-65" name = "__codelineno-27-65" href = "#__codelineno-27-65" > < / a > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "n" > num< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > peekFirst< / span > < span class = "p" > ();< / span >
< a id = "__codelineno-27-66" name = "__codelineno-27-66" href = "#__codelineno-27-66" > < / a > < span class = "w" > < / span > < span class = "c1" > // 队首指针向后移动一位< / span >
< a id = "__codelineno-27-67" name = "__codelineno-27-67" href = "#__codelineno-27-67" > < / a > < span class = "w" > < / span > < span class = "n" > front< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > index< / span > < span class = "p" > (< / span > < span class = "n" > front< / span > < span class = "w" > < / span > < span class = "o" > +< / span > < span class = "w" > < / span > < span class = "m" > 1< / span > < span class = "p" > );< / span >
< a id = "__codelineno-27-68" name = "__codelineno-27-68" href = "#__codelineno-27-68" > < / a > < span class = "w" > < / span > < span class = "n" > queSize< / span > < span class = "o" > --< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-27-69" name = "__codelineno-27-69" href = "#__codelineno-27-69" > < / a > < span class = "w" > < / span > < span class = "k" > return< / span > < span class = "w" > < / span > < span class = "n" > num< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-27-70" name = "__codelineno-27-70" href = "#__codelineno-27-70" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-27-71" name = "__codelineno-27-71" href = "#__codelineno-27-71" > < / a >
< a id = "__codelineno-27-72" name = "__codelineno-27-72" href = "#__codelineno-27-72" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 队尾出队 */< / span >
< a id = "__codelineno-27-73" name = "__codelineno-27-73" href = "#__codelineno-27-73" > < / a > < span class = "w" > < / span > < span class = "k" > public< / span > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "nf" > popLast< / span > < span class = "p" > ()< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-27-74" name = "__codelineno-27-74" href = "#__codelineno-27-74" > < / a > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "n" > num< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > peekLast< / span > < span class = "p" > ();< / span >
< a id = "__codelineno-27-75" name = "__codelineno-27-75" href = "#__codelineno-27-75" > < / a > < span class = "w" > < / span > < span class = "n" > queSize< / span > < span class = "o" > --< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-27-76" name = "__codelineno-27-76" href = "#__codelineno-27-76" > < / a > < span class = "w" > < / span > < span class = "k" > return< / span > < span class = "w" > < / span > < span class = "n" > num< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-27-77" name = "__codelineno-27-77" href = "#__codelineno-27-77" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-27-78" name = "__codelineno-27-78" href = "#__codelineno-27-78" > < / a >
< a id = "__codelineno-27-79" name = "__codelineno-27-79" href = "#__codelineno-27-79" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 访问队首元素 */< / span >
< a id = "__codelineno-27-80" name = "__codelineno-27-80" href = "#__codelineno-27-80" > < / a > < span class = "w" > < / span > < span class = "k" > public< / span > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "nf" > peekFirst< / span > < span class = "p" > ()< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-27-81" name = "__codelineno-27-81" href = "#__codelineno-27-81" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > isEmpty< / span > < span class = "p" > ())< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-27-82" name = "__codelineno-27-82" href = "#__codelineno-27-82" > < / a > < span class = "w" > < / span > < span class = "k" > throw< / span > < span class = "w" > < / span > < span class = "k" > new< / span > < span class = "w" > < / span > < span class = "nf" > InvalidOperationException< / span > < span class = "p" > ();< / span >
< a id = "__codelineno-27-83" name = "__codelineno-27-83" href = "#__codelineno-27-83" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-27-84" name = "__codelineno-27-84" href = "#__codelineno-27-84" > < / a > < span class = "w" > < / span > < span class = "k" > return< / span > < span class = "w" > < / span > < span class = "n" > nums< / span > < span class = "p" > [< / span > < span class = "n" > front< / span > < span class = "p" > ];< / span >
< a id = "__codelineno-27-85" name = "__codelineno-27-85" href = "#__codelineno-27-85" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-27-86" name = "__codelineno-27-86" href = "#__codelineno-27-86" > < / a >
< a id = "__codelineno-27-87" name = "__codelineno-27-87" href = "#__codelineno-27-87" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 访问队尾元素 */< / span >
< a id = "__codelineno-27-88" name = "__codelineno-27-88" href = "#__codelineno-27-88" > < / a > < span class = "w" > < / span > < span class = "k" > public< / span > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "nf" > peekLast< / span > < span class = "p" > ()< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-27-89" name = "__codelineno-27-89" href = "#__codelineno-27-89" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > isEmpty< / span > < span class = "p" > ())< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-27-90" name = "__codelineno-27-90" href = "#__codelineno-27-90" > < / a > < span class = "w" > < / span > < span class = "k" > throw< / span > < span class = "w" > < / span > < span class = "k" > new< / span > < span class = "w" > < / span > < span class = "nf" > InvalidOperationException< / span > < span class = "p" > ();< / span >
< a id = "__codelineno-27-91" name = "__codelineno-27-91" href = "#__codelineno-27-91" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-27-92" name = "__codelineno-27-92" href = "#__codelineno-27-92" > < / a > < span class = "w" > < / span > < span class = "c1" > // 计算尾元素索引< / span >
< a id = "__codelineno-27-93" name = "__codelineno-27-93" href = "#__codelineno-27-93" > < / a > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "n" > last< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > index< / span > < span class = "p" > (< / span > < span class = "n" > front< / span > < span class = "w" > < / span > < span class = "o" > +< / span > < span class = "w" > < / span > < span class = "n" > queSize< / span > < span class = "w" > < / span > < span class = "o" > -< / span > < span class = "w" > < / span > < span class = "m" > 1< / span > < span class = "p" > );< / span >
< a id = "__codelineno-27-94" name = "__codelineno-27-94" href = "#__codelineno-27-94" > < / a > < span class = "w" > < / span > < span class = "k" > return< / span > < span class = "w" > < / span > < span class = "n" > nums< / span > < span class = "p" > [< / span > < span class = "n" > last< / span > < span class = "p" > ];< / span >
< a id = "__codelineno-27-95" name = "__codelineno-27-95" href = "#__codelineno-27-95" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-27-96" name = "__codelineno-27-96" href = "#__codelineno-27-96" > < / a >
< a id = "__codelineno-27-97" name = "__codelineno-27-97" href = "#__codelineno-27-97" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 返回数组用于打印 */< / span >
< a id = "__codelineno-27-98" name = "__codelineno-27-98" href = "#__codelineno-27-98" > < / a > < span class = "w" > < / span > < span class = "k" > public< / span > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "p" > []< / span > < span class = "w" > < / span > < span class = "nf" > toArray< / span > < span class = "p" > ()< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-27-99" name = "__codelineno-27-99" href = "#__codelineno-27-99" > < / a > < span class = "w" > < / span > < span class = "c1" > // 仅转换有效长度范围内的列表元素< / span >
< a id = "__codelineno-27-100" name = "__codelineno-27-100" href = "#__codelineno-27-100" > < / a > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "p" > []< / span > < span class = "w" > < / span > < span class = "n" > res< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "k" > new< / span > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "p" > [< / span > < span class = "n" > queSize< / span > < span class = "p" > ];< / span >
< a id = "__codelineno-27-101" name = "__codelineno-27-101" href = "#__codelineno-27-101" > < / a > < span class = "w" > < / span > < span class = "k" > for< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "n" > i< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "m" > 0< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > j< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > front< / span > < span class = "p" > ;< / span > < span class = "w" > < / span > < span class = "n" > i< / span > < span class = "w" > < / span > < span class = "o" > < < / span > < span class = "w" > < / span > < span class = "n" > queSize< / span > < span class = "p" > ;< / span > < span class = "w" > < / span > < span class = "n" > i< / span > < span class = "o" > ++< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > j< / span > < span class = "o" > ++< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-27-102" name = "__codelineno-27-102" href = "#__codelineno-27-102" > < / a > < span class = "w" > < / span > < span class = "n" > res< / span > < span class = "p" > [< / span > < span class = "n" > i< / span > < span class = "p" > ]< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > nums< / span > < span class = "p" > [< / span > < span class = "n" > index< / span > < span class = "p" > (< / span > < span class = "n" > j< / span > < span class = "p" > )];< / span >
< a id = "__codelineno-27-103" name = "__codelineno-27-103" href = "#__codelineno-27-103" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-27-104" name = "__codelineno-27-104" href = "#__codelineno-27-104" > < / a > < span class = "w" > < / span > < span class = "k" > return< / span > < span class = "w" > < / span > < span class = "n" > res< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-27-105" name = "__codelineno-27-105" href = "#__codelineno-27-105" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-27-106" name = "__codelineno-27-106" href = "#__codelineno-27-106" > < / a > < span class = "p" > }< / span >
< / code > < / pre > < / div >
< / div >
< div class = "tabbed-block" >