-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.go
79 lines (68 loc) · 1.29 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
package queue
import (
"fmt"
)
// Service is interface
type Service interface {
Push(key interface{}) bool
Pop() interface{}
Contains(key interface{}) bool
Len() int
Keys() []interface{}
}
type customBridge struct {
Queue []interface{}
}
func (c *customBridge) Push(key interface{}) bool {
c.Queue = append(c.Queue, key)
return true
}
func (c *customBridge) Pop() interface{} {
if len(c.Queue) <= 0 {
defer func() {
if r := recover(); r != nil {
fmt.Println("Data is empty")
}
}()
}
var resEl = c.Queue[0]
fmt.Println("Take :", resEl)
c.Queue = c.Queue[0+1:]
return resEl
}
func (c *customBridge) Keys() []interface{} {
if len(c.Queue) <= 0 {
fmt.Println("Data is empty")
}
var data = make([]interface{}, 0)
for _, each := range c.Queue {
data = append(data, each)
}
return data
}
func (c *customBridge) Len() int {
if len(c.Queue) <= 0 {
fmt.Println("Data is empty")
}
return len(c.Queue)
}
func (c *customBridge) Contains(key interface{}) bool {
var exist bool
if len(c.Queue) == 1 {
exist = true
} else {
for _, g := range c.Queue[:len(c.Queue)-1] {
if key == g {
c.Queue = c.Queue[:len(c.Queue)-1]
exist = false
} else {
exist = true
}
}
}
return exist
}
// New is take main func
func New() Service {
return &customBridge{}
}