-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem_06.go
More file actions
47 lines (37 loc) · 813 Bytes
/
problem_06.go
File metadata and controls
47 lines (37 loc) · 813 Bytes
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
package main
import "fmt"
/**
DISCLAMER : XOR list does not really work with go's GC
*/
type XORNode struct {
data int
both *XORNode
}
func XOR(left, right *XORNode) int {
return left.data ^ right.data
}
func (node *XORNode) add(data int) {
newNode := &XORNode{}
newNode.data = data
newNode.both = &XORNode{data: XOR(node, nil)}
if node != nil {
next := &XORNode{data: XOR(newNode.both, nil)}
node.both = &XORNode{data: XOR(newNode, next)}
} else {
node = newNode
}
}
func (node *XORNode) get(index int) *XORNode {
current := node
prev := &XORNode{}
next := &XORNode{}
for ; index > 0 && next != nil; index-- {
next = &XORNode{data: XOR(prev, current)}
prev = current
current = next
if next == nil && index > 1 {
fmt.Errorf("Index out of bound!")
}
}
return current
}