-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
216 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package usecase | ||
|
||
type doublyLinkedList[T any] struct { | ||
head *item[T] | ||
tail *item[T] | ||
} | ||
|
||
type item[T any] struct { | ||
prev *item[T] | ||
next *item[T] | ||
value T | ||
} | ||
|
||
func (l *doublyLinkedList[T]) empty() bool { | ||
return l.head == nil | ||
} | ||
|
||
func (l *doublyLinkedList[T]) append(v T) *item[T] { | ||
newItem := &item[T]{ | ||
value: v, | ||
prev: l.tail, | ||
} | ||
if l.head == nil { | ||
// empty list, item becomes first and last item | ||
l.head = newItem | ||
} else { | ||
// non-empty list, item becomes last item | ||
l.tail.next = newItem | ||
} | ||
l.tail = newItem | ||
return newItem | ||
} | ||
|
||
func (l *doublyLinkedList[T]) remove(this *item[T]) { | ||
if this.prev == nil { | ||
// no prev, this is head, next becomes head | ||
l.head = this.next | ||
} else { | ||
// there's items after this | ||
this.prev.next = this.next | ||
} | ||
if this.next == nil { | ||
// this is tail, so prev becomes tail | ||
l.tail = this.prev | ||
} else { | ||
// this isn't tail (more items after this), so prev becomes tail | ||
this.next.prev = this.prev | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package usecase | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func Test_doublyLinkedList_append(t *testing.T) { | ||
t.Run("1 entry", func(t *testing.T) { | ||
list := new(doublyLinkedList[int]) | ||
list.append(1) | ||
assert.Equal(t, 1, list.head.value) | ||
assert.Equal(t, 1, list.tail.value) | ||
}) | ||
t.Run("2 entries", func(t *testing.T) { | ||
list := new(doublyLinkedList[int]) | ||
list.append(1) | ||
list.append(2) | ||
assert.Equal(t, 1, list.head.value) | ||
assert.Equal(t, 2, list.tail.value) | ||
assert.Equal(t, list.tail, list.head.next) | ||
assert.Equal(t, list.head, list.tail.prev) | ||
}) | ||
} | ||
|
||
func Test_doublyLinkedList_empty(t *testing.T) { | ||
t.Run("empty", func(t *testing.T) { | ||
list := new(doublyLinkedList[int]) | ||
assert.True(t, list.empty()) | ||
}) | ||
t.Run("non-empty", func(t *testing.T) { | ||
list := new(doublyLinkedList[int]) | ||
list.append(1) | ||
assert.False(t, list.empty()) | ||
}) | ||
} | ||
|
||
func Test_doublyLinkedList_remove(t *testing.T) { | ||
t.Run("remove head", func(t *testing.T) { | ||
list := new(doublyLinkedList[int]) | ||
first := list.append(1) | ||
list.append(2) | ||
list.remove(first) | ||
assert.Same(t, list.tail, list.head) | ||
assert.Equal(t, 2, list.head.value) | ||
assert.Nil(t, list.head.prev) | ||
assert.Nil(t, list.head.next) | ||
}) | ||
t.Run("remove tail", func(t *testing.T) { | ||
list := new(doublyLinkedList[int]) | ||
list.append(1) | ||
last := list.append(2) | ||
list.remove(last) | ||
assert.Same(t, list.tail, list.head) | ||
assert.Equal(t, 1, list.head.value) | ||
assert.Nil(t, list.head.prev) | ||
assert.Nil(t, list.head.next) | ||
}) | ||
t.Run("remove middle", func(t *testing.T) { | ||
list := new(doublyLinkedList[int]) | ||
first := list.append(1) | ||
middle := list.append(2) | ||
last := list.append(3) | ||
list.remove(middle) | ||
assert.Equal(t, 1, list.head.value) | ||
assert.Equal(t, 3, list.tail.value) | ||
assert.Same(t, first, list.head) | ||
assert.Same(t, last, list.tail) | ||
assert.Same(t, last, list.head.next) | ||
assert.Same(t, first, list.tail.prev) | ||
}) | ||
t.Run("empty list after remove of last item", func(t *testing.T) { | ||
list := new(doublyLinkedList[int]) | ||
first := list.append(1) | ||
second := list.append(2) | ||
list.remove(first) | ||
list.remove(second) | ||
assert.Nil(t, list.head) | ||
assert.Nil(t, list.tail) | ||
assert.True(t, list.empty()) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters