From d9f2a304dc5aa23acb32b1f57e50cdb5089c7aaa Mon Sep 17 00:00:00 2001 From: surdeus Date: Tue, 24 Oct 2023 22:03:30 +0300 Subject: [PATCH] ... --- cmd/ll/main.go | 18 +++++++--------- lists/main.go | 18 +++++++++------- lists/single.go | 56 ++++++++++++++++++++++++++++++------------------- 3 files changed, 52 insertions(+), 40 deletions(-) diff --git a/cmd/ll/main.go b/cmd/ll/main.go index cda6c48..02688c5 100644 --- a/cmd/ll/main.go +++ b/cmd/ll/main.go @@ -8,22 +8,18 @@ import ( func main() { list := lists.NewSingly[string]("zero", "one", "two", "three", "four", "five") 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) - list.Swap(0, 2) + + + //list.Swap(0, 2) fmt.Println(list) intList := lists.NewSingly[int](100, 5, -1, 1000, 200, 1337) fmt.Println(intList) - intList.Sort(func(vi, vj int) bool { - return vi < vj - }) - fmt.Println(intList) - - intList.Sort(func(vi, vj int) bool { - return vj < vi - }) - fmt.Println(intList) + //intList.Get(1000) } diff --git a/lists/main.go b/lists/main.go index b9371c4..4fb0528 100644 --- a/lists/main.go +++ b/lists/main.go @@ -2,13 +2,17 @@ package lists import ( "github.com/reklesio/gods" + "github.com/reklesio/gods/stacks" ) // The interface all the lists must implement. type List[V any] interface { gods.Container[V] - Push(...V) - // Get length of the list + stacks.Stack[V] + + // Like push in stacks. + Put(...V) + // Get length of the list. Len() int // Get the value by index. Get(int) V @@ -16,13 +20,13 @@ type List[V any] interface { Del(int) // Change already existing value. Set(int, V) - // Add the values + // Add the values to the end of the list. Add(...V) - // Insert the value before the specifed index. - InsB(V, int) - // Insert the value after the specified index. - InsA(int, V) + // Insert the value before the specified index. + InsB(int, ...V) + // Ansert values after the specified index. + InsA(int, ...V) // Swap elements by indexes specified in arguments. Swap(i, j int) diff --git a/lists/single.go b/lists/single.go index 3d130b6..589c510 100644 --- a/lists/single.go +++ b/lists/single.go @@ -45,7 +45,7 @@ func NewSingly[V any](values ...V) List[V] { } func (ll *sLinkedList[V]) Empty() bool { - return ll == nil + return ll.ln == 0 } 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. -func (ll *sLinkedList[V]) InsB(v V, i int) { - if i == 0 { - ll.before = &sElement[V]{ - value: v, - next: ll.before.next, - } - return - } - el := ll.getEl(i-1) +func (ll *sLinkedList[V]) InsB(index int, values ...V) { + if index == 0 { + ll.Put(values...) + return + } + + el := ll.getEl(index-1) + for _, v := range values { el.next = &sElement[V]{ value: v, next: el.next, } + el = el.next + } + ll.ln += len(values) } -// Insert the V value after the i-th element. -func (ll *sLinkedList[V]) InsA(i int, v V) { - el := ll.getEl(i) +func (ll *sLinkedList[V]) InsA(index int, values ...V) { + el := ll.getEl(index) + + for _, v := range values { el.next = &sElement[V]{ value: v, 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) { if i1 == i2 { return @@ -125,7 +129,6 @@ func (ll *sLinkedList[V]) Swap(i1, i2 int) { el2.value, el1.value } -// Deletes the element by its index. func (ll *sLinkedList[V]) Del(i int) { if i == 0 { ll.before.next = @@ -145,15 +148,24 @@ func (ll *sLinkedList[V]) Del(i int) { ll.ln-- } -// Push in the beginning of the list. -func (ll *sLinkedList[V]) Push(values ...V) { - for _, value := range values { - ll.push(value) +func (ll *sLinkedList[V]) Put(values ...V) { + ln := len(values) + for i:=ln-1 ; i >= 0 ; i-- { + ll.Push(values[i]) } } -// Push in the beginning of the list. -func (ll *sLinkedList[V]) push(v V) { +func (ll *sLinkedList[V]) Pop() 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 nextNext := &sElement[V]{ next: prevNext,