-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathsimple_circular_queue.go
157 lines (141 loc) · 3.51 KB
/
simple_circular_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
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
/*# Copyright (c) July 12, 2020 CareerMonk Publications and others.
# E-Mail : [email protected]
# Creation Date : 2020-07-12 06:15:46
# Last modification : 2020-07-12
# by : Narasimha Karumanchi
# Book Title : Data Structures And Algorithmic Thinking With Go
# Warranty : This software is provided "as is" without any
# warranty; without even the implied warranty of
# merchantability or fitness for a particular purpose. */
package main
import (
"bytes"
"fmt"
)
const MaxInt = int(^uint(0) >> 1)
const MinInt = -MaxInt - 1
type Queue struct {
// enQueue increments rear then writes to array[rear] ; enQueue
// writes to array[front] then decrements front; len(array) is a power
// of two; unused slots are nil and not garbage.
array []interface{}
front int
rear int
capacity int
size int
}
// New returns an initialized isEmpty queue.
func New(capacity int) *Queue {
return new(Queue).Init(capacity)
}
// Init initializes with capacity
func (q *Queue) Init(capacity int) *Queue {
q.array = make([]interface{}, capacity)
q.front, q.rear, q.size, q.capacity = -1, -1, 0, capacity
return q
}
// size returns the number of elements of queue q.
func (q *Queue) length() int {
return q.size
}
// isEmpty returns true if the queue q has no elements.
func (q *Queue) isEmpty() bool {
return q.size == 0
}
// isFull returns true if the queue q is at capacity.
func (q *Queue) isFull() bool {
return q.size == q.capacity
}
// String returns a string representation of queue q formatted
// from front to rear.
func (q *Queue) String() string {
var result bytes.Buffer
result.WriteByte('[')
j := q.front
for i := 0; i < q.size; i++ {
result.WriteString(fmt.Sprintf("%v", q.array[j]))
if i < q.size-1 {
result.WriteByte(' ')
}
j = (j + 1) % q.capacity
}
result.WriteByte(']')
return result.String()
}
// Front returns the first element of queue q or nil.
func (q *Queue) Front() interface{} {
// no need to check q.isEmpty(), unused slots are nil
return q.array[q.front]
}
// Back returns the last element of queue q or nil.
func (q *Queue) Back() interface{} {
return q.array[q.rear]
}
// enQueue inserts a new value v at the rear of queue q.
func (q *Queue) enQueue(v interface{}) {
if q.isFull() {
return
}
q.rear = (q.rear + 1) % q.capacity
q.array[q.rear] = v
if q.front == -1 {
q.front = q.rear
}
q.size++
}
// deQueue removes and returns the first element of queue q or MinInt.
func (q *Queue) deQueue() interface{} {
if q.isEmpty() {
return MinInt
}
data := q.array[q.front]
if q.front == q.rear {
q.front = -1
q.rear = -1
q.size = 0
} else {
q.front = (q.front + 1) % q.capacity
q.size -= 1
}
return data
}
func main() {
var q Queue
q.Init(10)
q.enQueue(1)
q.enQueue(6)
q.enQueue(7)
q.enQueue(8)
q.enQueue(9)
q.enQueue(2)
q.enQueue(3)
q.enQueue(4)
q.enQueue(5)
q.enQueue(6)
q.enQueue(7)
q.enQueue(8)
q.enQueue(9)
fmt.Println(q.String())
want := "[1 6 7 8 9 2 3 4 5 6]"
if s := q.String(); s != want {
fmt.Println("q.String() = ", s, "want = ", want)
}
fmt.Println(q.deQueue())
fmt.Println(q.deQueue())
fmt.Println(q.deQueue())
fmt.Println(q.deQueue())
fmt.Println(q.deQueue())
fmt.Println(q.deQueue())
fmt.Println(q.deQueue())
fmt.Println(q.deQueue())
fmt.Println(q.deQueue())
fmt.Println(q.deQueue())
fmt.Println(q.deQueue())
fmt.Println(q.deQueue())
fmt.Println(q.deQueue())
want = "[]"
fmt.Println(q.String())
if s := q.String(); s != want {
fmt.Println("q.String() = ", s, "want = ", want)
}
}