forked from root/public
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.
59 lines
1016 B
59 lines
1016 B
![]()
5 years ago
|
## listat
|
||
5 years ago
|
|
||
5 years ago
|
### Instructions
|
||
5 years ago
|
|
||
![]()
5 years ago
|
Write a function `ListAt` that takes a pointer to the list `l` and an `int pos` as parameters. This function should return the `NodeL` in the position `pos` of the linked list `l`.
|
||
5 years ago
|
|
||
5 years ago
|
- In case of error the function should return `nil`.
|
||
5 years ago
|
|
||
5 years ago
|
### Expected function and structure
|
||
5 years ago
|
|
||
|
```go
|
||
![]()
5 years ago
|
type NodeL struct {
|
||
5 years ago
|
Data interface{}
|
||
![]()
5 years ago
|
Next *NodeL
|
||
5 years ago
|
}
|
||
|
|
||
|
|
||
![]()
5 years ago
|
func ListAt(l *NodeL, pos int) *NodeL{
|
||
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
|
|
||
![]()
5 years ago
|
piscine.ListPushBack(link, "hello")
|
||
|
piscine.ListPushBack(link, "how are")
|
||
|
piscine.ListPushBack(link, "you")
|
||
|
piscine.ListPushBack(link, 1)
|
||
5 years ago
|
|
||
![]()
5 years ago
|
fmt.Println(piscine.ListAt(link.Head, 3).Data)
|
||
|
fmt.Println(piscine.ListAt(link.Head, 1).Data)
|
||
|
fmt.Println(piscine.ListAt(link.Head, 7))
|
||
5 years ago
|
}
|
||
|
```
|
||
|
|
||
|
And its output :
|
||
|
|
||
|
```console
|
||
5 years ago
|
student@ubuntu:~/[[ROOT]]/test$ go build
|
||
|
student@ubuntu:~/[[ROOT]]/test$ ./test
|
||
5 years ago
|
1
|
||
|
how are
|
||
5 years ago
|
<nil>
|
||
5 years ago
|
student@ubuntu:~/[[ROOT]]/test$
|
||
![]()
5 years ago
|
```
|