algo/go/05_array/array.go

93 lines
1.8 KiB
Go
Raw Normal View History

2018-10-03 18:30:00 +08:00
package _5_array
import (
"errors"
"fmt"
)
/**
* 1) 数组的插入删除按照下标随机访问操作
* 2数组中的数据是int类型的
*
* Author: leo
*/
type Array struct {
data []int
length uint
}
//为数组初始化内存
func NewArray(capacity uint) *Array {
if capacity == 0 {
return nil
}
return &Array{
data: make([]int, capacity, capacity),
length: 0,
}
}
func (this *Array) Len() uint {
return this.length
}
2018-10-09 22:35:37 +08:00
//判断索引是否越界
2018-10-03 18:30:00 +08:00
func (this *Array) isIndexOutOfRange(index uint) bool {
2018-11-29 22:24:16 +08:00
if index >= uint(cap(this.data)) {
2018-10-03 18:30:00 +08:00
return true
}
return false
}
//通过索引查找数组,索引范围[0,n-1]
func (this *Array) Find(index uint) (int, error) {
if this.isIndexOutOfRange(index) {
return 0, errors.New("out of index range")
}
return this.data[index], nil
}
//插入数值到索引index上
func (this *Array) Insert(index uint, v int) error {
if this.Len() == uint(cap(this.data)) {
return errors.New("full array")
}
2018-10-09 22:35:37 +08:00
if index != this.length && this.isIndexOutOfRange(index) {
2018-10-03 18:30:00 +08:00
return errors.New("out of index range")
}
for i := this.length; i > index; i-- {
this.data[i] = this.data[i-1]
}
this.data[index] = v
this.length++
return nil
}
func (this *Array) InsertToTail(v int) error {
return this.Insert(this.Len(), v)
}
//删除索引index上的值
func (this *Array) Delete(index uint) (int, error) {
if this.isIndexOutOfRange(index) {
return 0, errors.New("out of index range")
}
v := this.data[index]
for i := index; i < this.Len()-1; i++ {
this.data[i] = this.data[i+1]
}
this.length--
return v, nil
}
//打印数列
func (this *Array) Print() {
var format string
for i := uint(0); i < this.Len(); i++ {
format += fmt.Sprintf("|%+v", this.data[i])
}
fmt.Println(format)
}