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.

92 lines
1.2 KiB

5 years ago
## listpushback
5 years ago
### Instructions
Write a function `ListSort` that sorts the linked list by ascending order.
- This time you only will have the `node` structure.
- Try to use recursive.
- Use pointers when ever you can.
5 years ago
### Expected function and structure
```go
type node struct {
data int
next *node
}
func ListSort(l *node) *node {
}
```
5 years ago
### Usage
Here is a possible [program](TODO-LINK) to test your function :
```go
package main
import (
"fmt"
piscine ".."
)
//Prints the list
func PrintList(l *node) {
m := l
for m != nil {
fmt.Print(m.data, " -> ")
m = m.next
}
fmt.Print(nil)
fmt.Println()
}
//insert elements
func listPushBack(l *node, data int) {
n := &node{}
n.data = data
n.next = nil
if l == nil {
l = n
return
}
iterator := l
for iterator.next != nil {
iterator = iterator.next
}
iterator.next = n
}
func main() {
link := &node{}
listPushBack(link, 5)
listPushBack(link, 4)
listPushBack(link, 3)
listPushBack(link, 2)
listPushBack(link, 1)
PrintList(piscine.ListSort(link))
}
```
And its output :
```console
student@ubuntu:~/piscine/test$ go build
student@ubuntu:~/piscine/test$ ./test
0 -> 1 -> 2 -> 3 -> 4 -> 5 -> <nil>
student@ubuntu:~/piscine/test$
```