Graphic Go Algorithms: Graphically learn data structures and algorithms better than before by yang hu

Graphic Go Algorithms: Graphically learn data structures and algorithms better than before by yang hu

Author:yang hu [hu, yang]
Language: eng
Format: azw3, epub
Published: 2020-06-20T16:00:00+00:00


top = top.next // top move down

p.next = nil

size--

return p

}

func output() {

fmt.Printf("Top " )

var node *Node = nil

for {

node = pop()

if node == nil {

break

}

fmt.Printf("%s -> " , node.data)

}

fmt.Printf("End\n" )

}

func main() {

push("A" )

push("B" )

push("C" )

push("D" )

output()

}

Result:

Top D -> C -> B -> A -> End

Recursive Algorithm

Recursive Algorithm:

The program function itself calls its own layer to progress until it reaches a certain condition and step by step returns to the end..

1. Factorial of n : n*(n-1)*(n-2) ...... *2*1

TestFactorial.go

package main

import "fmt"

func factorial(n int ) int {

if n == 1 {

return 1

} else {

return factorial(n-1 ) * n //Recursively call yourself until the end of the return

}

}

func main() {

var n = 5

var fac = factorial(n)

fmt.Printf("The factorial of 5 is : %d" , fac)

}



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.