搭建ArrayList数据结构
结构图
List
element (interface{}),size
翻译成代码就是
type List struct{
element []interface{
}
size int
}
为了能够写成C++的STL中的vector动态数组相似的在这里引入了两个常量控制增长倍数和收缩倍数
const (
growthFactor=float32(2.0)
shrinkFactor=float32(0.25)
)
这后面的就是数据结构的方法,我方法的命名规格是驼峰式的,但是如果这个方法我不会用在外接的api我会写成小写的否则首字母是大写的
New
第一个是New方法(新建一个线性表)
func New(values ...interface{
})(*List){
list:=&List{
}
if values.size==0
return list
return list.Add(values...)
}
Add
Add : 向线性表里添加节点
func (list *List) Add(values ...interface{
}) {
list.gorwBy(len