Sync
This commit is contained in:
@@ -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())
|
||||
}
|
||||
@@ -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"))
|
||||
}
|
||||
Reference in New Issue
Block a user