Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# pkg

![Project status](https://img.shields.io/badge/version-5.12.1-green.svg)
![Project status](https://img.shields.io/badge/version-5.13.0-green.svg)
[![Build Status](https://travis-ci.org/go-playground/pkg.svg?branch=master)](https://travis-ci.org/go-playground/pkg)
[![Coverage Status](https://coveralls.io/repos/github/go-playground/pkg/badge.svg?branch=master)](https://coveralls.io/github/go-playground/pkg?branch=master)
[![GoDoc](https://godoc.org/github.com/go-playground/pkg?status.svg)](https://pkg.go.dev/mod/github.com/go-playground/pkg/v5)
Expand Down
28 changes: 28 additions & 0 deletions container/list/doubly_linked.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,20 @@ func (d *DoublyLinkedList[V]) PushBefore(node *Node[V], v V) *Node[V] {
return newNode
}

// InsertBefore inserts the supplied node before the supplied node.
//
// The supplied node must be attached to the current list otherwise undefined behaviour could occur.
func (d *DoublyLinkedList[V]) InsertBefore(node *Node[V], inserting *Node[V]) {
d.moveBefore(node, inserting)
}

// InsertAfter inserts the supplied node after the supplied node.
//
// The supplied node must be attached to the current list otherwise undefined behaviour could occur.
func (d *DoublyLinkedList[V]) InsertAfter(node *Node[V], inserting *Node[V]) {
d.moveAfter(node, inserting)
}

// MoveBefore moves the `moving` node before the supplied `node`.
//
// The supplied `node` and `moving` nodes must be attached to the current list otherwise
Expand Down Expand Up @@ -223,6 +237,13 @@ func (d *DoublyLinkedList[V]) MoveToFront(node *Node[V]) {
d.pushFront(node)
}

// InsertAtFront pushes the provided node to the front/head.
//
// The supplied node must not be attached to any list otherwise undefined behaviour could occur.
func (d *DoublyLinkedList[V]) InsertAtFront(node *Node[V]) {
d.pushFront(node)
}

// MoveToBack moves the provided node to the end/tail.
//
// The supplied node must be attached to the current list otherwise undefined behaviour could occur.
Expand All @@ -231,6 +252,13 @@ func (d *DoublyLinkedList[V]) MoveToBack(node *Node[V]) {
d.pushBack(node)
}

// InsertAtBack pushes the provided node to the back/tail.
//
// The supplied node must not be attached to any list otherwise undefined behaviour could occur.
func (d *DoublyLinkedList[V]) InsertAtBack(node *Node[V]) {
d.pushBack(node)
}

// IsEmpty returns true if the list is empty.
func (d *DoublyLinkedList[V]) IsEmpty() bool {
return d.len == 0
Expand Down
26 changes: 26 additions & 0 deletions container/list/doubly_linked_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,32 @@ import (
"testing"
)

func TestLinkedListInserts(t *testing.T) {
l := NewDoublyLinked[int]()
Equal(t, l.IsEmpty(), true)
Equal(t, l.Len(), 0)

node1 := l.PushFront(1)
node2 := l.PushFront(2)
node3 := l.PushFront(3)

l.Remove(node2)
l.InsertAtFront(node2)
Equal(t, l.Front().Value, node2.Value)

l.Remove(node2)
l.InsertAtBack(node2)
Equal(t, l.Back().Value, node2.Value)

l.Remove(node2)
l.InsertBefore(node3, node2)
Equal(t, l.Front().Value, node2.Value)

l.Remove(node2)
l.InsertAfter(node1, node2)
Equal(t, l.Back().Value, node2.Value)
}

func TestSingleEntryPopBack(t *testing.T) {

l := NewDoublyLinked[int]()
Expand Down