type node struct { undo int prev int next int } type Residue struct { nodes []node } func NewResidue(n int) *Residue { nodes := make([]node, 1+n) nodes[0].undo = -1 if n > 0 { nodes[0].prev = n nodes[0].next = 1 for x := 1; x < n; x++ { nodes[x].prev = x - 1 nodes[x].next = x + 1 } nodes[n].prev = n - 1 } return &Residue{ nodes: nodes, } } func (r *Residue) Prev(x int) int { if r.nodes[x].undo != 0 { x = 0 } return r.nodes[x].prev } func (r *Residue) Next(x int) int { if r.nodes[x].undo != 0 { x = 0 } return r.nodes[x].next } func (r *Residue) Contains(x int) bool { return r.nodes[x].undo == 0 } func (r *Residue) Remove(x int) bool { if r.nodes[x].undo != 0 { return false } r.nodes[x].undo = r.nodes[0].undo r.nodes[0].undo = 0 var ( i1 = r.nodes[x].prev i2 = r.nodes[x].next ) r.nodes[i1].next = i2 r.nodes[i2].prev = i1 return true } func (r *Residue) Undo() bool { x := r.nodes[0].undo if x == -1 { return false } r.nodes[0].undo = r.nodes[x].undo r.nodes[x].undo = 0 var ( i1 = r.nodes[x].prev i2 = r.nodes[x].next ) r.nodes[i1].next = x r.nodes[i2].prev = x return true }
To receive a hint, submit unfixed code.