mirror of https://github.com/01-edu/public.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
1.2 KiB
71 lines
1.2 KiB
![]()
5 years ago
|
## listfind
|
||
5 years ago
|
|
||
5 years ago
|
### Instructions
|
||
5 years ago
|
|
||
![]()
5 years ago
|
Write a function `ListFind` that returns the address of the first node in the list `l` that is determined to be equal to `ref` by the function `CompStr`.
|
||
5 years ago
|
|
||
5 years ago
|
- For this exercise the function `CompStr` must be used.
|
||
5 years ago
|
|
||
5 years ago
|
### Expected function and structure
|
||
5 years ago
|
|
||
|
```go
|
||
![]()
5 years ago
|
type NodeL struct {
|
||
|
Data interface{}
|
||
|
Next *NodeL
|
||
5 years ago
|
}
|
||
|
|
||
![]()
5 years ago
|
type List struct {
|
||
|
Head *NodeL
|
||
|
Tail *NodeL
|
||
5 years ago
|
}
|
||
|
|
||
![]()
5 years ago
|
func CompStr(a, b interface{}) bool {
|
||
|
return a == b
|
||
5 years ago
|
}
|
||
|
|
||
![]()
5 years ago
|
func ListFind(l *List, ref interface{}, comp func(a, b interface{}) bool) *interface{} {
|
||
5 years ago
|
|
||
|
}
|
||
|
```
|
||
|
|
||
5 years ago
|
### Usage
|
||
5 years ago
|
|
||
5 years ago
|
Here is a possible program to test your function :
|
||
5 years ago
|
|
||
|
```go
|
||
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
piscine ".."
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
![]()
5 years ago
|
link := &piscine.List{}
|
||
5 years ago
|
|
||
|
piscine.ListPushBack(link, "hello")
|
||
|
piscine.ListPushBack(link, "hello1")
|
||
|
piscine.ListPushBack(link, "hello2")
|
||
|
piscine.ListPushBack(link, "hello3")
|
||
|
|
||
![]()
5 years ago
|
found := piscine.ListFind(link, interface{}("hello2"), piscine.CompStr)
|
||
|
|
||
|
fmt.Println(found)
|
||
|
fmt.Println(*found)
|
||
5 years ago
|
}
|
||
|
```
|
||
|
|
||
|
And its output :
|
||
|
|
||
|
```console
|
||
5 years ago
|
student@ubuntu:~/[[ROOT]]/test$ go build
|
||
|
student@ubuntu:~/[[ROOT]]/test$ ./test
|
||
5 years ago
|
0xc42000a0a0
|
||
![]()
5 years ago
|
hello2
|
||
5 years ago
|
student@ubuntu:~/[[ROOT]]/test$
|
||
5 years ago
|
```
|
||
![]()
5 years ago
|
|
||
![]()
5 years ago
|
### Note
|
||
|
|
||
![]()
4 years ago
|
- The address may be different in each execution of the program.
|