-
Notifications
You must be signed in to change notification settings - Fork 1
/
Queue.go
55 lines (42 loc) · 844 Bytes
/
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
package main
import "fmt"
var queue []int
const queueLen = 3
var rear = -1
func main() {
enqueue(10)
enqueue(11)
enqueue(12)
enqueue(13)
fmt.Println(queue)
fmt.Println("Dequeued ", dequeue())
fmt.Println("Dequeued ", dequeue())
fmt.Println("Dequeued ", dequeue())
fmt.Println("Dequeued ", dequeue())
enqueue(120)
enqueue(130)
fmt.Println(queue)
fmt.Println("Dequeued ", dequeue())
fmt.Println("Dequeued ", dequeue())
}
func enqueue(item int){
fmt.Println("length: " , len(queue))
if len(queue) == queueLen {
fmt.Println("Queue is full")
return
} else {
rear++
queue = append(queue, item)
fmt.Printf("Item %d added to queue successfully\n", item)
}
}
func dequeue() (item int){
if len(queue) == 0 {
fmt.Println("Queue is empty")
return
}
item = queue[0]
queue = queue[1:]
rear--
return item
}