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.
69 lines
1.0 KiB
69 lines
1.0 KiB
![]()
5 years ago
|
## listclear
|
||
5 years ago
|
|
||
5 years ago
|
### Instructions
|
||
5 years ago
|
|
||
![]()
5 years ago
|
Write a function `ListClear` that deletes all `nodes` from a linked list `l`.
|
||
5 years ago
|
|
||
5 years ago
|
- Tip: assign the list's pointer to `nil`.
|
||
5 years ago
|
|
||
5 years ago
|
### Expected function and structure
|
||
5 years ago
|
|
||
|
```go
|
||
|
func ListClear(l *List) {
|
||
![]()
5 years ago
|
|
||
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"
|
||
5 years ago
|
|
||
5 years ago
|
piscine ".."
|
||
|
)
|
||
|
|
||
|
type List = piscine.List
|
||
5 years ago
|
type Node = piscine.NodeL
|
||
5 years ago
|
|
||
|
func PrintList(l *List) {
|
||
|
link := l.Head
|
||
|
for link != nil {
|
||
|
fmt.Print(link.Data, " -> ")
|
||
|
link = link.Next
|
||
|
}
|
||
|
fmt.Println(nil)
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
link := &List{}
|
||
|
|
||
|
piscine.ListPushBack(link, "I")
|
||
|
piscine.ListPushBack(link, 1)
|
||
|
piscine.ListPushBack(link, "something")
|
||
|
piscine.ListPushBack(link, 2)
|
||
|
|
||
|
fmt.Println("------list------")
|
||
|
PrintList(link)
|
||
|
piscine.ListClear(link)
|
||
|
fmt.Println("------updated list------")
|
||
|
PrintList(link)
|
||
|
}
|
||
|
```
|
||
|
|
||
|
And its output :
|
||
|
|
||
|
```console
|
||
5 years ago
|
student@ubuntu:~/[[ROOT]]/test$ go build
|
||
|
student@ubuntu:~/[[ROOT]]/test$ ./test
|
||
5 years ago
|
------list------
|
||
|
I -> 1 -> something -> 2 -> <nil>
|
||
|
------updated list------
|
||
|
<nil>
|
||
5 years ago
|
student@ubuntu:~/[[ROOT]]/test$
|
||
5 years ago
|
```
|