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.
86 lines
1.4 KiB
86 lines
1.4 KiB
5 years ago
|
## listmerge
|
||
6 years ago
|
|
||
6 years ago
|
### Instructions
|
||
6 years ago
|
|
||
5 years ago
|
Write a function `ListMerge` that places elements of a list `l2` at the end of another list `l1`.
|
||
6 years ago
|
|
||
5 years ago
|
- New elements should not be created!
|
||
6 years ago
|
|
||
6 years ago
|
### Expected function and structure
|
||
6 years ago
|
|
||
|
```go
|
||
5 years ago
|
type NodeL struct {
|
||
|
Data interface{}
|
||
|
Next *NodeL
|
||
6 years ago
|
}
|
||
|
|
||
5 years ago
|
type List struct {
|
||
|
Head *NodeL
|
||
|
Tail *NodeL
|
||
6 years ago
|
}
|
||
|
|
||
5 years ago
|
func ListMerge(l1 *List, l2 *List) {
|
||
6 years ago
|
|
||
|
}
|
||
|
```
|
||
|
|
||
6 years ago
|
### Usage
|
||
6 years ago
|
|
||
5 years ago
|
Here is a possible program to test your function :
|
||
6 years ago
|
|
||
|
```go
|
||
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
5 years ago
|
|
||
6 years ago
|
piscine ".."
|
||
|
)
|
||
|
|
||
5 years ago
|
func PrintList(l *piscine.List) {
|
||
|
it := l.Head
|
||
|
for it != nil {
|
||
|
fmt.Print(it.Data, " -> ")
|
||
|
it = it.Next
|
||
6 years ago
|
}
|
||
5 years ago
|
fmt.Print(nil, "\n")
|
||
6 years ago
|
}
|
||
|
|
||
|
func main() {
|
||
5 years ago
|
link := &piscine.List{}
|
||
|
link2 := &piscine.List{}
|
||
6 years ago
|
|
||
|
piscine.ListPushBack(link, "a")
|
||
|
piscine.ListPushBack(link, "b")
|
||
|
piscine.ListPushBack(link, "c")
|
||
|
piscine.ListPushBack(link, "d")
|
||
5 years ago
|
fmt.Println("-----first List------")
|
||
|
PrintList(link)
|
||
6 years ago
|
|
||
|
piscine.ListPushBack(link2, "e")
|
||
|
piscine.ListPushBack(link2, "f")
|
||
|
piscine.ListPushBack(link2, "g")
|
||
|
piscine.ListPushBack(link2, "h")
|
||
5 years ago
|
fmt.Println("-----second List------")
|
||
|
PrintList(link2)
|
||
6 years ago
|
|
||
5 years ago
|
fmt.Println("-----Merged List-----")
|
||
6 years ago
|
piscine.ListMerge(link, link2)
|
||
|
PrintList(link)
|
||
|
}
|
||
|
```
|
||
|
|
||
|
And its output :
|
||
|
|
||
|
```console
|
||
5 years ago
|
student@ubuntu:~/[[ROOT]]/test$ go build
|
||
|
student@ubuntu:~/[[ROOT]]/test$ ./test
|
||
5 years ago
|
-----first List------
|
||
|
a -> b -> c -> d -> <nil>
|
||
|
-----second List------
|
||
|
e -> f -> g -> h -> <nil>
|
||
|
-----Merged List-----
|
||
6 years ago
|
a -> b -> c -> d -> e -> f -> g -> h -> <nil>
|
||
5 years ago
|
student@ubuntu:~/[[ROOT]]/test$
|
||
6 years ago
|
```
|