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.
96 lines
1.7 KiB
96 lines
1.7 KiB
![]()
5 years ago
|
## listremoveif
|
||
5 years ago
|
|
||
5 years ago
|
### Instructions
|
||
5 years ago
|
|
||
![]()
5 years ago
|
Write a function `ListRemoveIf` that removes all elements that are equal to the `data_ref` in the argument of the function.
|
||
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{}
|
||
|
Next *NodeL
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
type List struct {
|
||
|
Head *NodeL
|
||
|
Tail *NodeL
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
func ListRemoveIf(l *List, data_ref 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"
|
||
5 years ago
|
|
||
5 years ago
|
piscine ".."
|
||
|
)
|
||
|
|
||
5 years ago
|
func PrintList(l *piscine.List) {
|
||
|
it := l.Head
|
||
|
for it != nil {
|
||
|
fmt.Print(it.Data, " -> ")
|
||
|
it = it.Next
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
fmt.Print(nil, "\n")
|
||
5 years ago
|
}
|
||
|
|
||
|
func main() {
|
||
5 years ago
|
link := &piscine.List{}
|
||
|
link2 := &piscine.List{}
|
||
5 years ago
|
|
||
|
fmt.Println("----normal state----")
|
||
|
piscine.ListPushBack(link2, 1)
|
||
|
PrintList(link2)
|
||
5 years ago
|
piscine.ListRemoveIf(link2, 1)
|
||
5 years ago
|
fmt.Println("------answer-----")
|
||
5 years ago
|
PrintList(link2)
|
||
5 years ago
|
fmt.Println()
|
||
|
|
||
|
fmt.Println("----normal state----")
|
||
|
piscine.ListPushBack(link, 1)
|
||
|
piscine.ListPushBack(link, "Hello")
|
||
|
piscine.ListPushBack(link, 1)
|
||
|
piscine.ListPushBack(link, "There")
|
||
|
piscine.ListPushBack(link, 1)
|
||
|
piscine.ListPushBack(link, 1)
|
||
|
piscine.ListPushBack(link, "How")
|
||
|
piscine.ListPushBack(link, 1)
|
||
|
piscine.ListPushBack(link, "are")
|
||
|
piscine.ListPushBack(link, "you")
|
||
|
piscine.ListPushBack(link, 1)
|
||
|
PrintList(link)
|
||
|
|
||
5 years ago
|
piscine.ListRemoveIf(link, 1)
|
||
5 years ago
|
fmt.Println("------answer-----")
|
||
|
PrintList(link)
|
||
|
}
|
||
|
```
|
||
|
|
||
|
And its output :
|
||
|
|
||
|
```console
|
||
5 years ago
|
student@ubuntu:~/[[ROOT]]/test$ go build
|
||
|
student@ubuntu:~/[[ROOT]]/test$ ./test
|
||
5 years ago
|
----normal state----
|
||
|
1 -> <nil>
|
||
|
------answer-----
|
||
|
<nil>
|
||
|
|
||
|
----normal state----
|
||
|
1 -> Hello -> 1 -> There -> 1 -> 1 -> How -> 1 -> are -> you -> 1 -> <nil>
|
||
|
------answer-----
|
||
|
Hello -> There -> How -> are -> you -> <nil>
|
||
5 years ago
|
student@ubuntu:~/[[ROOT]]/test$
|
||
5 years ago
|
```
|