forked from root/public
66 changed files with 0 additions and 1161 deletions
@ -1,43 +0,0 @@ |
|||||||
## join |
|
||||||
|
|
||||||
### Instructions |
|
||||||
|
|
||||||
Write a function, Join, that returns the elements of a slice strings (arstr) join together in one string. Using the string 'sep' as a separator between each element of the array |
|
||||||
|
|
||||||
The function must have the next signature. |
|
||||||
|
|
||||||
### Expected function |
|
||||||
|
|
||||||
```go |
|
||||||
func Join(arstr []string, sep string) string { |
|
||||||
|
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
### Usage |
|
||||||
|
|
||||||
Here is a possible [program](TODO-LINK) to test your function : |
|
||||||
|
|
||||||
```go |
|
||||||
package main |
|
||||||
|
|
||||||
import ( |
|
||||||
"fmt" |
|
||||||
student ".." |
|
||||||
) |
|
||||||
|
|
||||||
func main() { |
|
||||||
arrStr := []string{"hello", "how", "are", "you"} |
|
||||||
joined := student.Join(arrStr, "--") |
|
||||||
fmt.Println(joined) |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
And its output : |
|
||||||
|
|
||||||
```console |
|
||||||
student@ubuntu:~/student/join$ go build |
|
||||||
student@ubuntu:~/student/join$ ./join |
|
||||||
hello--how--are--you |
|
||||||
student@ubuntu:~/student/join$ |
|
||||||
``` |
|
@ -1,60 +0,0 @@ |
|||||||
## listpushback |
|
||||||
|
|
||||||
### Instructions |
|
||||||
|
|
||||||
Write a function `ListAt` that haves one pointer to the list, `l`, and an `int` as parameters. This function should print a `Node` of the linked list, depending on the number, `nbr`. |
|
||||||
|
|
||||||
- In case of error it should print `nil` |
|
||||||
|
|
||||||
### Expected function and structure |
|
||||||
|
|
||||||
```go |
|
||||||
type Node struct { |
|
||||||
Data interface{} |
|
||||||
Next *Node |
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
func ListAt(l *Node, nbr int) *Node{ |
|
||||||
|
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
### Usage |
|
||||||
|
|
||||||
Here is a possible [program](TODO-LINK) to test your function : |
|
||||||
|
|
||||||
```go |
|
||||||
package main |
|
||||||
|
|
||||||
import ( |
|
||||||
"fmt" |
|
||||||
piscine ".." |
|
||||||
) |
|
||||||
|
|
||||||
func main() { |
|
||||||
link := &Node{} |
|
||||||
|
|
||||||
ListPushBack(link, "hello") |
|
||||||
ListPushBack(link, "how are") |
|
||||||
ListPushBack(link, "you") |
|
||||||
ListPushBack(link, 1) |
|
||||||
|
|
||||||
fmt.Println() |
|
||||||
|
|
||||||
fmt.Println(ListAt(link, 3).Data) |
|
||||||
fmt.Println(ListAt(link, 1).Data) |
|
||||||
fmt.Println(ListAt(link, 7)) |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
And its output : |
|
||||||
|
|
||||||
```console |
|
||||||
student@ubuntu:~/piscine/test$ go build |
|
||||||
student@ubuntu:~/piscine/test$ ./test |
|
||||||
you |
|
||||||
hello |
|
||||||
<nil> |
|
||||||
student@ubuntu:~/piscine/test$ |
|
||||||
``` |
|
@ -1,77 +0,0 @@ |
|||||||
## listpushback |
|
||||||
|
|
||||||
### Instructions |
|
||||||
|
|
||||||
Write a function `ListClear` that delets all `nodes` from a linked list, deleting the link between the list. |
|
||||||
|
|
||||||
- Tip: assign the list's pointer to nil |
|
||||||
|
|
||||||
### Expected function and structure |
|
||||||
|
|
||||||
```go |
|
||||||
type Node struct { |
|
||||||
Data interface{} |
|
||||||
Next *Node |
|
||||||
} |
|
||||||
|
|
||||||
type List struct { |
|
||||||
Head *Node |
|
||||||
Tail *Node |
|
||||||
} |
|
||||||
|
|
||||||
func ListClear(l *List) { |
|
||||||
|
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
### Usage |
|
||||||
|
|
||||||
Here is a possible [program](TODO-LINK) to test your function : |
|
||||||
|
|
||||||
```go |
|
||||||
package main |
|
||||||
|
|
||||||
import ( |
|
||||||
"fmt" |
|
||||||
piscine ".." |
|
||||||
) |
|
||||||
|
|
||||||
type List = piscine.List |
|
||||||
type Node = piscine.Node |
|
||||||
|
|
||||||
func PrintList(l *List) { |
|
||||||
link := l.Head |
|
||||||
for link != nil { |
|
||||||
fmt.Print(link.Data, " -> ") |
|
||||||
link = link.Next |
|
||||||
} |
|
||||||
fmt.Println(nil) |
|
||||||
} |
|
||||||
|
|
||||||
func main() { |
|
||||||
link := &List{} |
|
||||||
|
|
||||||
piscine.ListPushBack(link, "I") |
|
||||||
piscine.ListPushBack(link, 1) |
|
||||||
piscine.ListPushBack(link, "something") |
|
||||||
piscine.ListPushBack(link, 2) |
|
||||||
|
|
||||||
fmt.Println("------list------") |
|
||||||
PrintList(link) |
|
||||||
piscine.ListClear(link) |
|
||||||
fmt.Println("------updated list------") |
|
||||||
PrintList(link) |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
And its output : |
|
||||||
|
|
||||||
```console |
|
||||||
student@ubuntu:~/piscine/test$ go build |
|
||||||
student@ubuntu:~/piscine/test$ ./test |
|
||||||
------list------ |
|
||||||
I -> 1 -> something -> 2 -> <nil> |
|
||||||
------updated list------ |
|
||||||
<nil> |
|
||||||
student@ubuntu:~/piscine/test$ |
|
||||||
``` |
|
@ -1,64 +0,0 @@ |
|||||||
## listpushback |
|
||||||
|
|
||||||
### Instructions |
|
||||||
|
|
||||||
Write a function `ListFind` that returns the value of the first link that the function in the arguments its equal. |
|
||||||
|
|
||||||
- For this you shoud use the function `CompStr`. |
|
||||||
|
|
||||||
- Use pointers when ever you can. |
|
||||||
|
|
||||||
### Expected function and structure |
|
||||||
|
|
||||||
```go |
|
||||||
type Node struct { |
|
||||||
Data interface{} |
|
||||||
Next *Node |
|
||||||
} |
|
||||||
|
|
||||||
type List struct { |
|
||||||
Head *Node |
|
||||||
Tail *Node |
|
||||||
} |
|
||||||
|
|
||||||
func CompStr(l *list) bool { |
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
func ListFind(l *List, comp func(l *List) bool) *interface{} { |
|
||||||
|
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
### Usage |
|
||||||
|
|
||||||
Here is a possible [program](TODO-LINK) to test your function : |
|
||||||
|
|
||||||
```go |
|
||||||
package main |
|
||||||
|
|
||||||
import ( |
|
||||||
"fmt" |
|
||||||
piscine ".." |
|
||||||
) |
|
||||||
|
|
||||||
func main() { |
|
||||||
link := &List{} |
|
||||||
|
|
||||||
piscine.ListPushBack(link, 1) |
|
||||||
piscine.ListPushBack(link, "hello") |
|
||||||
piscine.ListPushBack(link, "hello2") |
|
||||||
piscine.ListPushBack(link, "hello3") |
|
||||||
|
|
||||||
fmt.Println(piscine.ListFind(link, CompStr)) |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
And its output : |
|
||||||
|
|
||||||
```console |
|
||||||
student@ubuntu:~/piscine/test$ go build |
|
||||||
student@ubuntu:~/piscine/test$ ./test |
|
||||||
hello |
|
||||||
student@ubuntu:~/piscine/test$ |
|
||||||
``` |
|
@ -1,65 +0,0 @@ |
|||||||
## listpushback |
|
||||||
|
|
||||||
### Instructions |
|
||||||
|
|
||||||
Write a function `ListForEach` that applies a function given as argument to the information within each of the list's links. |
|
||||||
|
|
||||||
- The function given as argument must have a pointer as argument: `l *list` |
|
||||||
|
|
||||||
### Expected function and structure |
|
||||||
|
|
||||||
```go |
|
||||||
type Node struct { |
|
||||||
Data interface{} |
|
||||||
Next *Node |
|
||||||
} |
|
||||||
|
|
||||||
type List struct { |
|
||||||
Head *Node |
|
||||||
Tail *Node |
|
||||||
} |
|
||||||
|
|
||||||
func ListForEach(l *list, f func(l *list)) { |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
### Usage |
|
||||||
|
|
||||||
Here is a possible [program](TODO-LINK) to test your function : |
|
||||||
|
|
||||||
```go |
|
||||||
package main |
|
||||||
|
|
||||||
import ( |
|
||||||
"fmt" |
|
||||||
piscine ".." |
|
||||||
) |
|
||||||
|
|
||||||
func main() { |
|
||||||
link := &List{} |
|
||||||
|
|
||||||
piscine.ListPushBack(link, 1) |
|
||||||
piscine.ListPushBack(link, 2) |
|
||||||
piscine.ListPushBack(link, 3) |
|
||||||
piscine.ListPushBack(link, 4) |
|
||||||
|
|
||||||
piscine.ListForEach(link, piscine.ListReverse) |
|
||||||
|
|
||||||
for link.Head != nil { |
|
||||||
fmt.Println(link.Head.Data) |
|
||||||
link.Head = link.Head.Next |
|
||||||
} |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
And its output : |
|
||||||
|
|
||||||
```console |
|
||||||
student@ubuntu:~/piscine/test$ go build |
|
||||||
student@ubuntu:~/piscine/test$ ./test |
|
||||||
4 |
|
||||||
3 |
|
||||||
2 |
|
||||||
1 |
|
||||||
student@ubuntu:~/piscine/test$ |
|
||||||
``` |
|
@ -1,105 +0,0 @@ |
|||||||
## listpushback |
|
||||||
|
|
||||||
### Instructions |
|
||||||
|
|
||||||
Write a function `ListForEachIf` that applies a function given as argument to the information within some links of the list. |
|
||||||
|
|
||||||
- For this you will have to create a function `CompStr`, that returns a `bool`, to compare each elemente of the linked list, to see if it is a string, and than apply the function in the argument of `ListForEachIf`. |
|
||||||
|
|
||||||
- The function given as argument as to have a pointer as argument: `l *list`. |
|
||||||
|
|
||||||
- Use pointers wen ever you can. |
|
||||||
|
|
||||||
### Expected function and structure |
|
||||||
|
|
||||||
```go |
|
||||||
type node struct { |
|
||||||
data interface{} |
|
||||||
next *node |
|
||||||
} |
|
||||||
|
|
||||||
type list struct { |
|
||||||
head *node |
|
||||||
tail *node |
|
||||||
} |
|
||||||
|
|
||||||
func CompStr(l *list) bool { |
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
func ListForEachIf(l *list, f func(l *list), comp func(l *list) bool) { |
|
||||||
|
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
### Usage |
|
||||||
|
|
||||||
Here is a possible [program](TODO-LINK) to test your function : |
|
||||||
|
|
||||||
```go |
|
||||||
package main |
|
||||||
|
|
||||||
import ( |
|
||||||
"fmt" |
|
||||||
piscine ".." |
|
||||||
) |
|
||||||
|
|
||||||
func PrintElem(l *list) { |
|
||||||
fmt.Println(l.head.data) |
|
||||||
} |
|
||||||
|
|
||||||
func StringToInt(l *list) { |
|
||||||
count := 1 |
|
||||||
l.head.data = count |
|
||||||
} |
|
||||||
|
|
||||||
func PrintList(l *list) { |
|
||||||
m := l.head |
|
||||||
for m != nil { |
|
||||||
fmt.Print(m.data, " -> ") |
|
||||||
m = m.next |
|
||||||
} |
|
||||||
|
|
||||||
fmt.Print(l.tail) |
|
||||||
} |
|
||||||
|
|
||||||
func main() { |
|
||||||
link := &list{} |
|
||||||
|
|
||||||
piscine.ListPushBack(link, 1) |
|
||||||
piscine.ListPushBack(link, "hello") |
|
||||||
piscine.ListPushBack(link, 3) |
|
||||||
piscine.ListPushBack(link, "there") |
|
||||||
piscine.ListPushBack(link, 23) |
|
||||||
piscine.ListPushBack(link, "!") |
|
||||||
piscine.ListPushBack(link, 54) |
|
||||||
|
|
||||||
PrintAllList(link) |
|
||||||
|
|
||||||
fmt.Println() |
|
||||||
fmt.Println("--------function applied--------") |
|
||||||
piscine.ListForEachIf(link, PrintElem, CompStr) |
|
||||||
|
|
||||||
piscine.ListForEachIf(link, StringToInt, CompStr) |
|
||||||
|
|
||||||
fmt.Println("--------function applied--------") |
|
||||||
PrintAllList(link) |
|
||||||
|
|
||||||
fmt.Println() |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
And its output : |
|
||||||
|
|
||||||
```console |
|
||||||
student@ubuntu:~/piscine/test$ go build |
|
||||||
student@ubuntu:~/piscine/test$ ./test |
|
||||||
1 -> hello -> 3 -> there -> 23 -> ! -> 54 -> <nil> |
|
||||||
--------function applied-------- |
|
||||||
hello |
|
||||||
there |
|
||||||
! |
|
||||||
--------function applied-------- |
|
||||||
1 -> 1 -> 3 -> 1 -> 23 -> 1 -> 54 -> <nil> |
|
||||||
student@ubuntu:~/piscine/test$ |
|
||||||
``` |
|
@ -1,58 +0,0 @@ |
|||||||
## listpushback |
|
||||||
|
|
||||||
### Instructions |
|
||||||
|
|
||||||
Write a function `ListLast` that returns the last element of the linked list. |
|
||||||
|
|
||||||
### Expected function and structure |
|
||||||
|
|
||||||
```go |
|
||||||
type Node struct { |
|
||||||
Data interface{} |
|
||||||
Next *Node |
|
||||||
} |
|
||||||
|
|
||||||
type List struct { |
|
||||||
Head *Node |
|
||||||
Tail *Node |
|
||||||
} |
|
||||||
|
|
||||||
func ListLast(l *list) *list { |
|
||||||
|
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
### Usage |
|
||||||
|
|
||||||
Here is a possible [program](TODO-LINK) to test your function : |
|
||||||
|
|
||||||
```go |
|
||||||
package main |
|
||||||
|
|
||||||
import ( |
|
||||||
"fmt" |
|
||||||
piscine ".." |
|
||||||
) |
|
||||||
|
|
||||||
func main() { |
|
||||||
link := &list{} |
|
||||||
link2 := &list{} |
|
||||||
|
|
||||||
piscine.ListPushBack(link, "three") |
|
||||||
piscine.ListPushBack(link, 3) |
|
||||||
piscine.ListPushBack(link, "1") |
|
||||||
|
|
||||||
fmt.Println(piscine.ListLast(link).head) |
|
||||||
fmt.Println(piscine.ListLast(link2).head) |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
And its output : |
|
||||||
|
|
||||||
```console |
|
||||||
student@ubuntu:~/piscine/test$ go build |
|
||||||
student@ubuntu:~/piscine/test$ ./test |
|
||||||
&{1 <nil>} |
|
||||||
<nil> |
|
||||||
student@ubuntu:~/piscine/test$ |
|
||||||
``` |
|
@ -1,78 +0,0 @@ |
|||||||
## listpushback |
|
||||||
|
|
||||||
### Instructions |
|
||||||
|
|
||||||
Write a function `ListMerge` that places elements of a list `l2` at the end of an other list `l1`. |
|
||||||
|
|
||||||
- You can't create new elements! |
|
||||||
|
|
||||||
- Use pointers when ever you can. |
|
||||||
|
|
||||||
### Expected function and structure |
|
||||||
|
|
||||||
```go |
|
||||||
type NodeL struct { |
|
||||||
Data interface{} |
|
||||||
Next *NodeL |
|
||||||
} |
|
||||||
|
|
||||||
type List struct { |
|
||||||
Head *NodeL |
|
||||||
Tail *NodeL |
|
||||||
} |
|
||||||
|
|
||||||
func listMerge(l1 *List, l2 *List) { |
|
||||||
|
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
### Usage |
|
||||||
|
|
||||||
Here is a possible [program](TODO-LINK) to test your function : |
|
||||||
|
|
||||||
```go |
|
||||||
package main |
|
||||||
|
|
||||||
import ( |
|
||||||
"fmt" |
|
||||||
student ".." |
|
||||||
) |
|
||||||
|
|
||||||
func PrintList(l *List) { |
|
||||||
m := l.Head |
|
||||||
for m != nil { |
|
||||||
fmt.Print(m.Data, " -> ") |
|
||||||
m = m.Next |
|
||||||
} |
|
||||||
|
|
||||||
fmt.Print(nil) |
|
||||||
fmt.Println() |
|
||||||
} |
|
||||||
|
|
||||||
func main() { |
|
||||||
link := &List{} |
|
||||||
link2 := &List{} |
|
||||||
|
|
||||||
student.ListPushBack(link, "a") |
|
||||||
student.ListPushBack(link, "b") |
|
||||||
student.ListPushBack(link, "c") |
|
||||||
student.ListPushBack(link, "d") |
|
||||||
|
|
||||||
student.ListPushBack(link2, "e") |
|
||||||
student.ListPushBack(link2, "f") |
|
||||||
student.ListPushBack(link2, "g") |
|
||||||
student.ListPushBack(link2, "h") |
|
||||||
|
|
||||||
student.ListMerge(link, link2) |
|
||||||
PrintList(link) |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
And its output : |
|
||||||
|
|
||||||
```console |
|
||||||
student@ubuntu:~/student/test$ go build |
|
||||||
student@ubuntu:~/student/test$ ./test |
|
||||||
a -> b -> c -> d -> e -> f -> g -> h -> <nil> |
|
||||||
student@ubuntu:~/student/test$ |
|
||||||
``` |
|
@ -1,60 +0,0 @@ |
|||||||
## listpushback |
|
||||||
|
|
||||||
### Instructions |
|
||||||
|
|
||||||
Write a function `ListPushBack` that inserts a new element `Node` at the end of the list, using the structure `List` |
|
||||||
|
|
||||||
### Expected function and structure |
|
||||||
|
|
||||||
```go |
|
||||||
type Node struct { |
|
||||||
Data interface{} |
|
||||||
Next *Node |
|
||||||
} |
|
||||||
|
|
||||||
type List struct { |
|
||||||
Head *Node |
|
||||||
Tail *Node |
|
||||||
} |
|
||||||
|
|
||||||
func ListPushBack(l *List, data interface{}) { |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
### Usage |
|
||||||
|
|
||||||
Here is a possible [program](TODO-LINK) to test your function : |
|
||||||
|
|
||||||
```go |
|
||||||
package main |
|
||||||
|
|
||||||
import ( |
|
||||||
"fmt" |
|
||||||
piscine ".." |
|
||||||
) |
|
||||||
|
|
||||||
func main() { |
|
||||||
|
|
||||||
link := &List{} |
|
||||||
|
|
||||||
piscine.ListPushBack(link, "Hello") |
|
||||||
piscine.ListPushBack(link, "man") |
|
||||||
piscine.ListPushBack(link, "how are you") |
|
||||||
|
|
||||||
for link.Head != nil { |
|
||||||
fmt.Println(link.Head.Data) |
|
||||||
link.Head = link.Head.Next |
|
||||||
} |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
And its output : |
|
||||||
|
|
||||||
```console |
|
||||||
student@ubuntu:~/piscine/test$ go build |
|
||||||
student@ubuntu:~/piscine/test$ ./test |
|
||||||
Hello |
|
||||||
man |
|
||||||
how are you |
|
||||||
student@ubuntu:~/piscine/test$ |
|
||||||
``` |
|
@ -1,60 +0,0 @@ |
|||||||
## listpushback |
|
||||||
|
|
||||||
### Instructions |
|
||||||
|
|
||||||
Write a function `ListPushBack` that inserts a new element `node` at the beginning of the list using `list` |
|
||||||
|
|
||||||
### Expected function and structure |
|
||||||
|
|
||||||
```go |
|
||||||
type Node struct { |
|
||||||
Data interface{} |
|
||||||
Next *Node |
|
||||||
} |
|
||||||
|
|
||||||
type List struct { |
|
||||||
Head *Node |
|
||||||
Tail *Node |
|
||||||
} |
|
||||||
|
|
||||||
func ListPushFront(l *list, data interface{}) { |
|
||||||
|
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
### Usage |
|
||||||
|
|
||||||
Here is a possible [program](TODO-LINK) to test your function : |
|
||||||
|
|
||||||
```go |
|
||||||
package main |
|
||||||
|
|
||||||
import ( |
|
||||||
"fmt" |
|
||||||
piscine ".." |
|
||||||
) |
|
||||||
|
|
||||||
func main() { |
|
||||||
link := &list{} |
|
||||||
|
|
||||||
piscine.ListPushFront(link, "Hello") |
|
||||||
piscine.ListPushFront(link, "man") |
|
||||||
piscine.ListPushFront(link, "how are you") |
|
||||||
|
|
||||||
for link.head != nil { |
|
||||||
fmt.Println(link.head.data) |
|
||||||
link.head = link.head.next |
|
||||||
} |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
And its output : |
|
||||||
|
|
||||||
```console |
|
||||||
student@ubuntu:~/piscine/test$ go build |
|
||||||
student@ubuntu:~/piscine/test$ ./test |
|
||||||
how are you |
|
||||||
man |
|
||||||
Hello |
|
||||||
student@ubuntu:~/piscine/test$ |
|
||||||
``` |
|
@ -1,99 +0,0 @@ |
|||||||
## listpushback |
|
||||||
|
|
||||||
### Instructions |
|
||||||
|
|
||||||
Write a function `ListRemoveIf` that removes all elements that are equal to the `data_ref` introduced in the argument of the function. |
|
||||||
|
|
||||||
- Use pointers wen ever you can. |
|
||||||
|
|
||||||
### Expected function and structure |
|
||||||
|
|
||||||
```go |
|
||||||
type NodeL struct { |
|
||||||
Data interface{} |
|
||||||
Next *NodeL |
|
||||||
} |
|
||||||
|
|
||||||
type List struct { |
|
||||||
Head *NodeL |
|
||||||
Tail *NodeL |
|
||||||
} |
|
||||||
|
|
||||||
func ListPushFront(l *List, data interface{}) { |
|
||||||
|
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
### Usage |
|
||||||
|
|
||||||
Here is a possible [program](TODO-LINK) to test your function : |
|
||||||
|
|
||||||
```go |
|
||||||
package main |
|
||||||
|
|
||||||
import ( |
|
||||||
"fmt" |
|
||||||
piscine ".." |
|
||||||
) |
|
||||||
|
|
||||||
func PrintList(l *List) { |
|
||||||
m := l.Head |
|
||||||
for m != nil { |
|
||||||
fmt.Print(m.Data, " -> ") |
|
||||||
m = m.Next |
|
||||||
} |
|
||||||
|
|
||||||
fmt.Print(nil) |
|
||||||
fmt.Println() |
|
||||||
} |
|
||||||
|
|
||||||
func main() { |
|
||||||
link := &List{} |
|
||||||
link2 := &List{} |
|
||||||
link3 := &List{} |
|
||||||
|
|
||||||
|
|
||||||
fmt.Println("----normal state----") |
|
||||||
student.ListPushBack(link2, 1) |
|
||||||
PrintList(link2) |
|
||||||
ListRemoveIf(link2, 1) |
|
||||||
fmt.Println("------answer-----") |
|
||||||
PrintList(link) |
|
||||||
fmt.Println() |
|
||||||
|
|
||||||
fmt.Println("----normal state----") |
|
||||||
student.ListPushBack(link, 1) |
|
||||||
student.ListPushBack(link, "Hello") |
|
||||||
student.ListPushBack(link, 1) |
|
||||||
student.ListPushBack(link, "There") |
|
||||||
student.ListPushBack(link, 1) |
|
||||||
student.ListPushBack(link, 1) |
|
||||||
student.ListPushBack(link, "How") |
|
||||||
student.ListPushBack(link, 1) |
|
||||||
student.ListPushBack(link, "are") |
|
||||||
student.ListPushBack(link, "you") |
|
||||||
student.ListPushBack(link, 1) |
|
||||||
PrintList(link) |
|
||||||
|
|
||||||
ListRemoveIf(link, 1) |
|
||||||
fmt.Println("------answer-----") |
|
||||||
PrintList(link) |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
And its output : |
|
||||||
|
|
||||||
```console |
|
||||||
student@ubuntu:~/student/test$ go build |
|
||||||
student@ubuntu:~/student/test$ ./test |
|
||||||
----normal state---- |
|
||||||
1 -> <nil> |
|
||||||
------answer----- |
|
||||||
<nil> |
|
||||||
|
|
||||||
----normal state---- |
|
||||||
1 -> Hello -> 1 -> There -> 1 -> 1 -> How -> 1 -> are -> you -> 1 -> <nil> |
|
||||||
------answer----- |
|
||||||
Hello -> There -> How -> are -> you -> <nil> |
|
||||||
student@ubuntu:~/piscine/test$ |
|
||||||
``` |
|
@ -1,65 +0,0 @@ |
|||||||
## listpushback |
|
||||||
|
|
||||||
### Instructions |
|
||||||
|
|
||||||
Write a function `ListReverse` that reverses the elements order of a given linked list. |
|
||||||
|
|
||||||
- Use pointers when ever you can |
|
||||||
|
|
||||||
### Expected function and structure |
|
||||||
|
|
||||||
```go |
|
||||||
type Node struct { |
|
||||||
Data interface{} |
|
||||||
Next *Node |
|
||||||
} |
|
||||||
|
|
||||||
type List struct { |
|
||||||
Head *Node |
|
||||||
Tail *Node |
|
||||||
} |
|
||||||
|
|
||||||
func ListReverse(l *list) { |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
### Usage |
|
||||||
|
|
||||||
Here is a possible [program](TODO-LINK) to test your function : |
|
||||||
|
|
||||||
```go |
|
||||||
package main |
|
||||||
|
|
||||||
import ( |
|
||||||
"fmt" |
|
||||||
piscine ".." |
|
||||||
) |
|
||||||
|
|
||||||
func main() { |
|
||||||
link := &List{} |
|
||||||
|
|
||||||
listPushBack(link, 1) |
|
||||||
listPushBack(link, 2) |
|
||||||
listPushBack(link, 3) |
|
||||||
listPushBack(link, 4) |
|
||||||
|
|
||||||
listReverse(link) |
|
||||||
|
|
||||||
for link.Head != nil { |
|
||||||
fmt.Println(link.Head.Data) |
|
||||||
link.Head = link.Head.Next |
|
||||||
} |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
And its output : |
|
||||||
|
|
||||||
```console |
|
||||||
student@ubuntu:~/piscine/test$ go build |
|
||||||
student@ubuntu:~/piscine/test$ ./test |
|
||||||
4 |
|
||||||
3 |
|
||||||
2 |
|
||||||
1 |
|
||||||
student@ubuntu:~/piscine/test$ |
|
||||||
``` |
|
@ -1,56 +0,0 @@ |
|||||||
## listpushback |
|
||||||
|
|
||||||
### Instructions |
|
||||||
|
|
||||||
Write a function `ListSize` that returns the number of elements in the list. |
|
||||||
|
|
||||||
### Expected function and structure |
|
||||||
|
|
||||||
```go |
|
||||||
type Node struct { |
|
||||||
Data interface{} |
|
||||||
Next *Node |
|
||||||
} |
|
||||||
|
|
||||||
type List struct { |
|
||||||
Head *Node |
|
||||||
Tail *Node |
|
||||||
} |
|
||||||
|
|
||||||
func ListSize(l *List) int { |
|
||||||
|
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
### Usage |
|
||||||
|
|
||||||
Here is a possible [program](TODO-LINK) to test your function : |
|
||||||
|
|
||||||
```go |
|
||||||
package main |
|
||||||
|
|
||||||
import ( |
|
||||||
"fmt" |
|
||||||
piscine ".." |
|
||||||
) |
|
||||||
|
|
||||||
func main() { |
|
||||||
link := &List{} |
|
||||||
|
|
||||||
piscine.ListPushFront(link, "Hello") |
|
||||||
piscine.ListPushFront(link, "2") |
|
||||||
piscine.ListPushFront(link, "you") |
|
||||||
piscine.ListPushFront(link, "man") |
|
||||||
|
|
||||||
fmt.Println(piscine.ListSize(link)) |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
And its output : |
|
||||||
|
|
||||||
```console |
|
||||||
student@ubuntu:~/piscine/test$ go build |
|
||||||
student@ubuntu:~/piscine/test$ ./test |
|
||||||
4 |
|
||||||
student@ubuntu:~/piscine/test$ |
|
||||||
``` |
|
@ -1,89 +0,0 @@ |
|||||||
## listpushback |
|
||||||
|
|
||||||
### Instructions |
|
||||||
|
|
||||||
Write a function `ListSort` that sorts the linked list by ascending order. |
|
||||||
|
|
||||||
- This time you only will have the `Nodee` structure. |
|
||||||
|
|
||||||
- Try to use recursive. |
|
||||||
|
|
||||||
- Use pointers when ever you can. |
|
||||||
|
|
||||||
### Expected function and structure |
|
||||||
|
|
||||||
```go |
|
||||||
type Nodee struct { |
|
||||||
Data int |
|
||||||
Next *Nodee |
|
||||||
} |
|
||||||
|
|
||||||
func ListSort(l *NodeL) *NodeL { |
|
||||||
|
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
### Usage |
|
||||||
|
|
||||||
Here is a possible [program](TODO-LINK) to test your function : |
|
||||||
|
|
||||||
```go |
|
||||||
package main |
|
||||||
|
|
||||||
import ( |
|
||||||
"fmt" |
|
||||||
student ".." |
|
||||||
) |
|
||||||
|
|
||||||
//Prints the list |
|
||||||
func PrintList(l *Nodee) { |
|
||||||
m := l |
|
||||||
for m != nil { |
|
||||||
fmt.Print(m.Data, " -> ") |
|
||||||
m = m.next |
|
||||||
} |
|
||||||
|
|
||||||
fmt.Print(nil) |
|
||||||
fmt.Println() |
|
||||||
} |
|
||||||
|
|
||||||
//insert elements |
|
||||||
func listPushBack(l *Nodee, Data int) { |
|
||||||
|
|
||||||
n := &Nodee{} |
|
||||||
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 := &Nodee{} |
|
||||||
|
|
||||||
listPushBack(link, 5) |
|
||||||
listPushBack(link, 4) |
|
||||||
listPushBack(link, 3) |
|
||||||
listPushBack(link, 2) |
|
||||||
listPushBack(link, 1) |
|
||||||
|
|
||||||
PrintList(student.ListSort(link)) |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
And its output : |
|
||||||
|
|
||||||
```console |
|
||||||
student@ubuntu:~/student/test$ go build |
|
||||||
student@ubuntu:~/student/test$ ./test |
|
||||||
0 -> 1 -> 2 -> 3 -> 4 -> 5 -> <nil> |
|
||||||
student@ubuntu:~/student/test$ |
|
||||||
``` |
|
@ -1,71 +0,0 @@ |
|||||||
## listpushback |
|
||||||
|
|
||||||
### Instructions |
|
||||||
|
|
||||||
Write a function `SortedListMerge` that mereges two lists, `l1` and `l2`, but you have to join them in ascending order. |
|
||||||
|
|
||||||
- Tip each list as to be already sorted, and initialized with 0. |
|
||||||
|
|
||||||
- Use pointers when ever you can. |
|
||||||
|
|
||||||
### Expected function and structure |
|
||||||
|
|
||||||
```go |
|
||||||
type Nodee struct { |
|
||||||
Data interface{} |
|
||||||
Next *Nodee |
|
||||||
} |
|
||||||
|
|
||||||
func SortedListMerge(l1 *Nodee, l2 *Nodee) *Nodee { |
|
||||||
|
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
### Usage |
|
||||||
|
|
||||||
Here is a possible [program](TODO-LINK) to test your function : |
|
||||||
|
|
||||||
```go |
|
||||||
package main |
|
||||||
|
|
||||||
import ( |
|
||||||
"fmt" |
|
||||||
piscine ".." |
|
||||||
) |
|
||||||
|
|
||||||
func PrintList(l *Nodee) { |
|
||||||
m := l |
|
||||||
for m != nil { |
|
||||||
fmt.Print(m.Data, " -> ") |
|
||||||
m = m.Next |
|
||||||
} |
|
||||||
|
|
||||||
fmt.Print(nil) |
|
||||||
fmt.Println() |
|
||||||
} |
|
||||||
|
|
||||||
func main() { |
|
||||||
link := &Nodee{} |
|
||||||
link2 := &Nodee{} |
|
||||||
|
|
||||||
piscine.ListPushBack(link, "5") |
|
||||||
piscine.ListPushBack(link, "3") |
|
||||||
piscine.ListPushBack(link, "7") |
|
||||||
|
|
||||||
piscine.ListPushBack(link2, "1") |
|
||||||
piscine.ListPushBack(link2, "-2") |
|
||||||
piscine.ListPushBack(link2, "4") |
|
||||||
piscine.ListPushBack(link2, "6") |
|
||||||
|
|
||||||
PrintList(SortedListMerge(link, link2)) |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
And its output : |
|
||||||
|
|
||||||
```console |
|
||||||
student@ubuntu:~/piscine/test$ go build |
|
||||||
student@ubuntu:~/piscine/test$ ./test |
|
||||||
-2 -> 0 -> 0 -> 1 -> 3 -> 4 -> 5 -> 6 -> 7 -> <nil> |
|
||||||
student@ubuntu:~/piscine/test$ |
|
||||||
``` |
|
@ -1,87 +0,0 @@ |
|||||||
## listpushback |
|
||||||
|
|
||||||
### Instructions |
|
||||||
|
|
||||||
Write a function `SortListInsert` that inserts `Data_ref` in the linked list, but it as to remain sorted in ascending order. |
|
||||||
|
|
||||||
- The list as to be alredy sorted. |
|
||||||
|
|
||||||
- Use pointers when ever you can. |
|
||||||
|
|
||||||
### Expected function and structure |
|
||||||
|
|
||||||
```go |
|
||||||
type Nodee struct { |
|
||||||
Data int |
|
||||||
Next *Nodee |
|
||||||
} |
|
||||||
|
|
||||||
func SortListInsert(l *Nodee, Data_ref int) *Nodee{ |
|
||||||
|
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
### Usage |
|
||||||
|
|
||||||
Here is a possible [program](TODO-LINK) to test your function : |
|
||||||
|
|
||||||
```go |
|
||||||
package main |
|
||||||
|
|
||||||
import ( |
|
||||||
"fmt" |
|
||||||
piscine ".." |
|
||||||
) |
|
||||||
|
|
||||||
//Prints the list |
|
||||||
func PrintList(l *Nodee) { |
|
||||||
m := l |
|
||||||
for m != nil { |
|
||||||
fmt.Print(m.Data, " -> ") |
|
||||||
m = m.Next |
|
||||||
} |
|
||||||
fmt.Print(nil) |
|
||||||
fmt.Println() |
|
||||||
} |
|
||||||
|
|
||||||
//insert elements |
|
||||||
func listPushBack(l *Nodee, Data int) { |
|
||||||
n := &Nodee{} |
|
||||||
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 := &Nodee{} |
|
||||||
|
|
||||||
listPushBack(link, 1) |
|
||||||
listPushBack(link, 4) |
|
||||||
listPushBack(link, 9) |
|
||||||
|
|
||||||
PrintList(link) |
|
||||||
|
|
||||||
link = sortListInsert(link, -2) |
|
||||||
link = sortListInsert(link, 2) |
|
||||||
PrintList(link) |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
And its output : |
|
||||||
|
|
||||||
```console |
|
||||||
student@ubuntu:~/piscine/test$ go build |
|
||||||
student@ubuntu:~/piscine/test$ ./test |
|
||||||
-2 -> 0 -> 1 -> 2 -> 4 -> 9 -> <nil> |
|
||||||
lee@lee:~/Documents/work/day11/11-16-sortlistinsert/so |
|
||||||
student@ubuntu:~/piscine/test$ |
|
||||||
``` |
|
@ -1,24 +0,0 @@ |
|||||||
## strlen |
|
||||||
|
|
||||||
### Instructions |
|
||||||
|
|
||||||
Write a function that returns the length of a string. |
|
||||||
|
|
||||||
- `len` is forbidden |
|
||||||
|
|
||||||
### Expected function and structure |
|
||||||
|
|
||||||
```go |
|
||||||
func Strlen(str string) int { |
|
||||||
|
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
And its output : |
|
||||||
|
|
||||||
```console |
|
||||||
student@ubuntu:~/piscine/test$ go build |
|
||||||
student@ubuntu:~/piscine/test$ ./test |
|
||||||
4 |
|
||||||
student@ubuntu:~/piscine/test$ |
|
||||||
``` |
|
Loading…
Reference in new issue