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.
63 lines
790 B
63 lines
790 B
2 years ago
|
## listsize
|
||
|
|
||
|
### Instructions
|
||
|
|
||
|
Write a function `ListSize` that returns the number of elements in a linked list `l`.
|
||
|
|
||
|
### Expected function and structure
|
||
|
|
||
|
```go
|
||
|
type NodeL struct {
|
||
|
Data interface{}
|
||
|
Next *NodeL
|
||
|
}
|
||
|
|
||
|
type List struct {
|
||
|
Head *NodeL
|
||
|
Tail *NodeL
|
||
|
}
|
||
|
|
||
|
func ListSize(l *List) int {
|
||
|
|
||
|
}
|
||
|
```
|
||
|
|
||
|
### Usage
|
||
|
|
||
|
Here is a possible program to test your function :
|
||
|
|
||
|
```go
|
||
|
package main
|
||
|
|
||
|
import "fmt"
|
||
|
|
||
|
func ListPushFront(l *List, data interface{}) {
|
||
|
n := &NodeL{Data: data}
|
||
|
if l.Head == nil {
|
||
|
l.Head = n
|
||
|
l.Tail = n
|
||
|
} else {
|
||
|
l.Head, n.Next = n, l.Head
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
link := &List{}
|
||
|
|
||
|
ListPushFront(link, "Hello")
|
||
|
ListPushFront(link, "2")
|
||
|
ListPushFront(link, "you")
|
||
|
ListPushFront(link, "man")
|
||
|
|
||
|
fmt.Println(ListSize(link))
|
||
|
}
|
||
|
```
|
||
|
|
||
|
And its output :
|
||
|
|
||
|
```console
|
||
|
$ go run .
|
||
|
4
|
||
|
$
|
||
|
```
|