当前位置:网站首页>go语言学习笔记四
go语言学习笔记四
2022-07-30 04:45:00 【N. LAWLIET】
go语言结构体的定义
type student struct{
name string
age int
}go语言的结构体是面向对象方法,没有this指针,没有方法复写也没有extends
结构体是值传递,拷贝也是浅拷贝。
结构体可以实现很多种数据结构,可以作为list
比如
type node struct{
val string
next *node
}queue,stack,tree等都可以用结构体实现。
结构体指针声名如下
list := new(node)//生命结构体方法一
(*list).val = "123"
(*list).next = &new(node)
fmt.println((*list))
list2 := &(node("456",&new(node)))
fmt.println(list2)结构体还有切片映射的属性,地址为首字段地址
算法题目:如何根据搜索二叉树先序遍历还原二叉树
package main
import (
"fmt"
)
var array [10000]int
type bt struct {
val int
left *bt
right *bt
}
var index int = 0
func preOrder(root *bt) [10000]int {
if root != nil {
array[index] = root.val
index += 1
preOrder(root.left)
preOrder(root.right)
}
return array
}
func treeMain() {
var root bt
root.val = 10
var left bt
var right bt
left.val = 5
right.val = 15
root.left = &left
root.right = &right
var arr [10000]int = preOrder(&root)
var ans bt = reTree(&arr)
fmt.Println(ans.left.val)
}
func reTree(preArr *[10000]int) bt {
if len(preArr) <= 0 {
var nilBoot bt
return nilBoot
}
var root bt
process1(preArr, 0, 1, 2, &root)
return root
}
func process1(arrays *[10000]int, index int, L int, R int, ansTree *bt) {
if L > len(array)-1 || R > len(array)-1 {
return
}
ansTree.val = arrays[index]
if arrays[L] < arrays[index] {
var left bt
ansTree.left = &left
process1(arrays, L, L+1, R, &left)
}
if arrays[R] > arrays[index] {
var right bt
ansTree.right = &right
process1(arrays, R, L, R+1, &right)
}
}
边栏推荐
- 【软件工程之美 - 专栏笔记】31 | 软件测试要为产品质量负责吗?
- 软件测试员必看!数据库知识mysql查询语句大全
- MYSQL unique constraint
- The Double Pointer Problem (Part 1)
- Whole process scheduling - Azkaban entry and advanced
- (RCE) Remote Code/Command Execution Vulnerability Vulnerability Exercise
- Requirements design document and the changing role of the product manager
- mysql隔离级别
- Double pointer problem (middle)
- webService接口
猜你喜欢

共建共享数字世界的根:阿里云打造全面的云原生开源生态

Six, read application configuration + log configuration

protobuf 中复合数据类型的读写

七、自定义配置
![[MRCTF2020]Hello_misc](/img/ea/0faacf6e544b60e3459d8ace4d5f42.png)
[MRCTF2020]Hello_misc

Unity beginner 5 cameras follow, border control and simple particle control (2 d)

【线性表】- LeetCode力扣三道练习题详解

Learning of redis_Basic part

mysql isolation level

Small programs use npm packages to customize global styles
随机推荐
Discourse 自定义头部链接(Custom Header Links)
Weight line segment tree + line segment tree split/merge + CF1659D
Shanxi group (enterprises) in the second network security skills competition part problem WP (8)
四、Web开发
模拟问题(中)
六、读取应用配置+日志配置
Introduction to database - MySQL simple introduction
DAY17: weak password detection and test
mysql隔离级别
LeetCode Algorithm 328. Parity linked list
Learning of redis_Basic part
Double pointer problem (middle)
动态规划问题(完结篇)
字符串问题(上)
How to use labelme
Go study notes (84) - Go project directory structure
聊一聊什么是SaaS,以及遇到的问题......
22. Why do you need a message queue?What are the advantages of using the message queue?
Detailed explanation of REUSE_ALV_GRID_DISPLAY
Solve the error SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xb7 in position 0: invalid start b