-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
97 lines (77 loc) · 1.96 KB
/
main.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
package main
import (
"fmt"
"strings"
"github.com/victor-iyiola/go-algorithms/ds"
)
func main() {
// Trie example.
trie := ds.NewTrieFromChar('*')
usingTrie(trie)
divider()
// Stack example.
stack := ds.NewStack()
usingLinearDS("Stack", stack)
divider()
// Queue example.
queue := ds.NewQueue()
usingLinearDS("Queue", queue)
}
func divider(count ...int) {
if count == nil {
count = append(count, 50)
}
fmt.Printf("\n%s\n\n", strings.Repeat("=", count[0]))
}
func usingLinearDS(name string, linear ds.LinearDS) {
// Push elements to the dataStructure.
linear.Push(3.14)
linear.Push(13.)
linear.Push(.21)
linear.Push(526.1)
linear.Push(190.21)
// Print empty status.
fmt.Printf("%s is ", name)
if linear.Empty() {
fmt.Println("empty")
return // End the function if dataStructure is empty.
}
fmt.Println("not empty")
// Number of elements contained in a dataStructure.
fmt.Printf("%s contains: %d element(s)\n", name, linear.Count())
// Peek into the dataStructure.
peek, err := linear.Peek()
if err != nil {
fmt.Printf("Cannot peek [%s]\n", err)
} else {
fmt.Println("dataStructure.Peek() =", peek)
}
// Pop last element from stack.
for i := 0; i < linear.Count(); i++ {
pop, err := linear.Pop()
if err != nil {
fmt.Printf("Cannot pop [%s]\n", err)
} else {
fmt.Println("dataStructure.Pop() =", pop)
}
}
}
func usingTrie(t *ds.Trie) {
// Add single word to the trie.
t.Push("bell")
t.Push("base")
t.Push("beat")
t.Push("bellman")
// Add multiple words to the trie.
t.AddAll("victor victoria victory victories")
t.AddAll("programming terminal commit async escape milk terminate program")
// Print trie contents.
ds.Print(t.Root)
// Check occurrence of some word prefix in the trie.
hasVic, countVic := t.Find("vic")
hasProg := t.Contains("prog")
// Print results.
fmt.Printf("root.Char = %c\n", t.Root.Char)
fmt.Printf("hasVic = %t\tcountVic = %d\n", hasVic, countVic)
fmt.Printf("hasProg = %t\n", hasProg)
}