mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
change: change update and insert function in link datastruture
This commit is contained in:
@@ -1,13 +1,12 @@
|
|||||||
package datastructure
|
package datastructure
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/duke-git/lancet/v2/datastructure"
|
"github.com/duke-git/lancet/v2/datastructure"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DoublyLink is a linked list. Whose node has a generic Value, Pre pointer points to a previous node of the link, Next pointer points to a next node of the link.
|
// DoublyLink is a linked list. Whose node has a generic Value, Pre pointer points to a previous node of the dl, Next pointer points to a next node of the dl.
|
||||||
type DoublyLink[T any] struct {
|
type DoublyLink[T any] struct {
|
||||||
Head *datastructure.LinkNode[T]
|
Head *datastructure.LinkNode[T]
|
||||||
length int
|
length int
|
||||||
@@ -19,30 +18,30 @@ func NewDoublyLink[T any]() *DoublyLink[T] {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// InsertAtHead insert value into doubly linklist at head index
|
// InsertAtHead insert value into doubly linklist at head index
|
||||||
func (link *DoublyLink[T]) InsertAtHead(value T) {
|
func (dl *DoublyLink[T]) InsertAtHead(value T) {
|
||||||
newNode := datastructure.NewLinkNode(value)
|
newNode := datastructure.NewLinkNode(value)
|
||||||
size := link.Size()
|
size := dl.Size()
|
||||||
|
|
||||||
if size == 0 {
|
if size == 0 {
|
||||||
link.Head = newNode
|
dl.Head = newNode
|
||||||
link.length++
|
dl.length++
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
newNode.Next = link.Head
|
newNode.Next = dl.Head
|
||||||
newNode.Pre = nil
|
newNode.Pre = nil
|
||||||
|
|
||||||
link.Head.Pre = newNode
|
dl.Head.Pre = newNode
|
||||||
link.Head = newNode
|
dl.Head = newNode
|
||||||
|
|
||||||
link.length++
|
dl.length++
|
||||||
}
|
}
|
||||||
|
|
||||||
// InsertAtTail insert value into doubly linklist at tail index
|
// InsertAtTail insert value into doubly linklist at tail index
|
||||||
func (link *DoublyLink[T]) InsertAtTail(value T) {
|
func (dl *DoublyLink[T]) InsertAtTail(value T) {
|
||||||
current := link.Head
|
current := dl.Head
|
||||||
if current == nil {
|
if current == nil {
|
||||||
link.InsertAtHead(value)
|
dl.InsertAtHead(value)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,28 +54,29 @@ func (link *DoublyLink[T]) InsertAtTail(value T) {
|
|||||||
newNode.Pre = current
|
newNode.Pre = current
|
||||||
current.Next = newNode
|
current.Next = newNode
|
||||||
|
|
||||||
link.length++
|
dl.length++
|
||||||
}
|
}
|
||||||
|
|
||||||
// InsertAt insert value into doubly linklist at index
|
// InsertAt insert value into doubly linklist at index
|
||||||
func (link *DoublyLink[T]) InsertAt(index int, value T) error {
|
// param `index` should between [0, length], if index do not meet the conditions, do nothing
|
||||||
size := link.length
|
func (dl *DoublyLink[T]) InsertAt(index int, value T) {
|
||||||
|
size := dl.length
|
||||||
if index < 0 || index > size {
|
if index < 0 || index > size {
|
||||||
return errors.New("param index should between 0 and the length of doubly link.")
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if index == 0 {
|
if index == 0 {
|
||||||
link.InsertAtHead(value)
|
dl.InsertAtHead(value)
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if index == size {
|
if index == size {
|
||||||
link.InsertAtTail(value)
|
dl.InsertAtTail(value)
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
i := 0
|
i := 0
|
||||||
current := link.Head
|
current := dl.Head
|
||||||
|
|
||||||
for current != nil {
|
for current != nil {
|
||||||
if i == index-1 {
|
if i == index-1 {
|
||||||
@@ -85,38 +85,36 @@ func (link *DoublyLink[T]) InsertAt(index int, value T) error {
|
|||||||
newNode.Pre = current
|
newNode.Pre = current
|
||||||
|
|
||||||
current.Next = newNode
|
current.Next = newNode
|
||||||
link.length++
|
dl.length++
|
||||||
|
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
i++
|
i++
|
||||||
current = current.Next
|
current = current.Next
|
||||||
}
|
}
|
||||||
|
|
||||||
return errors.New("doubly link list no exist")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteAtHead delete value in doubly linklist at head index
|
// DeleteAtHead delete value in doubly linklist at head index
|
||||||
func (link *DoublyLink[T]) DeleteAtHead() error {
|
func (dl *DoublyLink[T]) DeleteAtHead() {
|
||||||
if link.Head == nil {
|
if dl.Head == nil {
|
||||||
return errors.New("doubly link list no exist")
|
return
|
||||||
}
|
}
|
||||||
current := link.Head
|
|
||||||
link.Head = current.Next
|
|
||||||
link.Head.Pre = nil
|
|
||||||
link.length--
|
|
||||||
|
|
||||||
return nil
|
current := dl.Head
|
||||||
|
dl.Head = current.Next
|
||||||
|
dl.Head.Pre = nil
|
||||||
|
dl.length--
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteAtTail delete value in doubly linklist at tail index
|
// DeleteAtTail delete value in doubly linklist at tail
|
||||||
func (link *DoublyLink[T]) DeleteAtTail() error {
|
func (dl *DoublyLink[T]) DeleteAtTail() {
|
||||||
if link.Head == nil {
|
if dl.Head == nil {
|
||||||
return errors.New("doubly link list no exist")
|
return
|
||||||
}
|
}
|
||||||
current := link.Head
|
|
||||||
|
current := dl.Head
|
||||||
if current.Next == nil {
|
if current.Next == nil {
|
||||||
return link.DeleteAtHead()
|
dl.DeleteAtHead()
|
||||||
}
|
}
|
||||||
|
|
||||||
for current.Next.Next != nil {
|
for current.Next.Next != nil {
|
||||||
@@ -124,45 +122,44 @@ func (link *DoublyLink[T]) DeleteAtTail() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
current.Next = nil
|
current.Next = nil
|
||||||
link.length--
|
dl.length--
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteAt delete value in doubly linklist at index
|
// DeleteAt delete value in doubly linklist at index
|
||||||
func (link *DoublyLink[T]) DeleteAt(index int) error {
|
// param `index` should be [0, len(DoublyLink)-1]
|
||||||
if link.Head == nil {
|
func (dl *DoublyLink[T]) DeleteAt(index int) {
|
||||||
return errors.New("doubly link list no exist")
|
if dl.Head == nil {
|
||||||
|
return
|
||||||
}
|
}
|
||||||
current := link.Head
|
|
||||||
|
current := dl.Head
|
||||||
if current.Next == nil || index == 0 {
|
if current.Next == nil || index == 0 {
|
||||||
return link.DeleteAtHead()
|
dl.DeleteAtHead()
|
||||||
}
|
}
|
||||||
|
|
||||||
if index == link.length-1 {
|
if index == dl.length-1 {
|
||||||
return link.DeleteAtTail()
|
dl.DeleteAtTail()
|
||||||
}
|
}
|
||||||
|
|
||||||
if index < 0 || index > link.length-1 {
|
if index < 0 || index > dl.length-1 {
|
||||||
return errors.New("param index should between 0 and link size -1.")
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
i := 0
|
i := 0
|
||||||
for current != nil {
|
for current != nil {
|
||||||
if i == index-1 {
|
if i == index-1 {
|
||||||
current.Next = current.Next.Next
|
current.Next = current.Next.Next
|
||||||
link.length--
|
dl.length--
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
i++
|
i++
|
||||||
current = current.Next
|
current = current.Next
|
||||||
}
|
}
|
||||||
|
|
||||||
return errors.New("delete error")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reverse the linked list
|
// Reverse the linked list
|
||||||
func (link *DoublyLink[T]) Reverse() {
|
func (dl *DoublyLink[T]) Reverse() {
|
||||||
current := link.Head
|
current := dl.Head
|
||||||
var temp *datastructure.LinkNode[T]
|
var temp *datastructure.LinkNode[T]
|
||||||
|
|
||||||
for current != nil {
|
for current != nil {
|
||||||
@@ -173,20 +170,20 @@ func (link *DoublyLink[T]) Reverse() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if temp != nil {
|
if temp != nil {
|
||||||
link.Head = temp.Pre
|
dl.Head = temp.Pre
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetMiddleNode return node at middle index of linked list
|
// GetMiddleNode return node at middle index of linked list
|
||||||
func (link *DoublyLink[T]) GetMiddleNode() *datastructure.LinkNode[T] {
|
func (dl *DoublyLink[T]) GetMiddleNode() *datastructure.LinkNode[T] {
|
||||||
if link.Head == nil {
|
if dl.Head == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if link.Head.Next == nil {
|
if dl.Head.Next == nil {
|
||||||
return link.Head
|
return dl.Head
|
||||||
}
|
}
|
||||||
fast := link.Head
|
fast := dl.Head
|
||||||
slow := link.Head
|
slow := dl.Head
|
||||||
|
|
||||||
for fast != nil {
|
for fast != nil {
|
||||||
fast = fast.Next
|
fast = fast.Next
|
||||||
@@ -202,14 +199,14 @@ func (link *DoublyLink[T]) GetMiddleNode() *datastructure.LinkNode[T] {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Size return the count of doubly linked list
|
// Size return the count of doubly linked list
|
||||||
func (link *DoublyLink[T]) Size() int {
|
func (dl *DoublyLink[T]) Size() int {
|
||||||
return link.length
|
return dl.length
|
||||||
}
|
}
|
||||||
|
|
||||||
// Values return slice of all doubly linklist node value
|
// Values return slice of all doubly linklist node value
|
||||||
func (link *DoublyLink[T]) Values() []T {
|
func (dl *DoublyLink[T]) Values() []T {
|
||||||
result := []T{}
|
result := []T{}
|
||||||
current := link.Head
|
current := dl.Head
|
||||||
for current != nil {
|
for current != nil {
|
||||||
result = append(result, current.Value)
|
result = append(result, current.Value)
|
||||||
current = current.Next
|
current = current.Next
|
||||||
@@ -218,8 +215,8 @@ func (link *DoublyLink[T]) Values() []T {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Print all nodes info of a linked list
|
// Print all nodes info of a linked list
|
||||||
func (link *DoublyLink[T]) Print() {
|
func (dl *DoublyLink[T]) Print() {
|
||||||
current := link.Head
|
current := dl.Head
|
||||||
info := "[ "
|
info := "[ "
|
||||||
for current != nil {
|
for current != nil {
|
||||||
info += fmt.Sprintf("%+v, ", current)
|
info += fmt.Sprintf("%+v, ", current)
|
||||||
@@ -229,13 +226,13 @@ func (link *DoublyLink[T]) Print() {
|
|||||||
fmt.Println(info)
|
fmt.Println(info)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsEmpty checks if link is empty or not
|
// IsEmpty checks if dl is empty or not
|
||||||
func (link *DoublyLink[T]) IsEmpty() bool {
|
func (dl *DoublyLink[T]) IsEmpty() bool {
|
||||||
return link.length == 0
|
return dl.length == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clear all nodes in doubly linklist
|
// Clear all nodes in doubly linklist
|
||||||
func (link *DoublyLink[T]) Clear() {
|
func (dl *DoublyLink[T]) Clear() {
|
||||||
link.Head = nil
|
dl.Head = nil
|
||||||
link.length = 0
|
dl.length = 0
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,29 +41,24 @@ func TestDoublyLink_InsertAt(t *testing.T) {
|
|||||||
|
|
||||||
link := NewDoublyLink[int]()
|
link := NewDoublyLink[int]()
|
||||||
|
|
||||||
err := link.InsertAt(1, 1)
|
link.InsertAt(1, 1) //do nothing
|
||||||
assert.IsNotNil(err)
|
|
||||||
|
|
||||||
link.InsertAt(0, 1)
|
link.InsertAt(0, 1)
|
||||||
link.InsertAt(1, 2)
|
link.InsertAt(1, 2)
|
||||||
link.InsertAt(2, 4)
|
link.InsertAt(2, 4)
|
||||||
link.InsertAt(2, 3)
|
link.InsertAt(2, 3)
|
||||||
|
|
||||||
link.Print()
|
|
||||||
|
|
||||||
expected := []int{1, 2, 3, 4}
|
expected := []int{1, 2, 3, 4}
|
||||||
values := link.Values()
|
values := link.Values()
|
||||||
|
|
||||||
assert.Equal(expected, values)
|
assert.Equal(expected, values)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDoublyLink_DeleteAtHead(t *testing.T) {
|
func TestDoublyLink_DeleteAtHead(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestDoublyLink_DeleteAtHead")
|
assert := internal.NewAssert(t, "TestDoublyLink_DeleteAtHead")
|
||||||
|
|
||||||
link := NewDoublyLink[int]()
|
link := NewDoublyLink[int]()
|
||||||
err := link.DeleteAtHead()
|
link.DeleteAtHead()
|
||||||
assert.IsNotNil(err)
|
|
||||||
|
|
||||||
link.InsertAtTail(1)
|
link.InsertAtTail(1)
|
||||||
link.InsertAtTail(2)
|
link.InsertAtTail(2)
|
||||||
@@ -71,7 +66,6 @@ func TestDoublyLink_DeleteAtHead(t *testing.T) {
|
|||||||
link.InsertAtTail(4)
|
link.InsertAtTail(4)
|
||||||
|
|
||||||
link.DeleteAtHead()
|
link.DeleteAtHead()
|
||||||
link.Print()
|
|
||||||
|
|
||||||
expected := []int{2, 3, 4}
|
expected := []int{2, 3, 4}
|
||||||
values := link.Values()
|
values := link.Values()
|
||||||
@@ -83,8 +77,7 @@ func TestDoublyLink_DeleteAtTail(t *testing.T) {
|
|||||||
assert := internal.NewAssert(t, "TestDoublyLink_DeleteAtTail")
|
assert := internal.NewAssert(t, "TestDoublyLink_DeleteAtTail")
|
||||||
|
|
||||||
link := NewDoublyLink[int]()
|
link := NewDoublyLink[int]()
|
||||||
err := link.DeleteAtTail()
|
link.DeleteAtTail()
|
||||||
assert.IsNotNil(err)
|
|
||||||
|
|
||||||
link.InsertAtTail(1)
|
link.InsertAtTail(1)
|
||||||
link.InsertAtTail(2)
|
link.InsertAtTail(2)
|
||||||
@@ -92,7 +85,6 @@ func TestDoublyLink_DeleteAtTail(t *testing.T) {
|
|||||||
link.InsertAtTail(4)
|
link.InsertAtTail(4)
|
||||||
|
|
||||||
link.DeleteAtTail()
|
link.DeleteAtTail()
|
||||||
link.Print()
|
|
||||||
|
|
||||||
expected := []int{1, 2, 3}
|
expected := []int{1, 2, 3}
|
||||||
values := link.Values()
|
values := link.Values()
|
||||||
@@ -104,8 +96,7 @@ func TestDoublyLink_DeleteAt(t *testing.T) {
|
|||||||
assert := internal.NewAssert(t, "TestDoublyLink_DeleteAt")
|
assert := internal.NewAssert(t, "TestDoublyLink_DeleteAt")
|
||||||
|
|
||||||
link := NewDoublyLink[int]()
|
link := NewDoublyLink[int]()
|
||||||
err := link.DeleteAt(0)
|
link.DeleteAt(0)
|
||||||
assert.IsNotNil(err)
|
|
||||||
|
|
||||||
link.InsertAtTail(1)
|
link.InsertAtTail(1)
|
||||||
link.InsertAtTail(2)
|
link.InsertAtTail(2)
|
||||||
@@ -113,11 +104,7 @@ func TestDoublyLink_DeleteAt(t *testing.T) {
|
|||||||
link.InsertAtTail(4)
|
link.InsertAtTail(4)
|
||||||
link.InsertAtTail(5)
|
link.InsertAtTail(5)
|
||||||
|
|
||||||
err = link.DeleteAt(5)
|
link.DeleteAt(0)
|
||||||
assert.IsNotNil(err)
|
|
||||||
|
|
||||||
err = link.DeleteAt(0)
|
|
||||||
assert.IsNil(err)
|
|
||||||
assert.Equal([]int{2, 3, 4, 5}, link.Values())
|
assert.Equal([]int{2, 3, 4, 5}, link.Values())
|
||||||
|
|
||||||
link.DeleteAt(3)
|
link.DeleteAt(3)
|
||||||
|
|||||||
@@ -1,14 +1,13 @@
|
|||||||
package datastructure
|
package datastructure
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
"github.com/duke-git/lancet/v2/datastructure"
|
"github.com/duke-git/lancet/v2/datastructure"
|
||||||
)
|
)
|
||||||
|
|
||||||
// SinglyLink is a linked list. Whose node has a Value generics and Next pointer points to a next node of the link.
|
// SinglyLink is a linked list. Whose node has a Value generics and Next pointer points to a next node of the sl.
|
||||||
type SinglyLink[T any] struct {
|
type SinglyLink[T any] struct {
|
||||||
Head *datastructure.LinkNode[T]
|
Head *datastructure.LinkNode[T]
|
||||||
length int
|
length int
|
||||||
@@ -20,18 +19,18 @@ func NewSinglyLink[T any]() *SinglyLink[T] {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// InsertAtHead insert value into singly linklist at head index
|
// InsertAtHead insert value into singly linklist at head index
|
||||||
func (link *SinglyLink[T]) InsertAtHead(value T) {
|
func (sl *SinglyLink[T]) InsertAtHead(value T) {
|
||||||
newNode := datastructure.NewLinkNode(value)
|
newNode := datastructure.NewLinkNode(value)
|
||||||
newNode.Next = link.Head
|
newNode.Next = sl.Head
|
||||||
link.Head = newNode
|
sl.Head = newNode
|
||||||
link.length++
|
sl.length++
|
||||||
}
|
}
|
||||||
|
|
||||||
// InsertAtTail insert value into singly linklist at tail index
|
// InsertAtTail insert value into singly linklist at tail index
|
||||||
func (link *SinglyLink[T]) InsertAtTail(value T) {
|
func (sl *SinglyLink[T]) InsertAtTail(value T) {
|
||||||
current := link.Head
|
current := sl.Head
|
||||||
if current == nil {
|
if current == nil {
|
||||||
link.InsertAtHead(value)
|
sl.InsertAtHead(value)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,65 +42,65 @@ func (link *SinglyLink[T]) InsertAtTail(value T) {
|
|||||||
newNode.Next = nil
|
newNode.Next = nil
|
||||||
current.Next = newNode
|
current.Next = newNode
|
||||||
|
|
||||||
link.length++
|
sl.length++
|
||||||
}
|
}
|
||||||
|
|
||||||
// InsertAt insert value into singly linklist at index
|
// InsertAt insert value into singly linklist at index
|
||||||
func (link *SinglyLink[T]) InsertAt(index int, value T) error {
|
// param `index` should between [0, len(SinglyLink)], if index do not meet the conditions, do nothing
|
||||||
size := link.length
|
func (sl *SinglyLink[T]) InsertAt(index int, value T) {
|
||||||
|
size := sl.length
|
||||||
if index < 0 || index > size {
|
if index < 0 || index > size {
|
||||||
return errors.New("param index should between 0 and the length of singly link.")
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if index == 0 {
|
if index == 0 {
|
||||||
link.InsertAtHead(value)
|
sl.InsertAtHead(value)
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if index == size {
|
if index == size {
|
||||||
link.InsertAtTail(value)
|
sl.InsertAtTail(value)
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
i := 0
|
i := 0
|
||||||
current := link.Head
|
current := sl.Head
|
||||||
|
|
||||||
for current != nil {
|
for current != nil {
|
||||||
if i == index-1 {
|
if i == index-1 {
|
||||||
newNode := datastructure.NewLinkNode(value)
|
newNode := datastructure.NewLinkNode(value)
|
||||||
newNode.Next = current.Next
|
newNode.Next = current.Next
|
||||||
current.Next = newNode
|
current.Next = newNode
|
||||||
link.length++
|
sl.length++
|
||||||
|
return
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
i++
|
i++
|
||||||
current = current.Next
|
current = current.Next
|
||||||
}
|
}
|
||||||
|
|
||||||
return errors.New("singly link list no exist")
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteAtHead delete value in singly linklist at head index
|
// DeleteAtHead delete value in singly linklist at head index
|
||||||
func (link *SinglyLink[T]) DeleteAtHead() error {
|
func (sl *SinglyLink[T]) DeleteAtHead() {
|
||||||
if link.Head == nil {
|
if sl.Head == nil {
|
||||||
return errors.New("singly link list no exist")
|
return
|
||||||
}
|
}
|
||||||
current := link.Head
|
|
||||||
link.Head = current.Next
|
|
||||||
link.length--
|
|
||||||
|
|
||||||
return nil
|
current := sl.Head
|
||||||
|
sl.Head = current.Next
|
||||||
|
sl.length--
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteAtTail delete value in singly linklist at tail index
|
// DeleteAtTail delete value in singly linklist at tail
|
||||||
func (link *SinglyLink[T]) DeleteAtTail() error {
|
func (sl *SinglyLink[T]) DeleteAtTail() {
|
||||||
if link.Head == nil {
|
if sl.Head == nil {
|
||||||
return errors.New("singly link list no exist")
|
return
|
||||||
}
|
}
|
||||||
current := link.Head
|
|
||||||
|
current := sl.Head
|
||||||
if current.Next == nil {
|
if current.Next == nil {
|
||||||
return link.DeleteAtHead()
|
sl.DeleteAtHead()
|
||||||
}
|
}
|
||||||
|
|
||||||
for current.Next.Next != nil {
|
for current.Next.Next != nil {
|
||||||
@@ -109,68 +108,66 @@ func (link *SinglyLink[T]) DeleteAtTail() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
current.Next = nil
|
current.Next = nil
|
||||||
link.length--
|
sl.length--
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteAt delete value in singly linklist at index
|
// DeleteAt delete value in singly linklist at index
|
||||||
func (link *SinglyLink[T]) DeleteAt(index int) error {
|
// param `index` should be [0, len(SinglyLink)-1]
|
||||||
if link.Head == nil {
|
func (sl *SinglyLink[T]) DeleteAt(index int) {
|
||||||
return errors.New("singly link list no exist")
|
if sl.Head == nil {
|
||||||
|
return
|
||||||
}
|
}
|
||||||
current := link.Head
|
current := sl.Head
|
||||||
if current.Next == nil || index == 0 {
|
if current.Next == nil || index == 0 {
|
||||||
return link.DeleteAtHead()
|
sl.DeleteAtHead()
|
||||||
}
|
}
|
||||||
|
|
||||||
if index == link.length-1 {
|
if index == sl.length-1 {
|
||||||
return link.DeleteAtTail()
|
sl.DeleteAtTail()
|
||||||
}
|
}
|
||||||
|
|
||||||
if index < 0 || index > link.length-1 {
|
if index < 0 || index > sl.length-1 {
|
||||||
return errors.New("param index should between 0 and link size -1.")
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
i := 0
|
i := 0
|
||||||
for current != nil {
|
for current != nil {
|
||||||
if i == index-1 {
|
if i == index-1 {
|
||||||
current.Next = current.Next.Next
|
current.Next = current.Next.Next
|
||||||
link.length--
|
sl.length--
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
i++
|
i++
|
||||||
current = current.Next
|
current = current.Next
|
||||||
}
|
}
|
||||||
|
|
||||||
return errors.New("delete error")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteValue delete value in singly linklist
|
// DeleteValue delete value in singly linklist
|
||||||
func (link *SinglyLink[T]) DeleteValue(value T) {
|
func (sl *SinglyLink[T]) DeleteValue(value T) {
|
||||||
if link.Head == nil {
|
if sl.Head == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
dummyHead := datastructure.NewLinkNode(value)
|
dummyHead := datastructure.NewLinkNode(value)
|
||||||
dummyHead.Next = link.Head
|
dummyHead.Next = sl.Head
|
||||||
current := dummyHead
|
current := dummyHead
|
||||||
|
|
||||||
for current.Next != nil {
|
for current.Next != nil {
|
||||||
if reflect.DeepEqual(current.Next.Value, value) {
|
if reflect.DeepEqual(current.Next.Value, value) {
|
||||||
current.Next = current.Next.Next
|
current.Next = current.Next.Next
|
||||||
link.length--
|
sl.length--
|
||||||
} else {
|
} else {
|
||||||
current = current.Next
|
current = current.Next
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
link.Head = dummyHead.Next
|
sl.Head = dummyHead.Next
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reverse the linked list
|
// Reverse the linked list
|
||||||
func (link *SinglyLink[T]) Reverse() {
|
func (sl *SinglyLink[T]) Reverse() {
|
||||||
var pre, next *datastructure.LinkNode[T]
|
var pre, next *datastructure.LinkNode[T]
|
||||||
|
|
||||||
current := link.Head
|
current := sl.Head
|
||||||
|
|
||||||
for current != nil {
|
for current != nil {
|
||||||
next = current.Next
|
next = current.Next
|
||||||
@@ -179,19 +176,19 @@ func (link *SinglyLink[T]) Reverse() {
|
|||||||
current = next
|
current = next
|
||||||
}
|
}
|
||||||
|
|
||||||
link.Head = pre
|
sl.Head = pre
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetMiddleNode return node at middle index of linked list
|
// GetMiddleNode return node at middle index of linked list
|
||||||
func (link *SinglyLink[T]) GetMiddleNode() *datastructure.LinkNode[T] {
|
func (sl *SinglyLink[T]) GetMiddleNode() *datastructure.LinkNode[T] {
|
||||||
if link.Head == nil {
|
if sl.Head == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if link.Head.Next == nil {
|
if sl.Head.Next == nil {
|
||||||
return link.Head
|
return sl.Head
|
||||||
}
|
}
|
||||||
fast := link.Head
|
fast := sl.Head
|
||||||
slow := link.Head
|
slow := sl.Head
|
||||||
|
|
||||||
for fast != nil {
|
for fast != nil {
|
||||||
fast = fast.Next
|
fast = fast.Next
|
||||||
@@ -207,14 +204,14 @@ func (link *SinglyLink[T]) GetMiddleNode() *datastructure.LinkNode[T] {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Size return the count of singly linked list
|
// Size return the count of singly linked list
|
||||||
func (link *SinglyLink[T]) Size() int {
|
func (sl *SinglyLink[T]) Size() int {
|
||||||
return link.length
|
return sl.length
|
||||||
}
|
}
|
||||||
|
|
||||||
// Values return slice of all singly linklist node value
|
// Values return slice of all singly linklist node value
|
||||||
func (link *SinglyLink[T]) Values() []T {
|
func (sl *SinglyLink[T]) Values() []T {
|
||||||
result := []T{}
|
result := []T{}
|
||||||
current := link.Head
|
current := sl.Head
|
||||||
for current != nil {
|
for current != nil {
|
||||||
result = append(result, current.Value)
|
result = append(result, current.Value)
|
||||||
current = current.Next
|
current = current.Next
|
||||||
@@ -222,20 +219,20 @@ func (link *SinglyLink[T]) Values() []T {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsEmpty checks if link is empty or not
|
// IsEmpty checks if sl is empty or not
|
||||||
func (link *SinglyLink[T]) IsEmpty() bool {
|
func (sl *SinglyLink[T]) IsEmpty() bool {
|
||||||
return link.length == 0
|
return sl.length == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clear all the node in singly linklist
|
// Clear all the node in singly linklist
|
||||||
func (link *SinglyLink[T]) Clear() {
|
func (sl *SinglyLink[T]) Clear() {
|
||||||
link.Head = nil
|
sl.Head = nil
|
||||||
link.length = 0
|
sl.length = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print all nodes info of a linked list
|
// Print all nodes info of a linked list
|
||||||
func (link *SinglyLink[T]) Print() {
|
func (sl *SinglyLink[T]) Print() {
|
||||||
current := link.Head
|
current := sl.Head
|
||||||
info := "[ "
|
info := "[ "
|
||||||
for current != nil {
|
for current != nil {
|
||||||
info += fmt.Sprintf("%+v, ", current)
|
info += fmt.Sprintf("%+v, ", current)
|
||||||
|
|||||||
@@ -41,25 +41,12 @@ func TestSinglyLink_InsertAt(t *testing.T) {
|
|||||||
|
|
||||||
link := NewSinglyLink[int]()
|
link := NewSinglyLink[int]()
|
||||||
|
|
||||||
err := link.InsertAt(1, 1)
|
link.InsertAt(1, 1) //do nothing
|
||||||
assert.IsNotNil(err)
|
|
||||||
|
|
||||||
err = link.InsertAt(0, 1)
|
link.InsertAt(0, 1)
|
||||||
if err != nil {
|
link.InsertAt(1, 2)
|
||||||
t.FailNow()
|
link.InsertAt(2, 4)
|
||||||
}
|
link.InsertAt(2, 3)
|
||||||
err = link.InsertAt(1, 2)
|
|
||||||
if err != nil {
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
err = link.InsertAt(2, 4)
|
|
||||||
if err != nil {
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
err = link.InsertAt(2, 3)
|
|
||||||
if err != nil {
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
|
|
||||||
link.Print()
|
link.Print()
|
||||||
|
|
||||||
@@ -73,8 +60,8 @@ func TestSinglyLink_DeleteAtHead(t *testing.T) {
|
|||||||
assert := internal.NewAssert(t, "TestSinglyLink_DeleteAtHead")
|
assert := internal.NewAssert(t, "TestSinglyLink_DeleteAtHead")
|
||||||
|
|
||||||
link := NewSinglyLink[int]()
|
link := NewSinglyLink[int]()
|
||||||
err := link.DeleteAtHead()
|
|
||||||
assert.IsNotNil(err)
|
link.DeleteAtHead()
|
||||||
|
|
||||||
link.InsertAtTail(1)
|
link.InsertAtTail(1)
|
||||||
link.InsertAtTail(2)
|
link.InsertAtTail(2)
|
||||||
@@ -94,8 +81,6 @@ func TestSinglyLink_DeleteAtTail(t *testing.T) {
|
|||||||
assert := internal.NewAssert(t, "TestSinglyLink_DeleteAtTail")
|
assert := internal.NewAssert(t, "TestSinglyLink_DeleteAtTail")
|
||||||
|
|
||||||
link := NewSinglyLink[int]()
|
link := NewSinglyLink[int]()
|
||||||
err := link.DeleteAtTail()
|
|
||||||
assert.IsNotNil(err)
|
|
||||||
|
|
||||||
link.InsertAtTail(1)
|
link.InsertAtTail(1)
|
||||||
link.InsertAtTail(2)
|
link.InsertAtTail(2)
|
||||||
@@ -103,7 +88,6 @@ func TestSinglyLink_DeleteAtTail(t *testing.T) {
|
|||||||
link.InsertAtTail(4)
|
link.InsertAtTail(4)
|
||||||
|
|
||||||
link.DeleteAtTail()
|
link.DeleteAtTail()
|
||||||
link.Print()
|
|
||||||
|
|
||||||
expected := []int{1, 2, 3}
|
expected := []int{1, 2, 3}
|
||||||
values := link.Values()
|
values := link.Values()
|
||||||
@@ -133,8 +117,6 @@ func TestSinglyLink_DeleteAt(t *testing.T) {
|
|||||||
assert := internal.NewAssert(t, "TestSinglyLink_DeleteAt")
|
assert := internal.NewAssert(t, "TestSinglyLink_DeleteAt")
|
||||||
|
|
||||||
link := NewSinglyLink[int]()
|
link := NewSinglyLink[int]()
|
||||||
err := link.DeleteAt(0)
|
|
||||||
assert.IsNotNil(err)
|
|
||||||
|
|
||||||
link.InsertAtTail(1)
|
link.InsertAtTail(1)
|
||||||
link.InsertAtTail(2)
|
link.InsertAtTail(2)
|
||||||
@@ -142,11 +124,7 @@ func TestSinglyLink_DeleteAt(t *testing.T) {
|
|||||||
link.InsertAtTail(4)
|
link.InsertAtTail(4)
|
||||||
link.InsertAtTail(5)
|
link.InsertAtTail(5)
|
||||||
|
|
||||||
err = link.DeleteAt(5)
|
link.DeleteAt(0)
|
||||||
assert.IsNotNil(err)
|
|
||||||
|
|
||||||
err = link.DeleteAt(0)
|
|
||||||
assert.IsNil(err)
|
|
||||||
assert.Equal([]int{2, 3, 4, 5}, link.Values())
|
assert.Equal([]int{2, 3, 4, 5}, link.Values())
|
||||||
|
|
||||||
link.DeleteAt(3)
|
link.DeleteAt(3)
|
||||||
|
|||||||
@@ -132,12 +132,12 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
### <span id="SinglyLink_InsertAt">InsertAt</span>
|
### <span id="SinglyLink_InsertAt">InsertAt</span>
|
||||||
<p>Insert value into singly linklist at index, index shoud be great or equal 0 and less or equal number of link nodes</p>
|
<p>Insert value into singly linklist at index, param `index` should between [0, len(SinglyLink)], if index do not meet the conditions, do nothing</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func (link *SinglyLink[T]) InsertAt(index int, value T) error
|
func (link *SinglyLink[T]) InsertAt(index int, value T)
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
@@ -152,6 +152,8 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
lk := link.NewSinglyLink[int]()
|
lk := link.NewSinglyLink[int]()
|
||||||
|
|
||||||
|
lk.InsertAt(1, 1) //do nothing
|
||||||
|
|
||||||
lk.InsertAt(0, 1)
|
lk.InsertAt(0, 1)
|
||||||
lk.InsertAt(1, 2)
|
lk.InsertAt(1, 2)
|
||||||
lk.InsertAt(2, 3)
|
lk.InsertAt(2, 3)
|
||||||
@@ -228,12 +230,12 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
### <span id="SinglyLink_DeleteAt">DeleteAt</span>
|
### <span id="SinglyLink_DeleteAt">DeleteAt</span>
|
||||||
<p>Delete value at specific index, index shoud be great or equal 0 and less or less than number of link nodes - 1</p>
|
<p>Delete value at specific index, param `index` should be [0, len(SinglyLink)-1]</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func (link *SinglyLink[T]) DeleteAt(index int) error
|
func (link *SinglyLink[T]) DeleteAt(index int)
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
@@ -253,9 +255,8 @@ func main() {
|
|||||||
lk.InsertAtTail(3)
|
lk.InsertAtTail(3)
|
||||||
lk.InsertAtTail(4)
|
lk.InsertAtTail(4)
|
||||||
|
|
||||||
err := lk.DeleteAt(3)
|
lk.DeleteAt(3)
|
||||||
|
|
||||||
fmt.Println(err) //nil
|
|
||||||
fmt.Println(lk.Values()) //[]int{1, 2, 3}
|
fmt.Println(lk.Values()) //[]int{1, 2, 3}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -268,7 +269,7 @@ func main() {
|
|||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func (link *SinglyLink[T]) DeleteAtHead() error
|
func (link *SinglyLink[T]) DeleteAtHead()
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
@@ -288,9 +289,8 @@ func main() {
|
|||||||
lk.InsertAtTail(3)
|
lk.InsertAtTail(3)
|
||||||
lk.InsertAtTail(4)
|
lk.InsertAtTail(4)
|
||||||
|
|
||||||
err := lk.DeleteAtHead()
|
lk.DeleteAtHead()
|
||||||
|
|
||||||
fmt.Println(err) //nil
|
|
||||||
fmt.Println(lk.Values()) //[]int{2, 3, 4}
|
fmt.Println(lk.Values()) //[]int{2, 3, 4}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -304,7 +304,7 @@ func main() {
|
|||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func (link *SinglyLink[T]) DeleteAtTail() error
|
func (link *SinglyLink[T]) DeleteAtTail()
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
@@ -323,9 +323,8 @@ func main() {
|
|||||||
lk.InsertAtTail(2)
|
lk.InsertAtTail(2)
|
||||||
lk.InsertAtTail(3)
|
lk.InsertAtTail(3)
|
||||||
|
|
||||||
err := lk.DeleteAtTail()
|
lk.DeleteAtTail()
|
||||||
|
|
||||||
fmt.Println(err) //nil
|
|
||||||
fmt.Println(lk.Values()) //[]int{1, 2}
|
fmt.Println(lk.Values()) //[]int{1, 2}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -628,12 +627,12 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
### <span id="DoublyLink_InsertAt">InsertAt</span>
|
### <span id="DoublyLink_InsertAt">InsertAt</span>
|
||||||
<p>Insert value into doubly linklist at index, index shoud be great or equal 0 and less or equal number of link nodes</p>
|
<p>Insert value into doubly linklist at index, param `index` should between [0, len(DoublyLink)], if index do not meet the conditions, do nothing</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func (link *DoublyLink[T]) InsertAt(index int, value T) error
|
func (link *DoublyLink[T]) InsertAt(index int, value T)
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
@@ -648,6 +647,8 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
lk := link.NewDoublyLink[int]()
|
lk := link.NewDoublyLink[int]()
|
||||||
|
|
||||||
|
lk.InsertAt(1, 1) //do nothing
|
||||||
|
|
||||||
lk.InsertAt(0, 1)
|
lk.InsertAt(0, 1)
|
||||||
lk.InsertAt(1, 2)
|
lk.InsertAt(1, 2)
|
||||||
lk.InsertAt(2, 3)
|
lk.InsertAt(2, 3)
|
||||||
@@ -724,12 +725,12 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
### <span id="DoublyLink_DeleteAt">DeleteAt</span>
|
### <span id="DoublyLink_DeleteAt">DeleteAt</span>
|
||||||
<p>Delete value at specific index, index shoud be great or equal 0 and less or less than number of link nodes - 1</p>
|
<p>Delete value at specific index, param `index` should be [0, len(DoublyLink)-1]</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func (link *DoublyLink[T]) DeleteAt(index int) error
|
func (link *DoublyLink[T]) DeleteAt(index int)
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
@@ -749,9 +750,8 @@ func main() {
|
|||||||
lk.InsertAtTail(3)
|
lk.InsertAtTail(3)
|
||||||
lk.InsertAtTail(4)
|
lk.InsertAtTail(4)
|
||||||
|
|
||||||
err := lk.DeleteAt(3)
|
lk.DeleteAt(3)
|
||||||
|
|
||||||
fmt.Println(err) //nil
|
|
||||||
fmt.Println(lk.Values()) //[]int{1, 2, 3}
|
fmt.Println(lk.Values()) //[]int{1, 2, 3}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -764,7 +764,7 @@ func main() {
|
|||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func (link *DoublyLink[T]) DeleteAtHead() error
|
func (link *DoublyLink[T]) DeleteAtHead()
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
@@ -784,9 +784,8 @@ func main() {
|
|||||||
lk.InsertAtTail(3)
|
lk.InsertAtTail(3)
|
||||||
lk.InsertAtTail(4)
|
lk.InsertAtTail(4)
|
||||||
|
|
||||||
err := lk.DeleteAtHead()
|
lk.DeleteAtHead()
|
||||||
|
|
||||||
fmt.Println(err) //nil
|
|
||||||
fmt.Println(lk.Values()) //[]int{2, 3, 4}
|
fmt.Println(lk.Values()) //[]int{2, 3, 4}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -132,12 +132,12 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
### <span id="SinglyLink_InsertAt">InsertAt</span>
|
### <span id="SinglyLink_InsertAt">InsertAt</span>
|
||||||
<p>将值插入到索引处的链表中,索引应大于或等于 0 且小于或等于链表节点数</p>
|
<p>将值插入到索引处的链表中,索引应大于或等于0且小于或等于链表节点数</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func (link *SinglyLink[T]) InsertAt(index int, value T) error
|
func (link *SinglyLink[T]) InsertAt(index int, value T)
|
||||||
```
|
```
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
@@ -152,6 +152,8 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
lk := link.NewSinglyLink[int]()
|
lk := link.NewSinglyLink[int]()
|
||||||
|
|
||||||
|
lk.InsertAt(1, 1) //do nothing
|
||||||
|
|
||||||
lk.InsertAt(0, 1)
|
lk.InsertAt(0, 1)
|
||||||
lk.InsertAt(1, 2)
|
lk.InsertAt(1, 2)
|
||||||
lk.InsertAt(2, 3)
|
lk.InsertAt(2, 3)
|
||||||
@@ -228,12 +230,12 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
### <span id="SinglyLink_DeleteAt">DeleteAt</span>
|
### <span id="SinglyLink_DeleteAt">DeleteAt</span>
|
||||||
<p>删除特定索引处的值,索引应大于或等于0且小于或等于链接节点数 - 1</p>
|
<p>删除特定索引处的值,索引应大于或等于0且小于或等于链接节点数-1</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func (link *SinglyLink[T]) DeleteAt(index int) error
|
func (link *SinglyLink[T]) DeleteAt(index int)
|
||||||
```
|
```
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
@@ -253,9 +255,8 @@ func main() {
|
|||||||
lk.InsertAtTail(3)
|
lk.InsertAtTail(3)
|
||||||
lk.InsertAtTail(4)
|
lk.InsertAtTail(4)
|
||||||
|
|
||||||
err := lk.DeleteAt(3)
|
lk.DeleteAt(3)
|
||||||
|
|
||||||
fmt.Println(err) //nil
|
|
||||||
fmt.Println(lk.Values()) //[]int{1, 2, 3}
|
fmt.Println(lk.Values()) //[]int{1, 2, 3}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -268,7 +269,7 @@ func main() {
|
|||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func (link *SinglyLink[T]) DeleteAtHead() error
|
func (link *SinglyLink[T]) DeleteAtHead()
|
||||||
```
|
```
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
@@ -288,9 +289,8 @@ func main() {
|
|||||||
lk.InsertAtTail(3)
|
lk.InsertAtTail(3)
|
||||||
lk.InsertAtTail(4)
|
lk.InsertAtTail(4)
|
||||||
|
|
||||||
err := lk.DeleteAtHead()
|
lk.DeleteAtHead()
|
||||||
|
|
||||||
fmt.Println(err) //nil
|
|
||||||
fmt.Println(lk.Values()) //[]int{2, 3, 4}
|
fmt.Println(lk.Values()) //[]int{2, 3, 4}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -304,7 +304,7 @@ func main() {
|
|||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func (link *SinglyLink[T]) DeleteAtTail() error
|
func (link *SinglyLink[T]) DeleteAtTail()
|
||||||
```
|
```
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
@@ -323,9 +323,8 @@ func main() {
|
|||||||
lk.InsertAtTail(2)
|
lk.InsertAtTail(2)
|
||||||
lk.InsertAtTail(3)
|
lk.InsertAtTail(3)
|
||||||
|
|
||||||
err := lk.DeleteAtTail()
|
lk.DeleteAtTail()
|
||||||
|
|
||||||
fmt.Println(err) //nil
|
|
||||||
fmt.Println(lk.Values()) //[]int{1, 2}
|
fmt.Println(lk.Values()) //[]int{1, 2}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -628,12 +627,12 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
### <span id="DoublyLink_InsertAt">InsertAt</span>
|
### <span id="DoublyLink_InsertAt">InsertAt</span>
|
||||||
<p>将值插入到索引处的链表中,索引应大于或等于 0 且小于或等于链表节点数</p>
|
<p>将值插入到索引处的链表中,索引应大于或等于0且小于或等于链表节点数</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func (link *DoublyLink[T]) InsertAt(index int, value T) error
|
func (link *DoublyLink[T]) InsertAt(index int, value T)
|
||||||
```
|
```
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
@@ -648,6 +647,8 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
lk := link.NewDoublyLink[int]()
|
lk := link.NewDoublyLink[int]()
|
||||||
|
|
||||||
|
lk.InsertAt(1, 1) //do nothing
|
||||||
|
|
||||||
lk.InsertAt(0, 1)
|
lk.InsertAt(0, 1)
|
||||||
lk.InsertAt(1, 2)
|
lk.InsertAt(1, 2)
|
||||||
lk.InsertAt(2, 3)
|
lk.InsertAt(2, 3)
|
||||||
@@ -724,12 +725,12 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
### <span id="DoublyLink_DeleteAt">DeleteAt</span>
|
### <span id="DoublyLink_DeleteAt">DeleteAt</span>
|
||||||
<p>删除特定索引处的值,索引应大于或等于0且小于或等于链接节点数 - 1</p>
|
<p>删除特定索引处的值,索引应大于或等于0且小于或等于链接节点数-1</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func (link *DoublyLink[T]) DeleteAt(index int) error
|
func (link *DoublyLink[T]) DeleteAt(index int)
|
||||||
```
|
```
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
@@ -749,9 +750,8 @@ func main() {
|
|||||||
lk.InsertAtTail(3)
|
lk.InsertAtTail(3)
|
||||||
lk.InsertAtTail(4)
|
lk.InsertAtTail(4)
|
||||||
|
|
||||||
err := lk.DeleteAt(3)
|
lk.DeleteAt(3)
|
||||||
|
|
||||||
fmt.Println(err) //nil
|
|
||||||
fmt.Println(lk.Values()) //[]int{1, 2, 3}
|
fmt.Println(lk.Values()) //[]int{1, 2, 3}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -764,7 +764,7 @@ func main() {
|
|||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func (link *DoublyLink[T]) DeleteAtHead() error
|
func (link *DoublyLink[T]) DeleteAtHead()
|
||||||
```
|
```
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
@@ -784,9 +784,8 @@ func main() {
|
|||||||
lk.InsertAtTail(3)
|
lk.InsertAtTail(3)
|
||||||
lk.InsertAtTail(4)
|
lk.InsertAtTail(4)
|
||||||
|
|
||||||
err := lk.DeleteAtHead()
|
lk.DeleteAtHead()
|
||||||
|
|
||||||
fmt.Println(err) //nil
|
|
||||||
fmt.Println(lk.Values()) //[]int{2, 3, 4}
|
fmt.Println(lk.Values()) //[]int{2, 3, 4}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -800,7 +799,7 @@ func main() {
|
|||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func (link *DoublyLink[T]) DeleteAtTail() error
|
func (link *DoublyLink[T]) DeleteAtTail()
|
||||||
```
|
```
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
@@ -819,9 +818,8 @@ func main() {
|
|||||||
lk.InsertAtTail(2)
|
lk.InsertAtTail(2)
|
||||||
lk.InsertAtTail(3)
|
lk.InsertAtTail(3)
|
||||||
|
|
||||||
err := lk.DeleteAtTail()
|
lk.DeleteAtTail()
|
||||||
|
|
||||||
fmt.Println(err) //nil
|
|
||||||
fmt.Println(lk.Values()) //[]int{1, 2}
|
fmt.Println(lk.Values()) //[]int{1, 2}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user