This commit is contained in:
V
2025-09-26 15:43:54 +01:00
parent cd080ddda2
commit ba7812bfdd
17 changed files with 332 additions and 12 deletions
+36
View File
@@ -0,0 +1,36 @@
package main
import (
"fmt"
)
type List[T any] struct {
next *List[T]
val T
}
func (l *List[T]) Prepend(v T) *List[T] {
return &List[T]{next: l, val: v}
}
func (l *List[t]) Len() int {
count := 0
for cur := l; cur != nil; cur = cur.next {
count++
}
return count
}
func main() {
var test_list *List[int]
fmt.Println(test_list)
test_list = test_list.Prepend(10)
fmt.Println(test_list)
test_list = test_list.Prepend(20)
fmt.Println(test_list)
fmt.Printf("The list has %d items\n", test_list.Len())
test_list = test_list.Prepend(30)
fmt.Println(test_list)
fmt.Printf("The list has %d items", test_list.Len())
}
+20
View File
@@ -0,0 +1,20 @@
package main
import "fmt"
func Index[T comparable](s []T, x T) int {
for i, v := range s {
if v == x {
return i
}
}
return -1
}
func main() {
si := []int{10, 20, 15, -10}
fmt.Println(Index(si, 15))
ss := []string{"foo", "bar", "bax"}
fmt.Println(Index(ss, "hello"))
}