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.
86 lines
1.4 KiB
86 lines
1.4 KiB
5 years ago
|
## listmerge
|
||
5 years ago
|
|
||
5 years ago
|
### Instructions
|
||
5 years ago
|
|
||
![]()
5 years ago
|
Write a function `ListMerge` that places elements of a list `l2` at the end of another list `l1`.
|
||
5 years ago
|
|
||
5 years ago
|
- New elements should not be created!
|
||
5 years ago
|
|
||
5 years ago
|
### Expected function and structure
|
||
5 years ago
|
|
||
|
```go
|
||
5 years ago
|
type NodeL struct {
|
||
|
Data interface{}
|
||
|
Next *NodeL
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
type List struct {
|
||
|
Head *NodeL
|
||
|
Tail *NodeL
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
func ListMerge(l1 *List, l2 *List) {
|
||
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
|
|
||
|
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)
|
||
5 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)
|
||
5 years ago
|
|
||
5 years ago
|
fmt.Println("-----Merged List-----")
|
||
5 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-----
|
||
5 years ago
|
a -> b -> c -> d -> e -> f -> g -> h -> <nil>
|
||
5 years ago
|
student@ubuntu:~/[[ROOT]]/test$
|
||
5 years ago
|
```
|