forked from oleiade/lane
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_lane_test.go
112 lines (87 loc) · 2.64 KB
/
example_lane_test.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
package lane
import (
"fmt"
"strings"
)
func ExamplePQueue() {
// Let's create a new max ordered priority queue
var priorityQueue *PQueue = NewPQueue(MINPQ)
// And push some prioritized content into it
priorityQueue.Push("easy as", 3)
priorityQueue.Push("123", 2)
priorityQueue.Push("do re mi", 4)
priorityQueue.Push("abc", 1)
// Now let's take a look at the min element in
// the priority queue
headValue, headPriority := priorityQueue.Head()
fmt.Println(headValue) // "abc"
fmt.Println(headPriority) // 1
// Okay the song order seems to be preserved, let's
// roll
var jacksonFive []string = make([]string, priorityQueue.Size())
for i := 0; i < len(jacksonFive); i++ {
value, _ := priorityQueue.Pop()
jacksonFive[i] = value.(string)
}
fmt.Println(strings.Join(jacksonFive, " "))
}
func ExampleDeque() {
// Let's create a new deque data structure
var deque *Deque = NewDeque()
// And push some content into it using the Append
// and Prepend methods
deque.Append("easy as")
deque.Prepend("123")
deque.Append("do re mi")
deque.Prepend("abc")
// Now let's take a look at what are the first and
// last element stored in the Deque
firstValue := deque.First()
lastValue := deque.Last()
fmt.Println(firstValue) // "abc"
fmt.Println(lastValue) // 1
// Okay now let's play with the Pop and Shift
// methods to bring the song words together
var jacksonFive []string = make([]string, deque.Size())
for i := 0; i < len(jacksonFive); i++ {
value := deque.Shift()
jacksonFive[i] = value.(string)
}
// abc 123 easy as do re mi
fmt.Println(strings.Join(jacksonFive, " "))
}
func ExampleQueue() {
// Create a new queue and pretend we're handling starbucks
// clients
var queue *Queue = NewQueue()
// Let's add the incoming clients to the queue
queue.Enqueue("grumpyClient")
queue.Enqueue("happyClient")
queue.Enqueue("ecstaticClient")
fmt.Println(queue.Head) // grumpyClient
// Let's handle the clients asynchronously
for client := queue.Dequeue(); client != nil; {
go fmt.Println(client)
}
}
func ExampleStack() {
// Create a new stack and put some plates over it
var stack *Stack = NewStack()
// Let's put some plates on the stack
stack.Push("redPlate")
stack.Push("bluePlate")
stack.Push("greenPlate")
fmt.Println(stack.Head) // greenPlate
// What's on top of the stack?
value := stack.Pop()
fmt.Println(value.(string)) // greenPlate
stack.Push("yellowPlate")
value = stack.Pop()
fmt.Println(value.(string)) // yellowPlate
// What's on top of the stack?
value = stack.Pop()
fmt.Println(value.(string)) // bluePlate
// What's on top of the stack?
value = stack.Pop()
fmt.Println(value.(string)) // redPlate
}