This repository was archived by the owner on Sep 20, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathdesign-circular-deque.go
executable file
·127 lines (116 loc) · 2.43 KB
/
design-circular-deque.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package problem0641
// MyCircularDeque 结构体
type MyCircularDeque struct {
f, r *node
len, cap int
}
type node struct {
value int
pre, next *node
}
// Constructor initialize your data structure here. Set the size of the deque to be k.
func Constructor(k int) MyCircularDeque {
return MyCircularDeque{
cap: k,
}
}
// InsertFront adds an item at the front of Deque. Return true if the operation is successful.
func (d *MyCircularDeque) InsertFront(value int) bool {
if d.len == d.cap {
return false
}
n := &node{
value: value,
}
if d.len == 0 {
d.f = n
d.r = n
} else {
n.next = d.f
d.f.pre = n
d.f = n
}
d.len++
return true
}
// InsertLast adds an item at the rear of Deque. Return true if the operation is successful.
func (d *MyCircularDeque) InsertLast(value int) bool {
if d.len == d.cap {
return false
}
n := &node{
value: value,
}
if d.len == 0 {
d.f = n
d.r = n
} else {
n.pre = d.r
d.r.next = n
d.r = n
}
d.len++
return true
}
// DeleteFront deletes an item from the front of Deque. Return true if the operation is successful.
func (d *MyCircularDeque) DeleteFront() bool {
if d.len == 0 {
return false
}
if d.len == 1 {
d.f, d.r = nil, nil
} else {
d.f = d.f.next
d.f.pre = nil
}
d.len--
return true
}
// DeleteLast deletes an item from the rear of Deque. Return true if the operation is successful.
func (d *MyCircularDeque) DeleteLast() bool {
if d.len == 0 {
return false
}
if d.len == 1 {
d.f, d.r = nil, nil
} else {
d.r = d.r.pre
d.r.next = nil
}
d.len--
return true
}
// GetFront get the front item from the deque.
func (d *MyCircularDeque) GetFront() int {
if d.len == 0 {
return -1
}
return d.f.value
}
// GetRear get the last item from the deque.
func (d *MyCircularDeque) GetRear() int {
if d.len == 0 {
return -1
}
return d.r.value
}
// IsEmpty checks whether the circular deque is empty or not.
func (d *MyCircularDeque) IsEmpty() bool {
return d.len == 0
}
// IsFull checks whether the circular deque is full or not.
func (d *MyCircularDeque) IsFull() bool {
return d.len == d.cap
}
/**
* Your MyCircularDeque object will be instantiated and called as such:
* obj := Constructor(k);
* param_1 := obj.InsertFront(value);
* param_2 := obj.InsertLast(value);
* param_3 := obj.DeleteFront();
* param_4 := obj.DeleteLast();
* param_5 := obj.GetFront();
* param_6 := obj.GetRear();
* param_7 := obj.IsEmpty();
* param_8 := obj.IsFull();
*/