-
Notifications
You must be signed in to change notification settings - Fork 20
/
SliceDeque.go
176 lines (152 loc) · 3.45 KB
/
SliceDeque.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"fmt"
"math/rand"
"time"
)
type Any interface{}
type Deque struct {
depan []Any
belakang []Any
}
// Create a new deque: O(1)
func NewDeque() *Deque {
var q Deque
q.depan = make([]Any,0,0)
q.belakang = make([]Any,0,0)
return &q
}
// Return size: O(1)
func (q *Deque) Count() int {
return (len(q.depan)+len(q.belakang))
}
// Text whether deque is empty: O(1)
func (q *Deque) IsEmpty() bool {
if (len(q.depan)+len(q.belakang)) == 0 { return true }
return false
}
// Access first element: O(1)
func (q *Deque) Front() Any {
if len(q.depan) == 0 { return nil }
return q.depan[0]
}
// Access last element: O(1)
func (q *Deque) Back() Any {
if len(q.belakang) == 0 { return nil }
return q.belakang[len(q.belakang)-1]
}
// Insert new element into the end of deque: O(1)
func (q *Deque) enqueueBack(data Any) {
q.belakang = append(q.belakang,data)
}
// Insert new element into the head of deque: O(1)
func (q *Deque) enqueueFront(data Any) {
q.depan = append(q.depan,data)
}
// Remove first element from deque: O(1)
func (q *Deque) dequeueFront() Any {
if len(q.depan) == 0 {
q.depan = q.belakang
data := q.depan[0]
q.depan = q.depan[1:]
for i:=0;i<len(q.belakang);i++ {
q.belakang = make([]Any,0,0)
}
return data
} else {
data := q.depan[len(q.depan)-1]
q.depan = q.depan[0:(len(q.depan)-1)]
return data
}
}
// Remove last element from deque: O(1)
func (q *Deque) dequeueBack() Any {
if len(q.belakang) == 0 {
data := q.depan[0]
q.depan = q.depan[1:(len(q.depan)-1)]
return data
} else {
data := q.belakang[(len(q.belakang)-1)]
q.belakang = q.belakang[0:(len(q.belakang)-1)]
return data
}
}
// Print elements from deque: O(N)
func (q *Deque) Print() {
if (len(q.depan)+len(q.belakang)) == 0 { fmt.Println("| Empty |"); return }
fmt.Print("|")
var k,i,j int
k=0
for i = (len(q.depan)-1); i>=0; i-- {
fmt.Printf(" %d: %#v |", k, q.depan[i])
k++
}
i=len(q.depan)+1
for j=0; j<len(q.belakang); j++ {
fmt.Printf(" %d: %#v |", i, q.belakang[j])
i++
}
fmt.Println()
}
func init() {
rand.Seed(time.Now().Unix())
}
func RandInt(min, max int) int {
return rand.Intn(max - min) + min
}
func main() {
q := NewDeque()
fmt.Println("Enqueue Front Numbers *Remember The Last One Become The Head*")
for i := 0; i < 6; i++ {
a := RandInt(0,100)
fmt.Print(a)
if i == 5 { fmt.Println() } else { fmt.Print(" ") }
q.enqueueFront(a)
}
q.Print()
fmt.Println()
fmt.Println("Enqueue Back String")
s:= "abc"
fmt.Println(s)
q.enqueueBack(s)
q.Print()
fmt.Println()
fmt.Println("Dequeue Front")
for i := 0; i < 6; i++ {
fmt.Print(q.dequeueFront())
if i == 5 { fmt.Println() } else { fmt.Print(" ") }
}
q.Print()
fmt.Println()
fmt.Println("Enqueue Back Numbers")
for i := 0; i < 6; i++ {
a := RandInt(0,100)
fmt.Print(a)
if i == 5 { fmt.Println() } else { fmt.Print(" ") }
q.enqueueBack(a)
}
q.Print()
fmt.Println()
fmt.Println("Dequeue Back All")
for i := 0; i < 7; i++ {
fmt.Print(q.dequeueBack())
if i == 6 { fmt.Println() } else { fmt.Print(" ") }
}
q.Print()
fmt.Println()
fmt.Println("Enqueue Front Numbers *Remember The Last One Become The Head*")
for i := 0; i < 6; i++ {
a := RandInt(0,100)
fmt.Print(a)
if i == 5 { fmt.Println() } else { fmt.Print(" ") }
q.enqueueFront(a)
}
q.Print()
fmt.Println()
fmt.Println("Dequeue Front All")
for i := 0; i < 6; i++ {
fmt.Print(q.dequeueFront())
if i == 5 { fmt.Println() } else { fmt.Print(" ") }
}
q.Print()
}