-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcoinRing.go
53 lines (45 loc) · 970 Bytes
/
coinRing.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
package main
// represents a circular list of ShapeShift coins
type coinRing interface {
Next() coinRing
Prev() coinRing
Value() *Coin
}
// linked list based implementation of coinRing
type coinNode struct {
next, prev *coinNode
coin *Coin
}
type ringItem interface {
Text() string
}
// implements coinRing.Value()
func (c *coinNode) Value() *Coin {
return c.coin
}
// implements coinRing.Prev()
func (c *coinNode) Prev() coinRing {
return c.prev
}
// implements coinList.Next()
func (c *coinNode) Next() coinRing {
return c.next
}
// Take a list of coins and convert them to a circular coinRing
func toCoinRing(coins []*Coin) *coinNode {
if len(coins) == 0 {
return nil
}
// TODO: fix edge case of 1 element list
start := &coinNode{coin: coins[0]}
prev := start
for i := 1; i < len(coins); i++ {
cur := coins[i]
n := &coinNode{coin: cur, prev: prev}
prev.next = n
prev = n
}
prev.next = start
start.prev = prev
return start
}