Skip to content

Commit 34ba206

Browse files
author
Dean Karn
authored
List node insert functions (#24)
1 parent ca069ea commit 34ba206

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# pkg
22

3-
![Project status](https://img.shields.io/badge/version-5.12.1-green.svg)
3+
![Project status](https://img.shields.io/badge/version-5.13.0-green.svg)
44
[![Build Status](https://travis-ci.org/go-playground/pkg.svg?branch=master)](https://travis-ci.org/go-playground/pkg)
55
[![Coverage Status](https://coveralls.io/repos/github/go-playground/pkg/badge.svg?branch=master)](https://coveralls.io/github/go-playground/pkg?branch=master)
66
[![GoDoc](https://godoc.org/github.com/go-playground/pkg?status.svg)](https://pkg.go.dev/mod/github.com/go-playground/pkg/v5)

container/list/doubly_linked.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,20 @@ func (d *DoublyLinkedList[V]) PushBefore(node *Node[V], v V) *Node[V] {
140140
return newNode
141141
}
142142

143+
// InsertBefore inserts the supplied node before the supplied node.
144+
//
145+
// The supplied node must be attached to the current list otherwise undefined behaviour could occur.
146+
func (d *DoublyLinkedList[V]) InsertBefore(node *Node[V], inserting *Node[V]) {
147+
d.moveBefore(node, inserting)
148+
}
149+
150+
// InsertAfter inserts the supplied node after the supplied node.
151+
//
152+
// The supplied node must be attached to the current list otherwise undefined behaviour could occur.
153+
func (d *DoublyLinkedList[V]) InsertAfter(node *Node[V], inserting *Node[V]) {
154+
d.moveAfter(node, inserting)
155+
}
156+
143157
// MoveBefore moves the `moving` node before the supplied `node`.
144158
//
145159
// The supplied `node` and `moving` nodes must be attached to the current list otherwise
@@ -223,6 +237,13 @@ func (d *DoublyLinkedList[V]) MoveToFront(node *Node[V]) {
223237
d.pushFront(node)
224238
}
225239

240+
// InsertAtFront pushes the provided node to the front/head.
241+
//
242+
// The supplied node must not be attached to any list otherwise undefined behaviour could occur.
243+
func (d *DoublyLinkedList[V]) InsertAtFront(node *Node[V]) {
244+
d.pushFront(node)
245+
}
246+
226247
// MoveToBack moves the provided node to the end/tail.
227248
//
228249
// The supplied node must be attached to the current list otherwise undefined behaviour could occur.
@@ -231,6 +252,13 @@ func (d *DoublyLinkedList[V]) MoveToBack(node *Node[V]) {
231252
d.pushBack(node)
232253
}
233254

255+
// InsertAtBack pushes the provided node to the back/tail.
256+
//
257+
// The supplied node must not be attached to any list otherwise undefined behaviour could occur.
258+
func (d *DoublyLinkedList[V]) InsertAtBack(node *Node[V]) {
259+
d.pushBack(node)
260+
}
261+
234262
// IsEmpty returns true if the list is empty.
235263
func (d *DoublyLinkedList[V]) IsEmpty() bool {
236264
return d.len == 0

container/list/doubly_linked_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,32 @@ import (
99
"testing"
1010
)
1111

12+
func TestLinkedListInserts(t *testing.T) {
13+
l := NewDoublyLinked[int]()
14+
Equal(t, l.IsEmpty(), true)
15+
Equal(t, l.Len(), 0)
16+
17+
node1 := l.PushFront(1)
18+
node2 := l.PushFront(2)
19+
node3 := l.PushFront(3)
20+
21+
l.Remove(node2)
22+
l.InsertAtFront(node2)
23+
Equal(t, l.Front().Value, node2.Value)
24+
25+
l.Remove(node2)
26+
l.InsertAtBack(node2)
27+
Equal(t, l.Back().Value, node2.Value)
28+
29+
l.Remove(node2)
30+
l.InsertBefore(node3, node2)
31+
Equal(t, l.Front().Value, node2.Value)
32+
33+
l.Remove(node2)
34+
l.InsertAfter(node1, node2)
35+
Equal(t, l.Back().Value, node2.Value)
36+
}
37+
1238
func TestSingleEntryPopBack(t *testing.T) {
1339

1440
l := NewDoublyLinked[int]()

0 commit comments

Comments
 (0)