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.

83 lines
1.3 KiB

5 years ago
## listpushback
5 years ago
### Instructions
Write a function `SortedListMerge` that mereges two lists, `n1` and `n2`, but it as to join them in ascending order.
- Tip each list as to be already sorted.
- Use pointers when ever you can.
5 years ago
### Expected function and structure
```go
func SortedListMerge(n1 *Nodee, n2 *Nodee) *Nodee {
}
```
5 years ago
### Usage
Here is a possible [program](TODO-LINK) to test your function :
```go
package main
import (
"fmt"
piscine ".."
)
type node = piscine.Nodee
type nodes = piscine.Nodee
func PrintList(l *piscine.Nodee) {
m := l
for m != nil {
fmt.Print(m.Data, " -> ")
m = m.Next
}
fmt.Print(nil)
fmt.Println()
}
func listPushBack(l *piscine.Nodee, data int) *piscine.Nodee {
n := &piscine.Nodee{Data: data}
if l == nil {
return n
}
iterator := l
for iterator.Next != nil {
iterator = iterator.Next
}
iterator.Next = n
return l
}
func main() {
var link *node
var link2 *nodes
link = listPushBack(link, 5)
link = listPushBack(link, 3)
link = listPushBack(link, 7)
link2 = listPushBack(link2, -2)
link2 = listPushBack(link2, 4)
PrintList(piscine.SortedListMerge(link2, link))
}
```
And its output :
```console
student@ubuntu:~/piscine/test$ go build
student@ubuntu:~/piscine/test$ ./test
-2 -> 3 -> 4 -> 5 -> 7 -> <nil>
student@ubuntu:~/piscine/test$
```