forked from steger/pr3-ws202526
52 lines
791 B
Go
52 lines
791 B
Go
package main
|
|
|
|
import "fmt"
|
|
|
|
func SlicesIndex[S []E, E int](s []string, v string) int {
|
|
for i := range s {
|
|
if v == s[i] {
|
|
return i
|
|
}
|
|
}
|
|
return -1
|
|
}
|
|
|
|
type List struct {
|
|
head, tail *element
|
|
}
|
|
|
|
type element struct {
|
|
next *element
|
|
val int
|
|
}
|
|
|
|
func (lst *List) Push(v int) {
|
|
if lst.tail == nil {
|
|
lst.head = &element{val: v}
|
|
lst.tail = lst.head
|
|
} else {
|
|
lst.tail.next = &element{val: v}
|
|
lst.tail = lst.tail.next
|
|
}
|
|
}
|
|
|
|
func (lst *List) AllElements() []int {
|
|
var elems []int
|
|
for e := lst.head; e != nil; e = e.next {
|
|
elems = append(elems, e.val)
|
|
}
|
|
return elems
|
|
}
|
|
|
|
func main() {
|
|
var s = []string{"foo", "bar", "zoo"}
|
|
|
|
fmt.Println("index of zoo:", SlicesIndex(s, "zoo"))
|
|
|
|
lst := List{}
|
|
lst.Push(10)
|
|
lst.Push(13)
|
|
lst.Push(23)
|
|
fmt.Println("list:", lst.AllElements())
|
|
}
|