Skip to content

Commit ca069ea

Browse files
author
Dean Karn
authored
correct documentation for some Linked List Methods (#23)
1 parent 14d007e commit ca069ea

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

container/list/doubly_linked.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ func (d *DoublyLinkedList[V]) PushAfter(node *Node[V], v V) *Node[V] {
106106

107107
// MoveAfter moves the `moving` node after the supplied `node`.
108108
//
109-
// The supplied `node` must be attached to the current list and the `moving` node must either be attached to the
110-
// current list or not attached to any other otherwise undefined behaviour could occur.
109+
// The supplied `node` and `moving` nodes must be attached to the current list otherwise
110+
// undefined behaviour could occur.
111111
func (d *DoublyLinkedList[V]) MoveAfter(node *Node[V], moving *Node[V]) {
112112
// first detach node were moving after, in case it was already attached somewhere else in the list.
113113
d.Remove(moving)
@@ -142,8 +142,8 @@ func (d *DoublyLinkedList[V]) PushBefore(node *Node[V], v V) *Node[V] {
142142

143143
// MoveBefore moves the `moving` node before the supplied `node`.
144144
//
145-
// The supplied `node` must be attached to the current list and the `moving` node must either be attached to the
146-
// current list or not attached to any other otherwise undefined behaviour could occur.
145+
// The supplied `node` and `moving` nodes must be attached to the current list otherwise
146+
// undefined behaviour could occur.
147147
func (d *DoublyLinkedList[V]) MoveBefore(node *Node[V], moving *Node[V]) {
148148
// first detach node were moving after, in case it was already attached somewhere else in the list.
149149
d.Remove(moving)
@@ -216,12 +216,16 @@ func (d *DoublyLinkedList[V]) Remove(node *Node[V]) {
216216
}
217217

218218
// MoveToFront moves the provided node to the front/head.
219+
//
220+
// The supplied node must be attached to the current list otherwise undefined behaviour could occur.
219221
func (d *DoublyLinkedList[V]) MoveToFront(node *Node[V]) {
220222
d.Remove(node)
221223
d.pushFront(node)
222224
}
223225

224226
// MoveToBack moves the provided node to the end/tail.
227+
//
228+
// The supplied node must be attached to the current list otherwise undefined behaviour could occur.
225229
func (d *DoublyLinkedList[V]) MoveToBack(node *Node[V]) {
226230
d.Remove(node)
227231
d.pushBack(node)

container/list/doubly_linked_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,24 @@ func TestLinkedListMoving(t *testing.T) {
368368
Equal(t, l.Len(), 0)
369369
}
370370

371+
func TestLinkedListRemoveSingleEntry(t *testing.T) {
372+
373+
l := NewDoublyLinked[int]()
374+
Equal(t, l.IsEmpty(), true)
375+
Equal(t, l.Len(), 0)
376+
377+
// test pushing after with one node
378+
node := l.PushFront(0)
379+
Equal(t, l.IsEmpty(), false)
380+
Equal(t, l.Len(), 1)
381+
Equal(t, l.Front().Value, node.Value)
382+
Equal(t, l.Back().Value, node.Value)
383+
384+
l.Remove(node)
385+
Equal(t, l.IsEmpty(), true)
386+
Equal(t, l.Len(), 0)
387+
}
388+
371389
func BenchmarkDoublyLinkedList(b *testing.B) {
372390
for i := 0; i < b.N; i++ {
373391
l := NewDoublyLinked[int]()

0 commit comments

Comments
 (0)