Skip to content

Commit 28c14d7

Browse files
committed
go deque
1 parent fff09ce commit 28c14d7

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

go-note/algorithms.rst

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,51 @@ Queue
4242
func (q *Queue) Empty() bool {
4343
return len(q.items) == 0
4444
}
45+
46+
Deque
47+
--------------------------------------------------
48+
49+
.. code-block:: go
50+
51+
import (
52+
"container/list"
53+
"fmt"
54+
)
55+
56+
// 滑动窗口最大值
57+
58+
type Deque struct {
59+
ll *list.List
60+
}
61+
62+
func NewDeque() *Deque {
63+
return &Deque{ll: list.New()}
64+
}
65+
66+
func (dq *Deque) PushFront(x int) {
67+
dq.ll.PushFront(x)
68+
}
69+
70+
func (dq *Deque) PushBack(x int) {
71+
dq.ll.PushBack(x)
72+
}
73+
74+
func (dq *Deque) Pop() { // remove back
75+
dq.ll.Remove(dq.ll.Back())
76+
}
77+
78+
func (dq *Deque) PopFront() { // remove first
79+
dq.ll.Remove(dq.ll.Front())
80+
}
81+
82+
func (dq *Deque) Front() int {
83+
return dq.ll.Front().Value.(int)
84+
}
85+
86+
func (dq *Deque) Back() int {
87+
return dq.ll.Back().Value.(int)
88+
}
89+
90+
func (dq *Deque) Len() int {
91+
return dq.ll.Len()
92+
}

0 commit comments

Comments
 (0)