|
|
@ -4,22 +4,42 @@ |
|
|
|
|
|
|
|
|
|
|
|
Write a function `ListForEach` that applies a function given as argument to the information within each of the list's links. |
|
|
|
Write a function `ListForEach` that applies a function given as argument to the information within each of the list's links. |
|
|
|
|
|
|
|
|
|
|
|
- The function given as argument must have a pointer as argument: `l *list` |
|
|
|
- The function given as argument must have a pointer as argument: `l *List` |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- Copy the functions `Add2_node` and `Subtract3_node` in the same file you defined the function `ListForEach`. |
|
|
|
|
|
|
|
|
|
|
|
### 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 { |
|
|
|
|
|
|
|
Head *NodeL |
|
|
|
|
|
|
|
Tail *NodeL |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
type list struct { |
|
|
|
func ListForEach(l *List, f func(*NodeL)) { |
|
|
|
head *node |
|
|
|
|
|
|
|
tail *node |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func ListForEach(l *list, f func(l *list)) { |
|
|
|
func Add2_node(node *NodeL) { |
|
|
|
|
|
|
|
switch node.Data.(type) { |
|
|
|
|
|
|
|
case int: |
|
|
|
|
|
|
|
node.Data = node.Data.(int) + 2 |
|
|
|
|
|
|
|
case string: |
|
|
|
|
|
|
|
node.Data = node.Data.(string) + "2" |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func Subtract3_node(node *NodeL) { |
|
|
|
|
|
|
|
switch node.Data.(type) { |
|
|
|
|
|
|
|
case int: |
|
|
|
|
|
|
|
node.Data = node.Data.(int) - 3 |
|
|
|
|
|
|
|
case string: |
|
|
|
|
|
|
|
node.Data = node.Data.(string) + "-3" |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
``` |
|
|
|
``` |
|
|
|
|
|
|
|
|
|
|
@ -36,18 +56,19 @@ import ( |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
func main() { |
|
|
|
func main() { |
|
|
|
link := &list{} |
|
|
|
link := &piscine.List{} |
|
|
|
|
|
|
|
|
|
|
|
piscine.ListPushBack(link, 1) |
|
|
|
piscine.ListPushBack(link, "1") |
|
|
|
piscine.ListPushBack(link, 2) |
|
|
|
piscine.ListPushBack(link, "2") |
|
|
|
piscine.ListPushBack(link, 3) |
|
|
|
piscine.ListPushBack(link, "3") |
|
|
|
piscine.ListPushBack(link, 4) |
|
|
|
piscine.ListPushBack(link, "5") |
|
|
|
|
|
|
|
|
|
|
|
piscine.ListForEach(link, piscine.ListReverse) |
|
|
|
piscine.ListForEach(link, piscine.Add2) |
|
|
|
|
|
|
|
|
|
|
|
for link.head != nil { |
|
|
|
it := link.Head |
|
|
|
fmt.Println(link.head.data) |
|
|
|
for it != nil { |
|
|
|
link.head = link.head.next |
|
|
|
fmt.Println(it.Data) |
|
|
|
|
|
|
|
it = it.Next |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
``` |
|
|
|
``` |
|
|
@ -57,9 +78,9 @@ And its output : |
|
|
|
```console |
|
|
|
```console |
|
|
|
student@ubuntu:~/piscine/test$ go build |
|
|
|
student@ubuntu:~/piscine/test$ go build |
|
|
|
student@ubuntu:~/piscine/test$ ./test |
|
|
|
student@ubuntu:~/piscine/test$ ./test |
|
|
|
4 |
|
|
|
12 |
|
|
|
3 |
|
|
|
22 |
|
|
|
2 |
|
|
|
32 |
|
|
|
1 |
|
|
|
52 |
|
|
|
student@ubuntu:~/piscine/test$ |
|
|
|
student@ubuntu:~/piscine/test$ |
|
|
|
``` |
|
|
|
``` |
|
|
|