Skip to content

Commit 95af479

Browse files
committed
basic linked list and node. generic for ints or strings. TODO: make generic for anything.
1 parent eaf54d4 commit 95af479

File tree

5 files changed

+129
-0
lines changed

5 files changed

+129
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.tool-versions
2+
.vscode

go.mod

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module github.com/killbot99/ds
2+
3+
go 1.18
4+
5+
require github.com/stretchr/testify v1.8.2
6+
7+
require (
8+
github.com/davecgh/go-spew v1.1.1 // indirect
9+
github.com/pmezard/go-difflib v1.0.0 // indirect
10+
gopkg.in/yaml.v3 v3.0.1 // indirect
11+
)

go.sum

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
5+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
6+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
7+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
8+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
9+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
10+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
11+
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
12+
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
13+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
14+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
15+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
16+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
17+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

pkg/linkedlist.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package ds
2+
3+
type Node[T int | string] interface {
4+
// Returns the next node
5+
Next() Node[T]
6+
7+
// creates a new node and sets it as the Next of the current node
8+
SetNext(T) Node[T]
9+
10+
// Return the value of the Node data
11+
Value() T
12+
}
13+
14+
type node[T int | string] struct {
15+
data T
16+
next Node[T]
17+
}
18+
19+
// returns the next
20+
func (n *node[T]) Next() Node[T] {
21+
return n.next
22+
}
23+
24+
func (n *node[T]) Value() T {
25+
return n.data
26+
}
27+
28+
func (n *node[T]) SetNext(val T) Node[T] {
29+
n.next = NewNode(val)
30+
return n.Next()
31+
}
32+
33+
type List[T int | string] interface {
34+
First() Node[T]
35+
Last() Node[T]
36+
Append(T)
37+
}
38+
39+
func (l *list[T]) First() Node[T] {
40+
return l.first
41+
}
42+
43+
func (l *list[T]) Last() Node[T] {
44+
current := l.First()
45+
46+
for current.Next() != nil {
47+
current = current.Next()
48+
}
49+
50+
return current
51+
}
52+
53+
func (l *list[T]) Append(val T) {
54+
last := l.Last()
55+
last.SetNext(val)
56+
}
57+
58+
type list[T int | string] struct {
59+
first Node[T]
60+
}
61+
62+
func NewLinkedList[T int | string](data T) List[T] {
63+
n := NewNode(data)
64+
return &list[T]{
65+
first: n,
66+
}
67+
}
68+
69+
func NewNode[T int | string](data T) Node[T] {
70+
n := node[T]{
71+
data: data,
72+
}
73+
return &n
74+
}

pkg/linkedlist_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package ds
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func Test_LinkedList(t *testing.T) {
10+
t.Run("single node with int", func(t *testing.T) {
11+
ll := NewLinkedList[int](5)
12+
assert.NotNil(t, ll)
13+
assert.Equal(t, 5, ll.First().Value())
14+
assert.Equal(t, 5, ll.Last().Value())
15+
assert.Nil(t, ll.Last().Next())
16+
})
17+
t.Run("list with three int values", func(t *testing.T) {
18+
ll := NewLinkedList[int](10)
19+
ll.Append(5)
20+
ll.Append(7)
21+
assert.Equal(t, 10, ll.First().Value())
22+
assert.Equal(t, 7, ll.Last().Value())
23+
assert.Nil(t, ll.Last().Next())
24+
})
25+
}

0 commit comments

Comments
 (0)