/
linked.go
207 lines (183 loc) · 3.84 KB
/
linked.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package linked
import (
"fmt"
"strings"
)
import (
"github.com/timtadh/data-structures/errors"
"github.com/timtadh/data-structures/list"
"github.com/timtadh/data-structures/types"
)
// A doubly linked list node.
type Node struct {
Data types.Hashable
Next, Prev *Node
}
// Compares the Data of the node to the passed element.
func (n *Node) Equals(b types.Equatable) bool {
switch x := b.(type) {
case *Node:
return n.Data.Equals(x.Data)
default:
return n.Data.Equals(b)
}
}
// Compares the Data of the node to the passed element.
func (n *Node) Less(b types.Sortable) bool {
switch x := b.(type) {
case *Node:
return n.Data.Less(x.Data)
default:
return n.Data.Less(b)
}
}
// Hashes the Data of the node to the passed element.
func (n *Node) Hash() int {
return n.Data.Hash()
}
// A doubly linked list. There is no synchronization.
// The fields are publically accessible to allow for easy customization.
type LinkedList struct {
Length int
Head *Node
Tail *Node
}
func New() *LinkedList {
return &LinkedList{
Length: 0,
Head: nil,
Tail: nil,
}
}
func (l *LinkedList) Size() int {
return l.Length
}
func (l *LinkedList) Items() (it types.KIterator) {
cur := l.Head
it = func() (item types.Hashable, _ types.KIterator) {
if cur == nil {
return nil, nil
}
item = cur.Data
cur = cur.Next
return item, it
}
return it
}
func (l *LinkedList) Backwards() (it types.KIterator) {
cur := l.Tail
it = func() (item types.Hashable, _ types.KIterator) {
if cur == nil {
return nil, nil
}
item = cur.Data
cur = cur.Prev
return item, it
}
return it
}
func (l *LinkedList) Has(item types.Hashable) bool {
for x, next := l.Items()(); next != nil; x, next = next() {
if x.Equals(item) {
return true
}
}
return false
}
func (l *LinkedList) Push(item types.Hashable) (err error) {
return l.EnqueBack(item)
}
func (l *LinkedList) Pop() (item types.Hashable, err error) {
return l.DequeBack()
}
func (l *LinkedList) EnqueFront(item types.Hashable) (err error) {
n := &Node{Data: item, Next: l.Head}
if l.Head != nil {
l.Head.Prev = n
} else {
l.Tail = n
}
l.Head = n
l.Length++
return nil
}
func (l *LinkedList) EnqueBack(item types.Hashable) (err error) {
n := &Node{Data: item, Prev: l.Tail}
if l.Tail != nil {
l.Tail.Next = n
} else {
l.Head = n
}
l.Tail = n
l.Length++
return nil
}
func (l *LinkedList) DequeFront() (item types.Hashable, err error) {
if l.Head == nil {
return nil, errors.Errorf("List is empty")
}
item = l.Head.Data
l.Head = l.Head.Next
if l.Head != nil {
l.Head.Prev = nil
} else {
l.Tail = nil
}
l.Length--
return item, nil
}
func (l *LinkedList) DequeBack() (item types.Hashable, err error) {
if l.Tail == nil {
return nil, errors.Errorf("List is empty")
}
item = l.Tail.Data
l.Tail = l.Tail.Prev
if l.Tail != nil {
l.Tail.Next = nil
} else {
l.Head = nil
}
l.Length--
return item, nil
}
func (l *LinkedList) First() (item types.Hashable) {
if l.Head == nil {
return nil
}
return l.Head.Data
}
func (l *LinkedList) Last() (item types.Hashable) {
if l.Tail == nil {
return nil
}
return l.Tail.Data
}
// Can be compared to any types.IterableContainer
func (l *LinkedList) Equals(b types.Equatable) bool {
if o, ok := b.(types.IterableContainer); ok {
return list.Equals(l, o)
} else {
return false
}
}
// Can be compared to any types.IterableContainer
func (l *LinkedList) Less(b types.Sortable) bool {
if o, ok := b.(types.IterableContainer); ok {
return list.Less(l, o)
} else {
return false
}
}
func (l *LinkedList) Hash() int {
return list.Hash(l)
}
func (l *LinkedList) String() string {
if l.Length <= 0 {
return "{}"
}
items := make([]string, 0, l.Length)
for item, next := l.Items()(); next != nil; item, next = next() {
items = append(items, fmt.Sprintf("%v", item))
}
return "{" + strings.Join(items, ", ") + "}"
}