Browse Source

change function ListForEachIf and some instructions

content-update
Augusto 5 years ago
parent
commit
988689d839
  1. 46
      subjects/listforeachif.en.md

46
subjects/listforeachif.en.md

@ -2,32 +2,34 @@
### Instructions ### Instructions
Write a function `ListForEachIf` that applies a function given as argument to the information within some links of the list. Write a function `ListForEachIf` that applies a function given as argument to the information within some nodes of the list.
- For this you will have to create a function `CompStr`, that returns a `bool`, to compare each elemente of the linked list, to see if it is a string, and than apply the function in the argument of `ListForEachIf`. - This functions receives two functions:
- The function given as argument as to have a pointer as argument: `l *list`. - `f` is a functions that is applied to the node.
- Use pointers wen ever you can. - `comp` is a predicate (a function that returns true or false) and will be use to determine if the function `f` would be applied to the node.
- The function given as argument must have a pointer as argument: `*NodeL`.
### Expected function and structure ### Expected function and structure
```go ```go
type node struct { type NodeL struct {
data interface{} Data interface{}
next *node Next *NodeL
} }
type list struct { type List struct {
head *node Head *NodeL
tail *node Tail *NodeL
} }
func CompStr(l *list) bool { func CompStr(l *NodeL) bool {
} }
func ListForEachIf(l *list, f func(l *list), comp func(l *list) bool) { func ListForEachIf(l *List, f func(*NodeL), comp func(*NodeL) bool) {
} }
``` ```
@ -44,26 +46,26 @@ import (
piscine ".." piscine ".."
) )
func PrintElem(l *list) { func PrintElem(l *List) {
fmt.Println(l.head.data) fmt.Println(l.Head.Data)
} }
func StringToInt(l *list) { func StringToInt(l *List) {
count := 1 count := 1
l.head.data = count l.Head.Data = count
} }
func PrintList(l *list) { func PrintList(l *List) {
m := l.head m := l.Head
for m != nil { for m != nil {
fmt.Print(m.data, " -> ") fmt.Print(m.Data, " -> ")
m = m.next m = m.Next
} }
fmt.Print(l.tail) fmt.Print(l.Tail)
} }
func main() { func main() {
link := &list{} link := &List{}
piscine.ListPushBack(link, 1) piscine.ListPushBack(link, 1)
piscine.ListPushBack(link, "hello") piscine.ListPushBack(link, "hello")

Loading…
Cancel
Save