1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 21:02:27 +08:00

change: change update and insert function in link datastruture

This commit is contained in:
dudaodong
2022-12-10 17:45:29 +08:00
parent 7d8d9c3543
commit 4e457ad672
6 changed files with 201 additions and 245 deletions

View File

@@ -1,13 +1,12 @@
package datastructure
import (
"errors"
"fmt"
"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 {
Head *datastructure.LinkNode[T]
length int
@@ -19,30 +18,30 @@ func NewDoublyLink[T any]() *DoublyLink[T] {
}
// 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)
size := link.Size()
size := dl.Size()
if size == 0 {
link.Head = newNode
link.length++
dl.Head = newNode
dl.length++
return
}
newNode.Next = link.Head
newNode.Next = dl.Head
newNode.Pre = nil
link.Head.Pre = newNode
link.Head = newNode
dl.Head.Pre = newNode
dl.Head = newNode
link.length++
dl.length++
}
// InsertAtTail insert value into doubly linklist at tail index
func (link *DoublyLink[T]) InsertAtTail(value T) {
current := link.Head
func (dl *DoublyLink[T]) InsertAtTail(value T) {
current := dl.Head
if current == nil {
link.InsertAtHead(value)
dl.InsertAtHead(value)
return
}
@@ -55,28 +54,29 @@ func (link *DoublyLink[T]) InsertAtTail(value T) {
newNode.Pre = current
current.Next = newNode
link.length++
dl.length++
}
// InsertAt insert value into doubly linklist at index
func (link *DoublyLink[T]) InsertAt(index int, value T) error {
size := link.length
// param `index` should between [0, length], if index do not meet the conditions, do nothing
func (dl *DoublyLink[T]) InsertAt(index int, value T) {
size := dl.length
if index < 0 || index > size {
return errors.New("param index should between 0 and the length of doubly link.")
return
}
if index == 0 {
link.InsertAtHead(value)
return nil
dl.InsertAtHead(value)
return
}
if index == size {
link.InsertAtTail(value)
return nil
dl.InsertAtTail(value)
return
}
i := 0
current := link.Head
current := dl.Head
for current != nil {
if i == index-1 {
@@ -85,38 +85,36 @@ func (link *DoublyLink[T]) InsertAt(index int, value T) error {
newNode.Pre = current
current.Next = newNode
link.length++
dl.length++
return nil
return
}
i++
current = current.Next
}
return errors.New("doubly link list no exist")
}
// DeleteAtHead delete value in doubly linklist at head index
func (link *DoublyLink[T]) DeleteAtHead() error {
if link.Head == nil {
return errors.New("doubly link list no exist")
func (dl *DoublyLink[T]) DeleteAtHead() {
if dl.Head == nil {
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
func (link *DoublyLink[T]) DeleteAtTail() error {
if link.Head == nil {
return errors.New("doubly link list no exist")
// DeleteAtTail delete value in doubly linklist at tail
func (dl *DoublyLink[T]) DeleteAtTail() {
if dl.Head == nil {
return
}
current := link.Head
current := dl.Head
if current.Next == nil {
return link.DeleteAtHead()
dl.DeleteAtHead()
}
for current.Next.Next != nil {
@@ -124,45 +122,44 @@ func (link *DoublyLink[T]) DeleteAtTail() error {
}
current.Next = nil
link.length--
return nil
dl.length--
}
// DeleteAt delete value in doubly linklist at index
func (link *DoublyLink[T]) DeleteAt(index int) error {
if link.Head == nil {
return errors.New("doubly link list no exist")
// param `index` should be [0, len(DoublyLink)-1]
func (dl *DoublyLink[T]) DeleteAt(index int) {
if dl.Head == nil {
return
}
current := link.Head
current := dl.Head
if current.Next == nil || index == 0 {
return link.DeleteAtHead()
dl.DeleteAtHead()
}
if index == link.length-1 {
return link.DeleteAtTail()
if index == dl.length-1 {
dl.DeleteAtTail()
}
if index < 0 || index > link.length-1 {
return errors.New("param index should between 0 and link size -1.")
if index < 0 || index > dl.length-1 {
return
}
i := 0
for current != nil {
if i == index-1 {
current.Next = current.Next.Next
link.length--
return nil
dl.length--
return
}
i++
current = current.Next
}
return errors.New("delete error")
}
// Reverse the linked list
func (link *DoublyLink[T]) Reverse() {
current := link.Head
func (dl *DoublyLink[T]) Reverse() {
current := dl.Head
var temp *datastructure.LinkNode[T]
for current != nil {
@@ -173,20 +170,20 @@ func (link *DoublyLink[T]) Reverse() {
}
if temp != nil {
link.Head = temp.Pre
dl.Head = temp.Pre
}
}
// GetMiddleNode return node at middle index of linked list
func (link *DoublyLink[T]) GetMiddleNode() *datastructure.LinkNode[T] {
if link.Head == nil {
func (dl *DoublyLink[T]) GetMiddleNode() *datastructure.LinkNode[T] {
if dl.Head == nil {
return nil
}
if link.Head.Next == nil {
return link.Head
if dl.Head.Next == nil {
return dl.Head
}
fast := link.Head
slow := link.Head
fast := dl.Head
slow := dl.Head
for fast != nil {
fast = fast.Next
@@ -202,14 +199,14 @@ func (link *DoublyLink[T]) GetMiddleNode() *datastructure.LinkNode[T] {
}
// Size return the count of doubly linked list
func (link *DoublyLink[T]) Size() int {
return link.length
func (dl *DoublyLink[T]) Size() int {
return dl.length
}
// Values return slice of all doubly linklist node value
func (link *DoublyLink[T]) Values() []T {
func (dl *DoublyLink[T]) Values() []T {
result := []T{}
current := link.Head
current := dl.Head
for current != nil {
result = append(result, current.Value)
current = current.Next
@@ -218,8 +215,8 @@ func (link *DoublyLink[T]) Values() []T {
}
// Print all nodes info of a linked list
func (link *DoublyLink[T]) Print() {
current := link.Head
func (dl *DoublyLink[T]) Print() {
current := dl.Head
info := "[ "
for current != nil {
info += fmt.Sprintf("%+v, ", current)
@@ -229,13 +226,13 @@ func (link *DoublyLink[T]) Print() {
fmt.Println(info)
}
// IsEmpty checks if link is empty or not
func (link *DoublyLink[T]) IsEmpty() bool {
return link.length == 0
// IsEmpty checks if dl is empty or not
func (dl *DoublyLink[T]) IsEmpty() bool {
return dl.length == 0
}
// Clear all nodes in doubly linklist
func (link *DoublyLink[T]) Clear() {
link.Head = nil
link.length = 0
func (dl *DoublyLink[T]) Clear() {
dl.Head = nil
dl.length = 0
}

View File

@@ -41,29 +41,24 @@ func TestDoublyLink_InsertAt(t *testing.T) {
link := NewDoublyLink[int]()
err := link.InsertAt(1, 1)
assert.IsNotNil(err)
link.InsertAt(1, 1) //do nothing
link.InsertAt(0, 1)
link.InsertAt(1, 2)
link.InsertAt(2, 4)
link.InsertAt(2, 3)
link.Print()
expected := []int{1, 2, 3, 4}
values := link.Values()
assert.Equal(expected, values)
}
func TestDoublyLink_DeleteAtHead(t *testing.T) {
assert := internal.NewAssert(t, "TestDoublyLink_DeleteAtHead")
link := NewDoublyLink[int]()
err := link.DeleteAtHead()
assert.IsNotNil(err)
link.DeleteAtHead()
link.InsertAtTail(1)
link.InsertAtTail(2)
@@ -71,7 +66,6 @@ func TestDoublyLink_DeleteAtHead(t *testing.T) {
link.InsertAtTail(4)
link.DeleteAtHead()
link.Print()
expected := []int{2, 3, 4}
values := link.Values()
@@ -83,8 +77,7 @@ func TestDoublyLink_DeleteAtTail(t *testing.T) {
assert := internal.NewAssert(t, "TestDoublyLink_DeleteAtTail")
link := NewDoublyLink[int]()
err := link.DeleteAtTail()
assert.IsNotNil(err)
link.DeleteAtTail()
link.InsertAtTail(1)
link.InsertAtTail(2)
@@ -92,7 +85,6 @@ func TestDoublyLink_DeleteAtTail(t *testing.T) {
link.InsertAtTail(4)
link.DeleteAtTail()
link.Print()
expected := []int{1, 2, 3}
values := link.Values()
@@ -104,8 +96,7 @@ func TestDoublyLink_DeleteAt(t *testing.T) {
assert := internal.NewAssert(t, "TestDoublyLink_DeleteAt")
link := NewDoublyLink[int]()
err := link.DeleteAt(0)
assert.IsNotNil(err)
link.DeleteAt(0)
link.InsertAtTail(1)
link.InsertAtTail(2)
@@ -113,11 +104,7 @@ func TestDoublyLink_DeleteAt(t *testing.T) {
link.InsertAtTail(4)
link.InsertAtTail(5)
err = link.DeleteAt(5)
assert.IsNotNil(err)
err = link.DeleteAt(0)
assert.IsNil(err)
link.DeleteAt(0)
assert.Equal([]int{2, 3, 4, 5}, link.Values())
link.DeleteAt(3)

View File

@@ -1,14 +1,13 @@
package datastructure
import (
"errors"
"fmt"
"reflect"
"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 {
Head *datastructure.LinkNode[T]
length int
@@ -20,18 +19,18 @@ func NewSinglyLink[T any]() *SinglyLink[T] {
}
// 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.Next = link.Head
link.Head = newNode
link.length++
newNode.Next = sl.Head
sl.Head = newNode
sl.length++
}
// InsertAtTail insert value into singly linklist at tail index
func (link *SinglyLink[T]) InsertAtTail(value T) {
current := link.Head
func (sl *SinglyLink[T]) InsertAtTail(value T) {
current := sl.Head
if current == nil {
link.InsertAtHead(value)
sl.InsertAtHead(value)
return
}
@@ -43,65 +42,65 @@ func (link *SinglyLink[T]) InsertAtTail(value T) {
newNode.Next = nil
current.Next = newNode
link.length++
sl.length++
}
// InsertAt insert value into singly linklist at index
func (link *SinglyLink[T]) InsertAt(index int, value T) error {
size := link.length
// param `index` should between [0, len(SinglyLink)], if index do not meet the conditions, do nothing
func (sl *SinglyLink[T]) InsertAt(index int, value T) {
size := sl.length
if index < 0 || index > size {
return errors.New("param index should between 0 and the length of singly link.")
return
}
if index == 0 {
link.InsertAtHead(value)
return nil
sl.InsertAtHead(value)
return
}
if index == size {
link.InsertAtTail(value)
return nil
sl.InsertAtTail(value)
return
}
i := 0
current := link.Head
current := sl.Head
for current != nil {
if i == index-1 {
newNode := datastructure.NewLinkNode(value)
newNode.Next = current.Next
current.Next = newNode
link.length++
return nil
sl.length++
return
}
i++
current = current.Next
}
return errors.New("singly link list no exist")
return
}
// DeleteAtHead delete value in singly linklist at head index
func (link *SinglyLink[T]) DeleteAtHead() error {
if link.Head == nil {
return errors.New("singly link list no exist")
func (sl *SinglyLink[T]) DeleteAtHead() {
if sl.Head == nil {
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
func (link *SinglyLink[T]) DeleteAtTail() error {
if link.Head == nil {
return errors.New("singly link list no exist")
// DeleteAtTail delete value in singly linklist at tail
func (sl *SinglyLink[T]) DeleteAtTail() {
if sl.Head == nil {
return
}
current := link.Head
current := sl.Head
if current.Next == nil {
return link.DeleteAtHead()
sl.DeleteAtHead()
}
for current.Next.Next != nil {
@@ -109,68 +108,66 @@ func (link *SinglyLink[T]) DeleteAtTail() error {
}
current.Next = nil
link.length--
return nil
sl.length--
}
// DeleteAt delete value in singly linklist at index
func (link *SinglyLink[T]) DeleteAt(index int) error {
if link.Head == nil {
return errors.New("singly link list no exist")
// param `index` should be [0, len(SinglyLink)-1]
func (sl *SinglyLink[T]) DeleteAt(index int) {
if sl.Head == nil {
return
}
current := link.Head
current := sl.Head
if current.Next == nil || index == 0 {
return link.DeleteAtHead()
sl.DeleteAtHead()
}
if index == link.length-1 {
return link.DeleteAtTail()
if index == sl.length-1 {
sl.DeleteAtTail()
}
if index < 0 || index > link.length-1 {
return errors.New("param index should between 0 and link size -1.")
if index < 0 || index > sl.length-1 {
return
}
i := 0
for current != nil {
if i == index-1 {
current.Next = current.Next.Next
link.length--
return nil
sl.length--
return
}
i++
current = current.Next
}
return errors.New("delete error")
}
// DeleteValue delete value in singly linklist
func (link *SinglyLink[T]) DeleteValue(value T) {
if link.Head == nil {
func (sl *SinglyLink[T]) DeleteValue(value T) {
if sl.Head == nil {
return
}
dummyHead := datastructure.NewLinkNode(value)
dummyHead.Next = link.Head
dummyHead.Next = sl.Head
current := dummyHead
for current.Next != nil {
if reflect.DeepEqual(current.Next.Value, value) {
current.Next = current.Next.Next
link.length--
sl.length--
} else {
current = current.Next
}
}
link.Head = dummyHead.Next
sl.Head = dummyHead.Next
}
// Reverse the linked list
func (link *SinglyLink[T]) Reverse() {
func (sl *SinglyLink[T]) Reverse() {
var pre, next *datastructure.LinkNode[T]
current := link.Head
current := sl.Head
for current != nil {
next = current.Next
@@ -179,19 +176,19 @@ func (link *SinglyLink[T]) Reverse() {
current = next
}
link.Head = pre
sl.Head = pre
}
// GetMiddleNode return node at middle index of linked list
func (link *SinglyLink[T]) GetMiddleNode() *datastructure.LinkNode[T] {
if link.Head == nil {
func (sl *SinglyLink[T]) GetMiddleNode() *datastructure.LinkNode[T] {
if sl.Head == nil {
return nil
}
if link.Head.Next == nil {
return link.Head
if sl.Head.Next == nil {
return sl.Head
}
fast := link.Head
slow := link.Head
fast := sl.Head
slow := sl.Head
for fast != nil {
fast = fast.Next
@@ -207,14 +204,14 @@ func (link *SinglyLink[T]) GetMiddleNode() *datastructure.LinkNode[T] {
}
// Size return the count of singly linked list
func (link *SinglyLink[T]) Size() int {
return link.length
func (sl *SinglyLink[T]) Size() int {
return sl.length
}
// Values return slice of all singly linklist node value
func (link *SinglyLink[T]) Values() []T {
func (sl *SinglyLink[T]) Values() []T {
result := []T{}
current := link.Head
current := sl.Head
for current != nil {
result = append(result, current.Value)
current = current.Next
@@ -222,20 +219,20 @@ func (link *SinglyLink[T]) Values() []T {
return result
}
// IsEmpty checks if link is empty or not
func (link *SinglyLink[T]) IsEmpty() bool {
return link.length == 0
// IsEmpty checks if sl is empty or not
func (sl *SinglyLink[T]) IsEmpty() bool {
return sl.length == 0
}
// Clear all the node in singly linklist
func (link *SinglyLink[T]) Clear() {
link.Head = nil
link.length = 0
func (sl *SinglyLink[T]) Clear() {
sl.Head = nil
sl.length = 0
}
// Print all nodes info of a linked list
func (link *SinglyLink[T]) Print() {
current := link.Head
func (sl *SinglyLink[T]) Print() {
current := sl.Head
info := "[ "
for current != nil {
info += fmt.Sprintf("%+v, ", current)

View File

@@ -41,25 +41,12 @@ func TestSinglyLink_InsertAt(t *testing.T) {
link := NewSinglyLink[int]()
err := link.InsertAt(1, 1)
assert.IsNotNil(err)
link.InsertAt(1, 1) //do nothing
err = link.InsertAt(0, 1)
if err != nil {
t.FailNow()
}
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.InsertAt(0, 1)
link.InsertAt(1, 2)
link.InsertAt(2, 4)
link.InsertAt(2, 3)
link.Print()
@@ -73,8 +60,8 @@ func TestSinglyLink_DeleteAtHead(t *testing.T) {
assert := internal.NewAssert(t, "TestSinglyLink_DeleteAtHead")
link := NewSinglyLink[int]()
err := link.DeleteAtHead()
assert.IsNotNil(err)
link.DeleteAtHead()
link.InsertAtTail(1)
link.InsertAtTail(2)
@@ -94,8 +81,6 @@ func TestSinglyLink_DeleteAtTail(t *testing.T) {
assert := internal.NewAssert(t, "TestSinglyLink_DeleteAtTail")
link := NewSinglyLink[int]()
err := link.DeleteAtTail()
assert.IsNotNil(err)
link.InsertAtTail(1)
link.InsertAtTail(2)
@@ -103,7 +88,6 @@ func TestSinglyLink_DeleteAtTail(t *testing.T) {
link.InsertAtTail(4)
link.DeleteAtTail()
link.Print()
expected := []int{1, 2, 3}
values := link.Values()
@@ -133,8 +117,6 @@ func TestSinglyLink_DeleteAt(t *testing.T) {
assert := internal.NewAssert(t, "TestSinglyLink_DeleteAt")
link := NewSinglyLink[int]()
err := link.DeleteAt(0)
assert.IsNotNil(err)
link.InsertAtTail(1)
link.InsertAtTail(2)
@@ -142,11 +124,7 @@ func TestSinglyLink_DeleteAt(t *testing.T) {
link.InsertAtTail(4)
link.InsertAtTail(5)
err = link.DeleteAt(5)
assert.IsNotNil(err)
err = link.DeleteAt(0)
assert.IsNil(err)
link.DeleteAt(0)
assert.Equal([]int{2, 3, 4, 5}, link.Values())
link.DeleteAt(3)

View File

@@ -132,12 +132,12 @@ func main() {
### <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>
```go
func (link *SinglyLink[T]) InsertAt(index int, value T) error
func (link *SinglyLink[T]) InsertAt(index int, value T)
```
<b>Example:</b>
@@ -152,6 +152,8 @@ import (
func main() {
lk := link.NewSinglyLink[int]()
lk.InsertAt(1, 1) //do nothing
lk.InsertAt(0, 1)
lk.InsertAt(1, 2)
lk.InsertAt(2, 3)
@@ -228,12 +230,12 @@ func main() {
### <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>
```go
func (link *SinglyLink[T]) DeleteAt(index int) error
func (link *SinglyLink[T]) DeleteAt(index int)
```
<b>Example:</b>
@@ -253,9 +255,8 @@ func main() {
lk.InsertAtTail(3)
lk.InsertAtTail(4)
err := lk.DeleteAt(3)
lk.DeleteAt(3)
fmt.Println(err) //nil
fmt.Println(lk.Values()) //[]int{1, 2, 3}
}
```
@@ -268,7 +269,7 @@ func main() {
<b>Signature:</b>
```go
func (link *SinglyLink[T]) DeleteAtHead() error
func (link *SinglyLink[T]) DeleteAtHead()
```
<b>Example:</b>
@@ -288,9 +289,8 @@ func main() {
lk.InsertAtTail(3)
lk.InsertAtTail(4)
err := lk.DeleteAtHead()
lk.DeleteAtHead()
fmt.Println(err) //nil
fmt.Println(lk.Values()) //[]int{2, 3, 4}
}
```
@@ -304,7 +304,7 @@ func main() {
<b>Signature:</b>
```go
func (link *SinglyLink[T]) DeleteAtTail() error
func (link *SinglyLink[T]) DeleteAtTail()
```
<b>Example:</b>
@@ -323,9 +323,8 @@ func main() {
lk.InsertAtTail(2)
lk.InsertAtTail(3)
err := lk.DeleteAtTail()
lk.DeleteAtTail()
fmt.Println(err) //nil
fmt.Println(lk.Values()) //[]int{1, 2}
}
```
@@ -628,12 +627,12 @@ func main() {
### <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>
```go
func (link *DoublyLink[T]) InsertAt(index int, value T) error
func (link *DoublyLink[T]) InsertAt(index int, value T)
```
<b>Example:</b>
@@ -648,6 +647,8 @@ import (
func main() {
lk := link.NewDoublyLink[int]()
lk.InsertAt(1, 1) //do nothing
lk.InsertAt(0, 1)
lk.InsertAt(1, 2)
lk.InsertAt(2, 3)
@@ -724,12 +725,12 @@ func main() {
### <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>
```go
func (link *DoublyLink[T]) DeleteAt(index int) error
func (link *DoublyLink[T]) DeleteAt(index int)
```
<b>Example:</b>
@@ -749,9 +750,8 @@ func main() {
lk.InsertAtTail(3)
lk.InsertAtTail(4)
err := lk.DeleteAt(3)
lk.DeleteAt(3)
fmt.Println(err) //nil
fmt.Println(lk.Values()) //[]int{1, 2, 3}
}
```
@@ -764,7 +764,7 @@ func main() {
<b>Signature:</b>
```go
func (link *DoublyLink[T]) DeleteAtHead() error
func (link *DoublyLink[T]) DeleteAtHead()
```
<b>Example:</b>
@@ -784,9 +784,8 @@ func main() {
lk.InsertAtTail(3)
lk.InsertAtTail(4)
err := lk.DeleteAtHead()
lk.DeleteAtHead()
fmt.Println(err) //nil
fmt.Println(lk.Values()) //[]int{2, 3, 4}
}
```

View File

@@ -132,12 +132,12 @@ func main() {
### <span id="SinglyLink_InsertAt">InsertAt</span>
<p>将值插入到索引处的链表中,索引应大于或等于 0 且小于或等于链表节点数</p>
<p>将值插入到索引处的链表中,索引应大于或等于0且小于或等于链表节点数</p>
<b>函数签名:</b>
```go
func (link *SinglyLink[T]) InsertAt(index int, value T) error
func (link *SinglyLink[T]) InsertAt(index int, value T)
```
<b>例子:</b>
@@ -152,6 +152,8 @@ import (
func main() {
lk := link.NewSinglyLink[int]()
lk.InsertAt(1, 1) //do nothing
lk.InsertAt(0, 1)
lk.InsertAt(1, 2)
lk.InsertAt(2, 3)
@@ -228,12 +230,12 @@ func main() {
### <span id="SinglyLink_DeleteAt">DeleteAt</span>
<p>删除特定索引处的值索引应大于或等于0且小于或等于链接节点数 - 1</p>
<p>删除特定索引处的值索引应大于或等于0且小于或等于链接节点数-1</p>
<b>函数签名:</b>
```go
func (link *SinglyLink[T]) DeleteAt(index int) error
func (link *SinglyLink[T]) DeleteAt(index int)
```
<b>例子:</b>
@@ -253,9 +255,8 @@ func main() {
lk.InsertAtTail(3)
lk.InsertAtTail(4)
err := lk.DeleteAt(3)
lk.DeleteAt(3)
fmt.Println(err) //nil
fmt.Println(lk.Values()) //[]int{1, 2, 3}
}
```
@@ -268,7 +269,7 @@ func main() {
<b>函数签名:</b>
```go
func (link *SinglyLink[T]) DeleteAtHead() error
func (link *SinglyLink[T]) DeleteAtHead()
```
<b>例子:</b>
@@ -288,9 +289,8 @@ func main() {
lk.InsertAtTail(3)
lk.InsertAtTail(4)
err := lk.DeleteAtHead()
lk.DeleteAtHead()
fmt.Println(err) //nil
fmt.Println(lk.Values()) //[]int{2, 3, 4}
}
```
@@ -304,7 +304,7 @@ func main() {
<b>函数签名:</b>
```go
func (link *SinglyLink[T]) DeleteAtTail() error
func (link *SinglyLink[T]) DeleteAtTail()
```
<b>例子:</b>
@@ -323,9 +323,8 @@ func main() {
lk.InsertAtTail(2)
lk.InsertAtTail(3)
err := lk.DeleteAtTail()
lk.DeleteAtTail()
fmt.Println(err) //nil
fmt.Println(lk.Values()) //[]int{1, 2}
}
```
@@ -628,12 +627,12 @@ func main() {
### <span id="DoublyLink_InsertAt">InsertAt</span>
<p>将值插入到索引处的链表中,索引应大于或等于 0 且小于或等于链表节点数</p>
<p>将值插入到索引处的链表中,索引应大于或等于0且小于或等于链表节点数</p>
<b>函数签名:</b>
```go
func (link *DoublyLink[T]) InsertAt(index int, value T) error
func (link *DoublyLink[T]) InsertAt(index int, value T)
```
<b>例子:</b>
@@ -648,6 +647,8 @@ import (
func main() {
lk := link.NewDoublyLink[int]()
lk.InsertAt(1, 1) //do nothing
lk.InsertAt(0, 1)
lk.InsertAt(1, 2)
lk.InsertAt(2, 3)
@@ -724,12 +725,12 @@ func main() {
### <span id="DoublyLink_DeleteAt">DeleteAt</span>
<p>删除特定索引处的值索引应大于或等于0且小于或等于链接节点数 - 1</p>
<p>删除特定索引处的值索引应大于或等于0且小于或等于链接节点数-1</p>
<b>函数签名:</b>
```go
func (link *DoublyLink[T]) DeleteAt(index int) error
func (link *DoublyLink[T]) DeleteAt(index int)
```
<b>例子:</b>
@@ -749,9 +750,8 @@ func main() {
lk.InsertAtTail(3)
lk.InsertAtTail(4)
err := lk.DeleteAt(3)
lk.DeleteAt(3)
fmt.Println(err) //nil
fmt.Println(lk.Values()) //[]int{1, 2, 3}
}
```
@@ -764,7 +764,7 @@ func main() {
<b>函数签名:</b>
```go
func (link *DoublyLink[T]) DeleteAtHead() error
func (link *DoublyLink[T]) DeleteAtHead()
```
<b>例子:</b>
@@ -784,9 +784,8 @@ func main() {
lk.InsertAtTail(3)
lk.InsertAtTail(4)
err := lk.DeleteAtHead()
lk.DeleteAtHead()
fmt.Println(err) //nil
fmt.Println(lk.Values()) //[]int{2, 3, 4}
}
```
@@ -800,7 +799,7 @@ func main() {
<b>函数签名:</b>
```go
func (link *DoublyLink[T]) DeleteAtTail() error
func (link *DoublyLink[T]) DeleteAtTail()
```
<b>例子:</b>
@@ -819,9 +818,8 @@ func main() {
lk.InsertAtTail(2)
lk.InsertAtTail(3)
err := lk.DeleteAtTail()
lk.DeleteAtTail()
fmt.Println(err) //nil
fmt.Println(lk.Values()) //[]int{1, 2}
}
```