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.
72 lines
964 B
72 lines
964 B
5 years ago
|
## listreverse
|
||
6 years ago
|
|
||
6 years ago
|
### Instructions
|
||
6 years ago
|
|
||
5 years ago
|
Write a function `ListReverse` that reverses the order of the elements of a given linked list `l`.
|
||
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 ListReverse(l *List) {
|
||
5 years ago
|
|
||
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"
|
||
|
piscine ".."
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
5 years ago
|
link := &piscine.List{}
|
||
6 years ago
|
|
||
5 years ago
|
piscine.ListPushBack(link, 1)
|
||
|
piscine.ListPushBack(link, 2)
|
||
|
piscine.ListPushBack(link, 3)
|
||
|
piscine.ListPushBack(link, 4)
|
||
6 years ago
|
|
||
5 years ago
|
piscine.ListReverse(link)
|
||
6 years ago
|
|
||
5 years ago
|
it := link.Head
|
||
|
|
||
|
for it != nil {
|
||
|
fmt.Println(it.Data)
|
||
|
it = it.Next
|
||
6 years ago
|
}
|
||
5 years ago
|
|
||
|
fmt.Println("Tail", link.Tail)
|
||
|
fmt.Println("Head", link.Head)
|
||
6 years ago
|
}
|
||
|
```
|
||
|
|
||
|
And its output :
|
||
|
|
||
|
```console
|
||
5 years ago
|
student@ubuntu:~/[[ROOT]]/test$ go build
|
||
|
student@ubuntu:~/[[ROOT]]/test$ ./test
|
||
6 years ago
|
4
|
||
|
3
|
||
|
2
|
||
|
1
|
||
5 years ago
|
Tail &{1 <nil>}
|
||
5 years ago
|
Head &{4 0xc42000a140}
|
||
5 years ago
|
student@ubuntu:~/[[ROOT]]/test$
|
||
5 years ago
|
```
|