2019-07-09 15:57:56 +00:00
|
|
|
## listlast
|
2019-04-24 17:06:54 +00:00
|
|
|
|
2019-04-24 17:17:07 +00:00
|
|
|
### Instructions
|
2019-04-24 17:06:54 +00:00
|
|
|
|
2022-07-15 12:47:52 +00:00
|
|
|
Write a function `ListLast` that returns the `Data` interface of the last element of a linked list `l`.
|
2019-04-24 17:06:54 +00:00
|
|
|
|
2019-04-24 17:17:07 +00:00
|
|
|
### Expected function and structure
|
2019-04-24 17:06:54 +00:00
|
|
|
|
|
|
|
```go
|
2019-11-04 10:21:55 +00:00
|
|
|
type NodeL struct {
|
2019-04-24 17:06:54 +00:00
|
|
|
Data interface{}
|
2019-11-04 10:21:55 +00:00
|
|
|
Next *NodeL
|
2019-04-24 17:06:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type List struct {
|
2019-11-04 10:21:55 +00:00
|
|
|
Head *NodeL
|
|
|
|
Tail *NodeL
|
2019-04-24 17:06:54 +00:00
|
|
|
}
|
|
|
|
|
2019-06-24 11:46:15 +00:00
|
|
|
func ListLast(l *List) interface{} {
|
2019-10-09 21:42:35 +00:00
|
|
|
|
2019-04-24 17:06:54 +00:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2019-04-24 17:17:07 +00:00
|
|
|
### Usage
|
2019-04-24 17:06:54 +00:00
|
|
|
|
2020-02-25 12:02:16 +00:00
|
|
|
Here is a possible program to test your function :
|
2019-04-24 17:06:54 +00:00
|
|
|
|
|
|
|
```go
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-06-24 11:34:10 +00:00
|
|
|
|
2021-05-02 15:18:17 +00:00
|
|
|
"piscine"
|
2019-04-24 17:06:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2019-06-24 11:34:10 +00:00
|
|
|
link := &piscine.List{}
|
|
|
|
link2 := &piscine.List{}
|
2019-04-24 17:06:54 +00:00
|
|
|
|
|
|
|
piscine.ListPushBack(link, "three")
|
|
|
|
piscine.ListPushBack(link, 3)
|
|
|
|
piscine.ListPushBack(link, "1")
|
|
|
|
|
2019-06-24 11:34:10 +00:00
|
|
|
fmt.Println(piscine.ListLast(link))
|
|
|
|
fmt.Println(piscine.ListLast(link2))
|
2019-04-24 17:06:54 +00:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
And its output :
|
|
|
|
|
|
|
|
```console
|
2021-04-28 09:47:34 +00:00
|
|
|
$ go run .
|
2019-06-24 11:34:10 +00:00
|
|
|
1
|
2019-04-24 17:06:54 +00:00
|
|
|
<nil>
|
2021-04-28 09:47:34 +00:00
|
|
|
$
|
2019-04-24 17:06:54 +00:00
|
|
|
```
|