This commit is contained in:
Andrey Parhomenko 2023-10-24 22:03:30 +03:00
parent 75a5d77699
commit d9f2a304dc
3 changed files with 52 additions and 40 deletions

View file

@ -8,22 +8,18 @@ import (
func main() { func main() {
list := lists.NewSingly[string]("zero", "one", "two", "three", "four", "five") list := lists.NewSingly[string]("zero", "one", "two", "three", "four", "five")
fmt.Println(list) fmt.Println(list)
list.Push("-one", "-two") list.InsA(0, "after-1", "after-2")
fmt.Println(list)
list.InsB(0, "-two", "-one")
fmt.Println(list) fmt.Println(list)
list.Swap(0, 2)
//list.Swap(0, 2)
fmt.Println(list) fmt.Println(list)
intList := lists.NewSingly[int](100, 5, -1, 1000, 200, 1337) intList := lists.NewSingly[int](100, 5, -1, 1000, 200, 1337)
fmt.Println(intList) fmt.Println(intList)
intList.Sort(func(vi, vj int) bool { //intList.Get(1000)
return vi < vj
})
fmt.Println(intList)
intList.Sort(func(vi, vj int) bool {
return vj < vi
})
fmt.Println(intList)
} }

View file

@ -2,13 +2,17 @@ package lists
import ( import (
"github.com/reklesio/gods" "github.com/reklesio/gods"
"github.com/reklesio/gods/stacks"
) )
// The interface all the lists must implement. // The interface all the lists must implement.
type List[V any] interface { type List[V any] interface {
gods.Container[V] gods.Container[V]
Push(...V) stacks.Stack[V]
// Get length of the list
// Like push in stacks.
Put(...V)
// Get length of the list.
Len() int Len() int
// Get the value by index. // Get the value by index.
Get(int) V Get(int) V
@ -16,13 +20,13 @@ type List[V any] interface {
Del(int) Del(int)
// Change already existing value. // Change already existing value.
Set(int, V) Set(int, V)
// Add the values // Add the values to the end of the list.
Add(...V) Add(...V)
// Insert the value before the specifed index. // Insert the value before the specified index.
InsB(V, int) InsB(int, ...V)
// Insert the value after the specified index. // Ansert values after the specified index.
InsA(int, V) InsA(int, ...V)
// Swap elements by indexes specified in arguments. // Swap elements by indexes specified in arguments.
Swap(i, j int) Swap(i, j int)

View file

@ -45,7 +45,7 @@ func NewSingly[V any](values ...V) List[V] {
} }
func (ll *sLinkedList[V]) Empty() bool { func (ll *sLinkedList[V]) Empty() bool {
return ll == nil return ll.ln == 0
} }
func (ll *sLinkedList[V]) Size() int { func (ll *sLinkedList[V]) Size() int {
@ -87,32 +87,36 @@ func (ll *sLinkedList[V]) Set(i int, v V) {
} }
// Insert the V value before the i-th element. // Insert the V value before the i-th element.
func (ll *sLinkedList[V]) InsB(v V, i int) { func (ll *sLinkedList[V]) InsB(index int, values ...V) {
if i == 0 { if index == 0 {
ll.before = &sElement[V]{ ll.Put(values...)
value: v, return
next: ll.before.next, }
}
return el := ll.getEl(index-1)
} for _, v := range values {
el := ll.getEl(i-1)
el.next = &sElement[V]{ el.next = &sElement[V]{
value: v, value: v,
next: el.next, next: el.next,
} }
el = el.next
}
ll.ln += len(values)
} }
// Insert the V value after the i-th element. func (ll *sLinkedList[V]) InsA(index int, values ...V) {
func (ll *sLinkedList[V]) InsA(i int, v V) { el := ll.getEl(index)
el := ll.getEl(i)
for _, v := range values {
el.next = &sElement[V]{ el.next = &sElement[V]{
value: v, value: v,
next: el.next, next: el.next,
} }
el = el.next
}
ll.ln += len(values)
} }
// Swap element values indexed by i1 and i2.
// Panic on "index out of range".
func (ll *sLinkedList[V]) Swap(i1, i2 int) { func (ll *sLinkedList[V]) Swap(i1, i2 int) {
if i1 == i2 { if i1 == i2 {
return return
@ -125,7 +129,6 @@ func (ll *sLinkedList[V]) Swap(i1, i2 int) {
el2.value, el1.value el2.value, el1.value
} }
// Deletes the element by its index.
func (ll *sLinkedList[V]) Del(i int) { func (ll *sLinkedList[V]) Del(i int) {
if i == 0 { if i == 0 {
ll.before.next = ll.before.next =
@ -145,15 +148,24 @@ func (ll *sLinkedList[V]) Del(i int) {
ll.ln-- ll.ln--
} }
// Push in the beginning of the list. func (ll *sLinkedList[V]) Put(values ...V) {
func (ll *sLinkedList[V]) Push(values ...V) { ln := len(values)
for _, value := range values { for i:=ln-1 ; i >= 0 ; i-- {
ll.push(value) ll.Push(values[i])
} }
} }
// Push in the beginning of the list. func (ll *sLinkedList[V]) Pop() V {
func (ll *sLinkedList[V]) push(v V) { el := ll.before.next
if el == nil {
panic(gods.IndexRangeErr)
}
ll.before.next = el.next
ll.ln--
return el.value
}
func (ll *sLinkedList[V]) Push(v V) {
prevNext := ll.before.next prevNext := ll.before.next
nextNext := &sElement[V]{ nextNext := &sElement[V]{
next: prevNext, next: prevNext,