Added comparable linked list functions.
This commit is contained in:
parent
4494e83428
commit
575c83ef52
3 changed files with 33 additions and 8 deletions
|
@ -41,13 +41,12 @@ func main() {
|
||||||
fmt.Printf("%q\n", mapx.Reverse(m))
|
fmt.Printf("%q\n", mapx.Reverse(m))
|
||||||
fmt.Printf("%v\n", mapx.Reverse(m1))
|
fmt.Printf("%v\n", mapx.Reverse(m1))
|
||||||
|
|
||||||
ll := llx.New[int]()
|
ll := llx.NewComparable[int]()
|
||||||
ll.Append(0)
|
ll.Append(0)
|
||||||
ll.Append(1)
|
ll.Append(1)
|
||||||
ll.Append(2)
|
ll.Append(2)
|
||||||
ll.Push(11)
|
|
||||||
ll.Set(1, 256)
|
ll.Set(1, 256)
|
||||||
ll.Del(3)
|
ll.DelVal(256)
|
||||||
for p := range ll.Range() {
|
for p := range ll.Range() {
|
||||||
fmt.Println(p)
|
fmt.Println(p)
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,6 +25,19 @@ type Pair[V any] struct {
|
||||||
V V
|
V V
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ComparableLinkedList[V comparable] struct {
|
||||||
|
*LinkedList[V]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Returns new empty linked list storing any COMPARABLE values
|
||||||
|
// and adds a few more methods.
|
||||||
|
func NewComparable[V comparable]() *ComparableLinkedList[V] {
|
||||||
|
return &ComparableLinkedList[V]{
|
||||||
|
New[V](),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Returns new empty linked list storing the V type.
|
// Returns new empty linked list storing the V type.
|
||||||
func New[V any]() *LinkedList[V] {
|
func New[V any]() *LinkedList[V] {
|
||||||
return &LinkedList[V]{
|
return &LinkedList[V]{
|
||||||
|
@ -98,10 +111,21 @@ func (ll *LinkedList[V]) Del(i int) (bool) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
/*func (ll *LinkedList[V comparable]) DelByVal(v V) int {
|
// Deletes the first appearance of the value in the list.
|
||||||
p := ll.before.next
|
func (cll *ComparableLinkedList[V]) DelVal(v V) bool {
|
||||||
i := 0
|
i := 0
|
||||||
}*/
|
ll := cll.LinkedList
|
||||||
|
for p:= ll.before.next ; p != nil ; p = p.next {
|
||||||
|
if p.value == v {
|
||||||
|
ll.Del(i)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
// Push in the beginning of the list.
|
// Push in the beginning of the list.
|
||||||
func (ll *LinkedList[V]) Push(v V) {
|
func (ll *LinkedList[V]) Push(v V) {
|
||||||
|
|
|
@ -6,7 +6,7 @@ import (
|
||||||
|
|
||||||
// Ordered value-only based structure.
|
// Ordered value-only based structure.
|
||||||
// Fast deleting by value.
|
// Fast deleting by value.
|
||||||
// Cannot store multiple the same values.
|
// Cannot store multiple equal values.
|
||||||
|
|
||||||
type Pool[V comparable] struct {
|
type Pool[V comparable] struct {
|
||||||
store *llx.LinkedList[V]
|
store *llx.LinkedList[V]
|
||||||
|
@ -23,6 +23,8 @@ func New[V comparable]() *Pool {
|
||||||
|
|
||||||
func (p *Pool[V]) Append(v V) {
|
func (p *Pool[V]) Append(v V) {
|
||||||
p.store.Append(v)
|
p.store.Append(v)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Pool[V]) Del(v V) bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue