-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathqueue.go
107 lines (97 loc) · 2.07 KB
/
queue.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
package queue
type MyCircularQueue struct {
head *node
tail *node
size int
}
type node struct {
next *node
value interface{}
}
/** Initialize your data structure here. Set the size of the queue to be k. */
func Constructor(k int) MyCircularQueue {
return MyCircularQueue{
size: k,
}
}
/** Insert an element into the circular queue. Return true if the operation is successful. */
func (this *MyCircularQueue) EnQueue(value int) bool {
if this.size == 0 {
return false
} else {
n := &node{
value: value,
}
if this.tail == nil {
this.head = n
this.tail = n
this.size--
} else {
this.tail.next = n
this.tail = n
this.size--
}
return true
}
}
/** Delete an element from the circular queue. Return true if the operation is successful. */
func (this *MyCircularQueue) DeQueue() bool {
if this.head == nil {
return false
} else {
if this.head == this.tail {
this.head, this.tail = nil, nil
this.size++
} else {
this.head = this.head.next
this.size++
}
return true
}
}
/** Get the front item from the queue. */
func (this *MyCircularQueue) Front() int {
if this.head == nil {
return -1
}
if v, ok := this.head.value.(int); ok {
return v
} else {
return -1
}
}
/** Get the last item from the queue. */
func (this *MyCircularQueue) Rear() int {
if this.tail == nil {
return -1
}
if v, ok := this.tail.value.(int); ok {
return v
} else {
return -1
}
}
/** Checks whether the circular queue is empty or not. */
func (this *MyCircularQueue) IsEmpty() bool {
if (this.head == nil || this.tail == nil) && this.size != 0 {
return true
}
return false
}
/** Checks whether the circular queue is full or not. */
func (this *MyCircularQueue) IsFull() bool {
if this.head != nil && this.tail != nil && this.size == 0 {
return true
}
return false
}
/**
* Your MyCircularQueue object will be instantiated and called as such:
* obj := Constructor(k);
* param_1 := obj.EnQueue(value);
* param_2 := obj.DeQueue();
* param_3 := obj.Front();
* param_4 := obj.Rear();
* param_5 := obj.IsEmpty();
* param_6 := obj.IsFull();
*/