Skip to content

Instantly share code, notes, and snippets.

@zhangyunhao116
Last active April 21, 2021 06:10
Show Gist options
  • Save zhangyunhao116/833c3113db343a660a2adb1e4c21951d to your computer and use it in GitHub Desktop.
Save zhangyunhao116/833c3113db343a660a2adb1e4c21951d to your computer and use it in GitHub Desktop.
simplelist
package simplelist
import (
"sync"
)
type IntList struct {
head *intNode
length int64
mu sync.RWMutex
}
type intNode struct {
value int
next *intNode
}
func newIntNode(value int) *intNode {
return &intNode{value: value}
}
func NewInt() *IntList {
return &IntList{head: newIntNode(0)}
}
func (l *IntList) Insert(value int) bool {
l.mu.Lock()
defer l.mu.Unlock()
a := l.head
b := a.next
for b != nil && b.value < value {
a = b
b = b.next
}
// Check if the node is exist.
if b != nil && b.value == value {
return false
}
x := newIntNode(value)
x.next = b
a.next = x
l.length++
return true
}
func (l *IntList) Delete(value int) bool {
l.mu.Lock()
defer l.mu.Unlock()
a := l.head
b := a.next
for b != nil && b.value < value {
a = b
b = b.next
}
// Check if b is not exists
if b == nil || b.value != value {
return false
}
a.next = b.next
l.length--
return true
}
func (l *IntList) Contains(value int) bool {
l.mu.RLock()
defer l.mu.RUnlock()
x := l.head.next
for x != nil && x.value < value {
x = x.next
}
if x == nil {
return false
}
return x.value == value
}
func (l *IntList) Range(f func(value int) bool) {
l.mu.RLock()
x := l.head.next
l.mu.RUnlock()
for x != nil {
if !f(x.value) {
break
}
l.mu.RLock()
x = x.next
l.mu.RUnlock()
}
}
func (l *IntList) Len() int {
return int(l.length)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment