-
Notifications
You must be signed in to change notification settings - Fork 10
/
list.go
60 lines (52 loc) · 953 Bytes
/
list.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
// Copyright (c) 2023 cheng-zhongliang. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package event
type element struct {
next, prev *element
list *list
value interface{}
}
func (e *element) nextEle() *element {
if p := e.next; e.list != nil && p != &e.list.root {
return p
}
return nil
}
type list struct {
root element
len int
}
func newList() *list {
l := new(list)
l.root.next = &l.root
l.root.prev = &l.root
l.len = 0
return l
}
func (l *list) front() *element {
if l.len == 0 {
return nil
}
return l.root.next
}
func (l *list) pushBack(v interface{}, e *element) *element {
e.list = l
e.value = v
n := &l.root
p := n.prev
e.next = n
e.prev = p
p.next = e
n.prev = e
l.len++
return e
}
func (l *list) remove(e *element) {
e.prev.next = e.next
e.next.prev = e.prev
e.next = nil
e.prev = nil
e.list = nil
l.len--
}